Java socket read not receiving write sent immediately after connection is established -


I have a Java client and server that I am trying to write a junit test. In my tests, I wait for the connection between the server and the client, and then to write the communication from the server to the customer to test the communication. I have come to know that if I write immediately, then it is not received and the client remains blocked on the BufferedReader.readLine () method. If I add a sleep for half a second between the socket connection and the server writes then everything works well.

I understand why sleep is needed. I know that the socket has been prepared and a backup reader is wrapped around the input stream before waiting for me, my waitContence () method report and server With reports attached to it. My best estimate is that I need to make sure that I call the client's readline method before writing the call method; But I thought that the chairs used to buffer themselves in buffer input and were not read immediately?

Does anyone confirm me (or can deny it) I am sure the client is called readline before calling the client. And if it is necessary, can such a method suggest that the server is ready to receive input without the addition of TCP handshake without applying another handshake?

EDIT: I should have said this as soon as possible, but the test that is failing is testing the ability to reestablish a connection when the old fails . After connecting my fake server with the client, the socket / server kills the socket and a few seconds later a new server reopens the socket and accepts a new connection from the customer. After the client establishes a connection to the second time that the writing fails. My earlier tests which only check a connection and the client gets a written job that works.

Thank you

You do not have to make sure that the server The network layer will read until the traffic layer buffers the first reading until it is used.

After issuing a message to the server be sure to call the flush on the output stream Otherwise, the actual network call can not be made until the buffer fills. In addition, the use of your lidline can be problematic. Maybe you're not releasing the line ending character? Using a more original reading (four []) method may be better.

Comments