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);	
				
	}

}

"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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.