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