Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sockets: BufferedReader readLine() blocks

I am using BufferedReader.readLine() method to read a response from a remote server (which is written in C and I have no access to source code).

BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
while((line = br.readLine())!=null){
    [...]
}

But it always blocks at the last line until it times out. So I used the following code:

int b;
while(true){
   b = in.read;
   [...]
}

and I found out that the last byte read has an integer value of 13, which I think it is a carriage return, right?

So why the readLine method blocks? How does the server usually signal an end of stream is reached? Thanks.

like image 718
neo Avatar asked May 26 '11 14:05

neo


People also ask

What does BufferedReader readLine do?

The readLine() method of BufferedReader class in Java is used to read one line text at a time. The end of a line is to be understood by '\n' or '\r' or EOF.

Which exception is thrown by readLine () method?

For example, an IOException is a checked exception. Some programs use the readLine() method of BufferedReader for input. This method throws an IOException when there is a problem reading.

Does BufferedReader read line by line?

Java Read File line by line using BufferedReader We can use java. io. BufferedReader readLine() method to read file line by line to String. This method returns null when end of file is reached.

What is the difference between InputStreamReader and BufferedReader?

BufferedReader reads a couple of characters from the Input Stream and stores them in a buffer. InputStreamReader reads only one character from the input stream and the remaining characters still remain in the streams hence There is no buffer in this case.


3 Answers

make sure that the server code has out.println() instead of out.print()

like image 168
MayureshG Avatar answered Oct 06 '22 00:10

MayureshG


In the case of a network connection, the stream is terminated when the socket is closed.

So it is perfectly normal that readLine() blocks until it received an "end of line" or you close manually the connection. When your readLine() receives the last character with the '13' value, the line is read and the loop starts again, waiting for the next line.

There is no difference between the "last line" and the other lines.

In order to stop the loop, you must manually close the connection somewhere or wait for the timeout. But without more information about your communication protocol, it is impossible to be more precise about this.

like image 41
krtek Avatar answered Oct 06 '22 01:10

krtek


It depends on the protocol. If the server doesn't close the stream, readLine will block until the proper line end is received. So if the server never sends the proper line end, you're blocked. You should maybe use more low-level methods and try to get the protocol documentation, or reverse-engineer it.

like image 23
JB Nizet Avatar answered Oct 06 '22 00:10

JB Nizet