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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>