I've the code for server and client classes. I've two classes, ServerApp working as server and ClientApp working as client in my local machine.
I first run the ServerApp class and then the ClientApp class. After running the ClientApp class I write some message in the eclipse console "Hello Server" then the message is printed in ServerApp console displaying the client message.
But when I run the ServerApp class and write some text in console and running the ClientApp does not display any message in the console.
I can not understand how to show two way message communication in eclipse console? Please suggest how I can perform two way communication (client server) in eclipse.
Server App
import java.io.*;
import java.net.*;
public class ServerApp {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
ServerSocket Server_Socket;
Server_Socket = new ServerSocket(5555);
Socket clientSocket = null;
clientSocket = Server_Socket.accept();
DataInputStream input;
input = new DataInputStream(clientSocket.getInputStream());
System.out.println(input.readUTF());
DataOutputStream output;
output = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = "", str2 = "";
while (!str.equals("stop"))
{
str = input.readUTF();
System.out.println("client says: " + str);
str2 = br.readLine();
output.writeUTF(str2);
output.flush();
}
input.close();
clientSocket.close();
Server_Socket.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
Client App
import java.io.*;
import java.net.*;
public class ClientApp {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Socket Client_Socket;
Client_Socket = new Socket("localhost", 5555);
DataInputStream input;
input = new DataInputStream(Client_Socket.getInputStream());
DataOutputStream output;
output = new DataOutputStream(Client_Socket.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = "", str2 = "";
while (!str.equals("stop")) {
str = br.readLine();
output.writeUTF(str);
output.flush();
str2 = input.readUTF();
System.out.println("Server says: " + str2);
}
output.flush();
output.close();
Client_Socket.close();
} catch (Exception e) {
System.out.println(e);
}
}
}