This basic client server program compiles fine in eclipse IDE but I keep getting a zero back from server!!! I'm new to all this client server stuff by the way.

package echo3;

import java.io.*;
import java.net.*;

class Client
 {
 public static void main(String argv[]) throws Exception
  {

   int outgoingdata = 0;
   int incomingData = 0;

   BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
   Socket clientSocket = new Socket("localhost", 4444);
   DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
   BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

   System.out.println("Input a Number:");
   outgoingdata = inFromUser.read();
   outToServer.writeInt(outgoingdata + '\n');

   incomingData = inFromServer.read();
   System.out.println("FROM SERVER: " + incomingData);
   clientSocket.close();

 }
}


package echo3;

import java.net.*;
import java.io.*;

class Server
 {
 public static void main(String argv[]) throws Exception
  {
    int incomingData = 0;
    int outgoingData = 0;
    ServerSocket welcomeSocket = new ServerSocket(4444);

    while (true)
     {
        Socket connectionSocket = welcomeSocket.accept();
        BufferedReader inFromClient =
           new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
        DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());

        incomingData = inFromClient.read();
        outgoingData = incomingData;
        outToClient.writeInt(outgoingData);
     }
  }
}

Also this is my typical console output:

Input a Number:
5
FROM SERVER: 0

It's always zero but it should be the same as what the user entered as input, in this case output should be 5!!!

Thanks in advance

-Gary

Just add lots of print statements to see where that value is going missing.
ps: Yopur code seems to mix reading chars vs writing ints, and Data IO streams vs ordinary IO streams. That's a formula for confusion. Stick to one type of stream (eg ordinary) and one data format (eg String).

O.k I put in a println() to see the data, also I've changed .writeInt to .write and have had some success. No changes were made to the Server class though.

package echo3;

import java.io.*;
import java.net.*;

class Client
{
 public static void main(String argv[]) throws Exception
  {

   int outgoingData = 0;
   int incomingData = 0;

   BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
   Socket clientSocket = new Socket("localhost", 4444);
   DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
   BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

   System.out.println("Input a Number:");

   outgoingData = inFromUser.read();

   System.out.println("before sending:\n" + outgoingData);

   outToServer.write(outgoingData + '\n');

   incomingData = inFromServer.read();
   System.out.println("FROM SERVER:\n" + incomingData);
   clientSocket.close();

  }
}

You can see from below that there is a problem with inFromUser.read() but I'm clueless as to why!!!

Input a Number:
5
before sending:
53
FROM SERVER:
63

Are the results correct? If not please explain what is wrong and what the print out should have been.

Sorry about that, the number should stay as 5, but magicly it transforms into 53 and is then sent to the server class which magicly adds 10 and returns 63!!!

Input a Number:
5
before sending:
53
FROM SERVER:
63

p.s this is what it should look like, if the input was 5.

Input a Number:
5
before sending:
5
FROM SERVER:
5

You are entering the character '5' which has an int value of 53.
Add some debug println code to the server to show what it receives and sends.

The + '\n' is adding 10 to the 53.

Thanks for that, you were right about the \n causing the extra 10 to be added. As for the part about me entering characters, I thought I was entering int values lol!!! This is the new client class below, the server class is still unchanged from the first post.

package echo3;

import java.io.*;
import java.net.*;

class Client
{
 public static void main(String argv[]) throws Exception
  {

   int outgoingData = 0;
   int incomingData = 0;
   Socket clientSocket = new Socket("localhost", 4444);

   //BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));

   DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
   BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

   System.out.println("Input a Number:");
   outgoingData = System.in.read();

   //outgoingData = inFromUser.read();

   System.out.println("before sending:\n" + outgoingData);
   outToServer.write(outgoingData);

   incomingData = inFromServer.read();
   System.out.println("FROM SERVER:\n" + incomingData);
   clientSocket.close();

  }
}

The output is now showing that the server gets 53 as input and returns it again which is correct but the user input of 5 is still been changed to 53!!! I thought that changing "outgoingData = inFromUser.read();" to "outgoingData = System.in.read();" would stop 5 becoming 53 but as you can see from the console output below, I was utterly wrong. I may need advice regarding this, thanks in advance.

Input a Number:
5
before sending:
53
FROM SERVER:
53

You can not normally enter int values from a keyboard. Normal input is character.
To print a '5' vs 53, cast the int to char.

System.out.println("FROM SERVER:\n" + (char)incomingData);

Nice one, the type casting works for the data sent from the server back to the client, but I will need the server to recieve the integer value 5 and the reason is that I will need the server to perform some minor calculations with the user input value sent from the client... so if I send 5 to be added to 20 on the server it will give back incorrect results because the server is actually sent the value 53, but maybe if I try the type casting in the server class I might be able to turn the received value 53 into value 5, perform the math, then send the result to the client class!!! I'll give it a try anyway.

I will need the server to recieve the integer value 5

Then your code will have to convert the char input to an int. The quick and dirty way is to test if the char is a digit and then subtract '0' from it.
If you are working with multidigit values, make it a String and use the Integer class's parse method.

o.k got it working now, thanks for your help Norm.

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.