0

Hi I successfully wrote a program that keeps sending data from client to server and from server back to client where the client prints the data.
But now I'm trying to write one whereby the user enter numbers at the client and they are sent to the server whereby they are summed and sent back to the server.It is not giving any errors but its doesn't work as it should.Can someone please help me with this?

Echo Program(This works)

Client code:

import java.io.*;
import java.net.*;
public class Clientb {

	public static void main(String [] args)
	{
		Socket MyClient = null;
		try{
			 MyClient = new Socket("localhost",7441);
			}
		catch(IOException e)
		{
			System.out.println(e);
			
		}
		
		PrintWriter out = null;
		try{
			out = new PrintWriter(MyClient.getOutputStream(),true); 
		}
		catch(IOException e)
		{
			System.out.println(e);
		}
		
		BufferedReader in = null;
		try{
			in = new BufferedReader(new InputStreamReader(MyClient.getInputStream()));
			}
		catch(IOException e)
		{
			System.out.println(e);
		}
		
		BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Please enter a word: ");
		String line = "";
		try {
			while(line!="finish")
			{
				line = userInput.readLine();
				System.out.println(line);
				out.println(line);
			}
		} catch (IOException e) {
			System.out.println(e);
		}
		
		try {
			while((line=in.readLine())!=null)
			{
				System.out.println(line);
			}
		} catch (IOException e) {
			System.out.println(e);
		}
		
	    try {
	           out.close();
	           in.close();
	       MyClient.close();
	    } 
	    catch (IOException e) {
	       System.out.println(e);
	    }
		}

}

Server Code:

import java.io.*;
import java.net.*;
public class Serverb {
	public static void main(String [] args)
	{
		ServerSocket MyServer = null;
		
		try{
			MyServer = new ServerSocket(7441);
		}
		catch(IOException e)
		{
			System.out.println(e);
		}
		
		Socket clientSocket = null;
		try{
			clientSocket = MyServer.accept();
		}
		catch(IOException e)
		{
			System.out.println(e);
		}
		
		PrintWriter out = null;
		try{
			out = new PrintWriter(clientSocket.getOutputStream(),true); 
		}
		catch(IOException e)
		{
			System.out.println(e);
		}
		
		BufferedReader in = null;
		try{
			in = new BufferedReader(new InputStreamReader( clientSocket.getInputStream()));
			}
		catch(IOException e)
		{
			System.out.println(e);
		}
		
				
		String line="default";
		try {
			while(line!=null)
			{
				line = in.readLine();
				out.println(line);
			}
		} catch (IOException e) {
			System.out.println(e);
		}
				
	}

}

This client sends numbers to server:

import java.io.*;
import java.net.*;
public class Clientc {

	public static void main(String [] args)
	{
		Socket MyClient = null;
		try{
			 MyClient = new Socket("localhost",6666);
			}
		catch(IOException e)
		{
			System.out.println(e);
			
		}
		
		PrintWriter out = null;
		try{
			out = new PrintWriter(MyClient.getOutputStream(),true); 
		}
		catch(IOException e)
		{
			System.out.println(e);
		}
		
		BufferedReader in = null;
		try{
			in = new BufferedReader(new InputStreamReader(MyClient.getInputStream()));
			}
		catch(IOException e)
		{
			System.out.println(e);
		}
		
		BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Please enter the numbers: ");
		String line = "default";
		try {
			while((line=userInput.readLine())!="finish")
			{
				out.println(line);
			}
		} catch (IOException e) {
			System.out.println(e);
		}
		
		try {
			line = in.readLine();
		} catch (IOException e) {
			System.out.println(e);
		}
		System.out.println(line);
		
	    try {
	           out.close();
	           in.close();
	       MyClient.close();
	    } 
	    catch (IOException e) {
	       System.out.println(e);
	    }
		}

}

This server sums up the numbers:

import java.io.*;
import java.net.*;
public class Serverc {
	public static void main(String [] args)
	{
		ServerSocket MyServer = null;
		
		try{
			MyServer = new ServerSocket(6666);
		}
		catch(IOException e)
		{
			System.out.println(e);
		}
		
		Socket clientSocket = null;
		try{
			clientSocket = MyServer.accept();
		}
		catch(IOException e)
		{
			System.out.println(e);
		}
		
		PrintWriter out = null;
		try{
			out = new PrintWriter(clientSocket.getOutputStream(),true); 
		}
		catch(IOException e)
		{
			System.out.println(e);
		}
		
		BufferedReader in = null;
		try{
			in = new BufferedReader(new InputStreamReader( clientSocket.getInputStream()));
			}
		catch(IOException e)
		{
			System.out.println(e);
		}
		
		String [] myArr = new String[50];
		String hold = "";
		int i = 0;
		try {
			while((hold=in.readLine())!=null)
			{
				myArr[i] = hold;
			}
		} catch (IOException e) {
			System.out.println(e);
		}
		
		int sum = 0;
		int j=0;
		while(j<myArr.length)
		{
			sum += Integer.parseInt(myArr[j]);
		}
		
		hold = Integer.toString(sum);
		out.println(hold);	
				
	}

}

Edited by DARK_BYTE: n/a

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by JamesCherrill
0

"its doesn't work as it should" gives us absolutely nothing to work with!
How/why/where doesn't it "work"?
Put more System.out.println statements at strategic points in your code so you can see exactly what is being executed and what isn't, and what values the variables have.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.