How to solve the problem of netty Connection reset by peer

netty often reports such exceptions, especially frequently

there are only 3 tcp connections checked, but this exception is reported frequently

error message

java.io.IOException: Connection reset by peer

at sun.nio.ch.FileDispatcherImpl.read0(Native Method) ~[?:1.7.0_55]
at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:39) ~[?:1.7.0_55]
at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223) ~[?:1.7.0_55]
at sun.nio.ch.IOUtil.read(IOUtil.java:192) ~[?:1.7.0_55]
at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:379) ~[?:1.7.0_55]
at io.netty.buffer.UnpooledUnsafeDirectByteBuf.setBytes(UnpooledUnsafeDirectByteBuf.java:447) ~[netty-buffer-4.0.27.Final.jar!/:4.0.27.Final]
at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:881) ~[netty-buffer-4.0.27.Final.jar!/:4.0.27.Final]
at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:241) ~[netty-transport-4.0.27.Final.jar!/:4.0.27.Final]
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:119) [netty-transport-4.0.27.Final.jar!/:4.0.27.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511) [netty-transport-4.0.27.Final.jar!/:4.0.27.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468) [netty-transport-4.0.27.Final.jar!/:4.0.27.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382) [netty-transport-4.0.27.Final.jar!/:4.0.27.Final]
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354) [netty-transport-4.0.27.Final.jar!/:4.0.27.Final]
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:111) [netty-common-4.0.27.Final.jar!/:4.0.27.Final]
at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137) [netty-common-4.0.27.Final.jar!/:4.0.27.Final]
at java.lang.Thread.run(Thread.java:745) [?:1.7.0_55]
Sep.16,2021

this is complicated, and there are mainly several situations:
1) the number of concurrent connections of the server exceeds its carrying capacity, including links waiting to be closed. You can use netstat-an to view network connections.
2) the client forcibly closes the link, but the server is not aware of it and thinks that the normal link continues to send data to the client.
3) Firewall problem: if the network connection passes through the firewall, and the firewall generally has a timeout mechanism, when the network connection does not transmit data for a long time, it will close the TCP session. If it is closed, it will cause an exception


I have encountered this problem. My problem is caused by the size of the codec. You can refer to
MarshallingDecoder decoder = new MarshallingDecoder (provider, 1024).
errors occur when the file size is larger than 1024 bytes
btw, in the exceptionCaught method, printStackTrace can see more detailed errors log, helps solve the problem

Menu