Netty什么情况下会抛出java.io.IOException: Connection reset by peer异常?

3

当我使用netty向web服务器发送请求时,我收到以下异常。是什么原因导致了这个异常?

java.io.IOException: Connection reset by peer
        at sun.nio.ch.FileDispatcherImpl.read0(Native Method) ~[na:1.7.0_25]
        at sun.nio.ch.SocketDispatcher.read(Unknown Source) ~[na:1.7.0_25]
        at sun.nio.ch.IOUtil.readIntoNativeBuffer(Unknown Source) ~[na:1.7.0_25]
        at sun.nio.ch.IOUtil.read(Unknown Source) ~[na:1.7.0_25]
        at sun.nio.ch.SocketChannelImpl.read(Unknown Source) ~[na:1.7.0_25]
        at io.netty.buffer.UnpooledHeapByteBuf.setBytes(UnpooledHeapByteBuf.java:237) ~[netty-all-4.0.6.Final.jar:na]
        at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:867) ~[netty-all-4.0.6.Final.jar:na]
        at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:227) ~[netty-all-4.0.6.Final.jar:na]
        at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:87) ~[netty-all-4.0.6.Final.jar:na]
        at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:497) ~[netty-all-4.0.6.Final.jar:na]
        at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:465) ~[netty-all-4.0.6.Final.jar:na]
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:359) ~[netty-all-4.0.6.Final.jar:na]
        at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:101) ~[netty-all-4.0.6.Final.jar:na]
        at java.lang.Thread.run(Unknown Source) ~[na:1.7.0_25]
2个回答

5

这个错误通常的原因是你向已经被另一端关闭的连接写入了数据。换句话说,这是一种应用程序协议错误。还有其他原因,但这是最常见的。

NB Netty与此无关。


你会如何处理/恢复这个问题?目前使用Lettuce连接Redis服务器时遇到了这个问题。 - Alboz

-2

在读取之前尝试检查和捕获异常。这帮助我摆脱了那个异常。

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

不回答问题(“为什么?”);它没有“摆脱[你]的异常”,它只是在不同的地方捕获了它;在关闭通道之前取消密钥是多余的。 - user207421

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接