When is “java.io.IOException:Connection reset by peer” thrown?

后端 未结 8 989
不思量自难忘°
不思量自难忘° 2020-12-08 13:00
ERROR GServerHandler  - java.io.IOException: Connection reset by peer
java.io.IOException: Connection reset by peer
        at sun.nio.ch.FileDispatcher.read0(Native         


        
8条回答
  •  轮回少年
    2020-12-08 13:20

    For me useful code witch help me was http://rox-xmlrpc.sourceforge.net/niotut/src/NioServer.java

    // The remote forcibly closed the connection, cancel

    // the selection key and close the channel.

        private void read(SelectionKey key) throws IOException {
                SocketChannel socketChannel = (SocketChannel) key.channel();
    
                // Clear out our read buffer so it's ready for new data
                this.readBuffer.clear();
    
                // Attempt to read off the channel
                int numRead;
                try {
                    numRead = socketChannel.read(this.readBuffer);
                } catch (IOException e) {
                    // The remote forcibly closed the connection, cancel
                    // the selection key and close the channel.
                    key.cancel();
                    socketChannel.close();
                    return;
                }
    
                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;
                }
    ...
    

提交回复
热议问题