有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

插座io什么时候是java。木卫一。IOException:对等方使用Netty引发的连接重置?

当我使用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) 个答案

  1. # 1 楼答案

    在读取之前尝试检查并捕获异常。它帮助我摆脱了那个例外

    // 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;
            }
    
  2. # 2 楼答案

    此错误的常见原因是您已写入另一端已关闭的连接。换句话说,应用程序协议错误。还有其他原因,但这是最常见的

    NB Netty与此无关