Some users use the bufferedreader class ready when reading data returned by the server:
1 While (reader. Ready () {2 // execute the read operation, that is, Readline 3}
This method is often used, but the returned results are empty. Why? I checked the help document and checked it online, and summarized it as follows:
1. ready is used to check whether the stream is ready to be read. It is a non-blocking method, so it will return immediately. Because the server is not ready to be read, it will return immediately, therefore, all read results are null.
2. readline is a blocking method. As long as the connection is not disconnected, it will wait until something is returned. When will it return NULL? It will only return null when it reads the end of the data stream.
From: http://blog.csdn.net/neusoftware_20063500/article/details/3723176
In fact, data delay and other problems often occur when reading network data. In this case, java. Io. ioexception: premature EOF is easily encountered when you directly read data in the network stream. You can use the ready function to check whether bufferedreader is ready.
While (! Reader. Ready) {// blocking, wait for a while} while (reader. Readline ()! = NULL) {// execute the operation}
When reader. Readline has been read, false is returned if you continue to perform the ready operation. Therefore, the following two sections of code may cause an endless loop:
1.
While (reader. Readline ()! = NULL) {// execute the operation while (! Reader. Ready) {// blocking, waiting for a while }}
2.
While (reader. Readline ()! = NULL) {// execute the operation} while (! Reader. Ready) {// blocking, waiting for a while}