JAVA NIO Connection reset by peer 異常

客戶端主動斷開與服務端的鏈接,可是若是客戶端掉線,服務端就接收不到了。。java

異常信息socket

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;
    }

注意 若是你的代碼有讀和寫,在客戶端掉線以後ui

key.cancel();
    socketChannel.close();

當你在以後還有調用 SelectionKey 須要判斷是否 有效。this

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/4712855spa

下面這裏給的方法解決不了客戶端掉線問題 https://blog.csdn.net/lopper/article/details/6645905.net

代碼來源: https://blog.csdn.net/weixin_37910453/article/details/86662228code

相關文章
相關標籤/搜索