BufferedReader readLine() blocks

2020-02-05 07:53发布

When receiving data using readLine(), even though I put a "\n" at the end of the message using the .flush when sending the message, the while loop that reads my message still blocks. Only when closing the socket connection, it leaves the loop.

Here's the client code :

bos = new BufferedOutputStream(socket.
            getOutputStream());
bis = new BufferedInputStream(socket.
            getInputStream());
osw = new OutputStreamWriter(bos, "UTF-8");
osw.write(REG_CMD + "\n");
osw.flush();

isr = new InputStreamReader(bis, "UTF-8");
BufferedReader br = new BufferedReader(isr);

String response = "";
String line;

while((line = br.readLine()) != null){
   response += line;
}

and the server's code:

BufferedInputStream is;
BufferedOutputStream os;

is = new BufferedInputStream(connection.getInputStream());
os = new BufferedOutputStream(connection.getOutputStream());

isr = new InputStreamReader(is);

String query= "";
String line;

while((line = br.readLine()) != null){
   query+= line;
}

String response = executeMyQuery(query);
osw = new OutputStreamWriter(os, "UTF-8");

osw.write(returnCode + "\n");
osw.flush();

My code blocks at the server while loop. Thanks.

6条回答
【Aperson】
2楼-- · 2020-02-05 08:09

I tried a lot of solutions but the only one I found which wasn't blocking execution was:

BufferedReader inStream = new BufferedReader(new 
InputStreamReader(yourInputStream));
String line;
while(inStream.ready() && (line = inStream.readLine()) != null) {
    System.out.println(line);
}

The inStream.ready() returns false if the next readLine() call will block the execution.

查看更多
做自己的国王
3楼-- · 2020-02-05 08:16

if you want to get what's in the socket without being forced to close it simply use ObjectInputStream and ObjectOutputStream ..

Example:

ObjectInputStream ois;
ObjectOutputStream oos;

ois = new ObjectInputStream(connection.getInputStream());

String dataIn = ois.readUTF(); //or dataIn = (String)ois.readObject();

oos = new ObjectOutputStream(connection.getOutputStream());
oos.writeUtf("some message"); //or use oos.writeObject("some message");
oos.flush();

.....
查看更多
在下西门庆
4楼-- · 2020-02-05 08:19

The BufferedReader will keep on reading the input until it reaches the end (end of file or stream or source etc). In this case, the 'end' is the closing of the socket. So as long as the Socket connection is open, your loop will run, and the BufferedReader will just wait for more input, looping each time a '\n' is reached.

查看更多
女痞
5楼-- · 2020-02-05 08:22

This happens because the InputStream is not ready to be red, so it blocks on in.readLine() . Please try this :

boolean exitCondition= false;

while(!exitCondition){
    if(in.ready()){
        if((line=in.readLine())!=null){
            // do whatever you like with the line
        }
    }
}

Of course you have to control the exitCondition .

An other option can be the use of nio package, which allows asynchronised (not blocking) reading but it depend on your need.

查看更多
Ridiculous、
6楼-- · 2020-02-05 08:30

readline() and read() will be blocked while socket doesn't close. So you should close socket:

Socket.shutdownInput();//after reader
Socket.shutdownOutput();//after wirite

rather than Socket.close();

查看更多
Emotional °昔
7楼-- · 2020-02-05 08:33

This is because of the condition in the while-loop: while((line = br.readLine()) != null)

you read a line on every iteration and leve the loop if readLine returns null.

readLine returns only null, if eof is reached (= socked is closed) and returns a String if a '\n' is read.

if you want to exit the loop on readLine, you can omit the whole while-loop und just do:

line = br.readLine()

查看更多
登录 后发表回答