I am trying to make a client send a request to a server and receive a response whilst keeping to connection up.
If i close the socket:
//server side
outToClient.writeBytes("Message to send");
connectionSocket.close();
//client side
serverResponse = inFromServer.readLine();
System.out.println("FROM SERVER: " + serverResponse);
Output on client side
FROM SERVER: Message to send
And after that the connection is lost, obviously.
If i do not close the socket:
//server side
outToClient.writeBytes("Message to send");
//client side
serverResponse = inFromServer.readLine();
System.out.println("FROM SERVER: " + serverResponse);
No output on client side. The server never sends the message or the client never receives it.
Anyone knows a possible reason for this to be happening? The client uses a thread to receive messages and a thread to send messages. The client socket is created in the main client thread so receiver and sender threads use the current socket to communicate.
Thanks in advance.
readlLinewill wait for a newline (or connection closed, obviously).readLinewill block until it receives a newline or a closed connection. The name of the function should give you a hint about that.