JAVA NIO Connection reset by peer 異常
阿新 • • 發佈:2019-05-23
客戶端主動斷開與服務端的連線,但是如果客戶端掉線,服務端就接收不到了。。
異常資訊
java.io.IOException: Connection reset by peer
at java.base/sun.nio.ch.FileDispatcherImpl.read0(Native Method)
at java.base/sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:39)
at java.base/sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:276)
at java.base/sun.nio.ch.IOUtil.read(IOUtil.java:245)
at java.base/sun.nio.ch.IOUtil.read(IOUtil.java:223)
at java.base/sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:358)
at space.pankui.nio.chat.ChatNIOServer.read(ChatNIOServer.java:123)
at space.pankui.nio.chat.ChatNIOServer.start(ChatNIOServer.java:100)
at space.pankui.nio.chat.ChatNIOServer.main(ChatNIOServer.java:27)
客戶端掉線了....
/**
* 讀取客戶端訊息
*/
private String read(SelectionKey key) {
SocketChannel socketChannel = (SocketChannel) key.channel();
String readResult = "客戶端傳送資訊:";
try {
ByteBuffer readBuffer = ByteBuffer.allocate(256);
socketChannel.configureBlocking(false);
int read = 0;
try {
read = socketChannel.read(readBuffer);
} catch (IOException e) {
System.out.println("客戶端掉線了....");
// https://stackoverflow.com/questions/8658118/when-is-java-io-ioexceptionconnection-reset-by-peer-thrown
// The remote forcibly closed the connection, cancel
// the selection key and close the channel.
// 需要在這裡做判斷
key.cancel();
socketChannel.close();
return "客戶端掉線";
}
//這個方法只能偵測到客戶端主動斷開與服務端的連線,但是如果客戶端掉線,服務端就接收不到
if (read == -1) {
socketChannel.close();
key.cancel();
}
readBuffer.flip();
String result = new String(readBuffer.array(), StandardCharsets.UTF_8).trim();
System.out.println("客戶端傳送資訊:" + result);
return readResult + result;
} catch (IOException e) {
e.printStackTrace();
}
return readResult;
}
注意 如果你的程式碼有讀和寫,在客戶端掉線之後
key.cancel();
socketChannel.close();
當你在之後還有呼叫 SelectionKey 需要判斷是否 有效。
while (iterator.hasNext()) {
SelectionKey selectionKey = iterator.next();
iterator.remove();
// 每個都需要判斷是否有效,因為客戶端可能掉線了,然後就釋放了
if (selectionKey.isValid() && selectionKey.isAcceptable()) {
this.accept(selectionKey);
}
if (selectionKey.isValid() && selectionKey.isReadable()) {
this.read(selectionKey);
}
if (selectionKey.isValid() && selectionKey.isWritable()) {
this.write(selectionKey);
}
}
參考 https://stackoverflow.com/a/28645076/4712855
下面這裡給的方法解決不了客戶端掉線問題 https://blog.csdn.net/lopper/article/details/6645905
