如何在不阻塞的情况下从Java中读取BufferedReader?

问题描述:

我想向服务器发送命令,并查看我是否收到回复。

I want to send a command to a server, and find out if I get a response.

现在我正在使用 BufferedReader readline()函数,它阻塞直到服务器响应,但我想做的就是验证服务器是否有响应首先。

Right now i am using BufferedReader's readline() function, which blocks until there's a response from server, but all I want to do is verify that there's a response from the server in the first place.

我尝试使用 ready() reset()为了避免这个阻塞,但它没有帮助。

I tried using ready() or reset() to avoid this block, but it doesn't help.

这导致我的程序卡住等待服务器响应,这从来没有发生。根据我对事物的理解, InputStreamReader 似乎做同样的事情。

This is causing my program to get stuck waiting for the server to respond, which never happens. InputStreamReader seems to do the same thing, by my understanding of things.

我在这里找到的其他问题没有回答我的问题,
所以,如果你能回答我的问题,那就太好了。

Other questions I found here on the subject didn't answer my question, so please if you can answer my question it will be great.

5月你需要的只是 InputStream 而不包装在 BufferedReader

May be all you need is the InputStream without wrapping it in a BufferedReader

while (inputStream.available() > 0) {
     int i = inputStream.read(tmp, 0, 1024);
     if (i < 0)
          break;
     strBuff.append(new String(tmp, 0, i));
}

我希望这有帮助。