code hangs when reading from socket

J

Jani Yusef

I am attempting to send data to a client socket and then turn around
and read the response back from the client. The data is sent but when
I attempt to read the data sent back it hangs. I have checked and the
client is receiving the data just fine and seemingly sending back a
perfectly correct reponse. Is there an error in the code below which
is preventing me from reading the data sent back to me correctly?
Any advice would be much appreciated.

//servo is an instance of ServerSocket and socko is an instance of
Socket


//send the message to attached clients
Socket s=servo.accept();
PrintStream ps=new PrintStream(s.getOutputStream());
ps.println(response);

//Now read the response from the clients
s=servo.accept();//read input from clients
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
response=br.readLine();//STUCK HERE, THE NEXT LINE IS NOT REACHED
 
G

Gordon Beaton

I am attempting to send data to a client socket and then turn around
and read the response back from the client. The data is sent but when
I attempt to read the data sent back it hangs. I have checked and the
client is receiving the data just fine and seemingly sending back a
perfectly correct reponse. Is there an error in the code below which
is preventing me from reading the data sent back to me correctly?
Any advice would be much appreciated.

//servo is an instance of ServerSocket and socko is an instance of
Socket

//send the message to attached clients
Socket s=servo.accept();
PrintStream ps=new PrintStream(s.getOutputStream());
ps.println(response);

//Now read the response from the clients
s=servo.accept();//read input from clients
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
response=br.readLine();//STUCK HERE, THE NEXT LINE IS NOT REACHED

The fact that you call accept() after sending the text seems to
indicate that you expect to receive it back from a *different* client
(or at least on a new connection). Is that the case? Does the client
really read the data from the server, then connect again?

How does the client send the response? Does it send a line of text
(i.e. that ends with a newline)?

Why don't you show us a complete, compilable example that includes
both sides of the connection.

/gordon
 
J

jaialai_technology

OK, I figured out the problem. Justa simple matter of the client not binding
to the waiting port. After I figured that out I just loop over br.readLine()
for each expected connection and respond and all is well in that regard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top