I am trying to develop a client/Server communication application in java. But at the very initial stage I am unable to read data string from the socket's Output stream using BufferedReader.readline(). I have googled a lot but could not find the specific answer to my question.
Debugging the client application shows that the client application halts at response.readline().
Server:
package myserver;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MyServer {
public static void main(String[] args) {
String[] tokens = new String[10];
int count = 0;
ServerSocket server = null;
try {
server = new ServerSocket(16);
} catch (IOException ex) {
Logger.getLogger(OdeskServer.class.getName()).log(Level.SEVERE, null, ex);
}
if (server != null) {
while (true) {
PrintWriter response;
BufferedReader command;
String inCommand;
try {
Socket client = server.accept();
System.out.println("Connected!");
response = new PrintWriter(client.getOutputStream());
command = new BufferedReader(new InputStreamReader(client.getInputStream()));
response.print("EHLO\r\n");
command.read(); //just to stop the application to start a new iteration
} catch (IOException ex) {
Logger.getLogger(OdeskServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
Client:
package myclient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MyClient {
public static void main(String[] args) {
Socket server = null;
PrintWriter command;
BufferedReader response;
BufferedReader Console = new BufferedReader(new InputStreamReader(System.in));
try {
server = new Socket("175.107.231.218", 16);
System.out.println("Connected");
command = new PrintWriter(server.getOutputStream());
response = new BufferedReader(new InputStreamReader(server.getInputStream()));
while (server.isBound()) {
String input = response.readLine(); //Here is where the communication halts
System.out.print(input);
}
} catch (UnknownHostException ex) {
Logger.getLogger(OdeskClient.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(OdeskClient.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Any idea about this issue?