-
服務器如何知道客戶端斷開了呢??????
private void read(SelectionKey key) throws IOException{
SocketChannel socketChannel = (SocketChannel) key.channel();
this.readBuffer.clear();//清除緩衝區,準備接受新數據
System.out.println("===============read");
int numRead;
try{
numRead = socketChannel.read(this.readBuffer);
}catch (IOException e){ // 客戶端斷開連接,這裏會報錯提示遠程主機強迫關閉了一個現有的連接。
offlineUser(key);
key.cancel();
socketChannel.close();
return;
}
}