JAVA NIO Connection reset by peer 异常

客户端主动断开与服务端的连接,但是如果客户端掉线,服务端就接收不到了。。

异常信息

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

代码来源: https://blog.csdn.net/weixin_37910453/article/details/86662228

转载于:https://my.oschina.net/u/1394615/blog/3053331

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值