-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事件,看起來像一個忙等待。
在這種情況下,正確的方法是什麼?