-1
SocketChannel read保持读取“-1”字节,这意味着文档结束流。SocketChannel保持读取-1
在ROX NIO教程他说,
if (numRead == -1) {
// Remote entity shut the socket down cleanly. Do the
// same from our end and cancel the channel.
key.channel().close();
key.cancel();
return;
}
但我不知道他是对的,不能把它发生的远程端给我发了N个字节,等待5秒(假设没有超时)然后再向我发送M字节。
在调用read()方法时,我还会得到-1吗?如果是这样,我不想关闭socketChannel并注销它,而是等待下一个M字节。
另一方面,我不断从选择器中看到大量的READ事件,看起来像一个忙等待。
在这种情况下,正确的方法是什么?