简体   繁体   English

无法从Java流中读取

[英]Unable to read from the stream in java

I am using tcp socket for client server programming in java . 我正在使用tcp socketjava中进行客户端服务器编程。 After writing a message in the outputstream , I started to read the inputstream and I continues the reading till the inputstream returns -1(EOF). 写在OutputStream的消息后,我开始阅读InputStream和我继续读,直到的InputStream返回-1(EOF)。 But its not returning -1, I got stucked. 但是它没有返回-1,我被卡住了。 Please give your suggestions. 请提出您的建议。 Look at the code below. 看下面的代码。

int bytesRead = tcpSocket.readPacket(buffer);
while(bytesRead != -1) {
    builder.append(new String(buffer, 0, bytesRead));
    bytesRead = tcpSocket.readPacket(buffer);
}

If its a Socket then -1 would be returned when the connection is terminated (normally). 如果它是一个Socket,则在终止连接时(通常)将返回-1。

In your case, I would guess that there is no further data coming through but it is not returning -1 either. 在您的情况下,我想没有更多的数据通过,但也不返回-1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM