Java – socket: BufferedReader readline() block
I use BufferedReader ReadLine () method to read the response of the remote server (written in C, I can't access the source code)
BufferedReader br = new BufferedReader(new InputStreamReader(in)); String line; while((line = br.readLine())!=null){ [...] }
But it always blocks on the last line until it times out So I use the following code:
int b; while(true){ b = in.read; [...] }
I found that the integer value of the last byte read was 13. I think this is a carriage return, right?
So why is the readLine method blocked? The server usually signals whether the flow has reached the end? thank you.
Solution
In the case of a network connection, the stream terminates when the socket is closed
So it is perfectly normal for readline() to block until it receives the "end of line" or manually close the connection When your readline() receives the last character of the '13' value, the line is read, and the loop starts again, waiting for the next line
There is no difference between "last line" and other lines
To stop the loop, you must manually close the connection somewhere or wait for a timeout However, this cannot be more accurate without more information about your communication protocol