0

I am implementing a server client application in java. Situation is that client sends some message to server and server may or may not respond(send back some message).


Have a look at this

is = new DataInputStream(client.getInputStream());

Now i am using is.readUTF(); method to get the message back.

Problem is that readUTF is a blocking call and it stops the program right there to receive a message. As i said that server may or may not respond and i need a way to stop the readUTF method from stopping the program(checking if there is a message or not) and keep on continue if there is no message received.

  • Make the server respond in any case. Also, if the client can continue before receiving the answer, communicate in a second thread. – xehpuk Oct 04 '16 at 13:46

1 Answers1

1

Use DataInputStream.available() to check whether there is data to be read and wrap the socket's InputSteam with a PushbackInputStream as described here.

Community
  • 1
  • 1
adjan
  • 13,371
  • 2
  • 31
  • 48