Hanging Communication between Sockets
Hiya folks, I've got a situation here that is stumping me. Now, I've found a way around it, but it seems 'hacky' to me, and I kindof would like to understand the issue instead of just dancing around it.
So, I've got a server class listening on port 4444. Talking to it is a client class. The client will send something to the server which will answer. The client then displays the reply on the System.out. Now.. if the server replies with a one line answer, all is well, the client displays the reply and exits. But if the server replies with, say, 3 seperate out.println("message") statements, the client will receive all three replies, then it will hang as if waiting for a fourth... My solution was to have the server send a fourth message that indicates to the client that it is done with transmission, ie: out.println("10-4") and when the client sees "10-4" as a reply, it breaks and moves on in life. I will include the relevant code snippets below so you can see how things are working...out
is the socket's PrintWriter for both the server and client. What happens (see client code) is that if I don't send the "10-4" message, it won't make it to the System.out.println("after inner while loop");
line..
Server code:
while((inputLine = in.readLine()) !=null){
out.println("Hello World! Received "+ inputLine);
out.println("Second Message.");
out.println("Third/Last Message!");
out.println("10-4");
}
Client code:
while ((userInput = stdIn.readLine()) !=null){
out.println(userInput);
//ok, its something having to do with in.readLine not completing
//or something. it isn't finishing the evaluation so the
//while condition never evaluates as true or false
while((received = in.readLine()) !=null){
System.out.println("received: "+received);
if(received.equals("10-4"))
break;
System.out.println("bottom of inner while");
}
System.out.println("after inner while loop");
}

