Azure / DotNetty

DotNetty project – a port of netty, event-driven asynchronous network application framework
Other
4.09k stars 977 forks source link

Got an exception:" An existing connection was forcibly closed by the remote host...." #357

Open liuyugan opened 6 years ago

liuyugan commented 6 years ago

When I initiative disconnected TCP connection from client side, then I got an exception:

Exception: System.Net.Sockets.SocketException (0x80004005): An existing connection was forcibly closed by the remote host at DotNetty.Transport.Channels.Sockets.SocketChannelAsyncOperation.Validate() at DotNetty.Transport.Channels.Sockets.AbstractSocketByteChannel.SocketByteChannelUnsafe.FinishRead(SocketChannelAsyncOperation operation)

How can I avoid this exception?

Below is my code snippet:

` try { serverBootstrap .Group(bossGroup, workerGroup) .Channel() .Option(ChannelOption.SoBacklog, 200) .Handler(new MainHandler()) .ChildHandler( new ActionChannelInitializer( channel => { IChannelPipeline pipeline = channel.Pipeline;

                            pipeline.AddLast(new ByteArrayDecoder());
                            pipeline.AddLast(new ByteArrayEncoder());

                            pipeline.AddLast(new DotnettyServerHandler());
                            pipeline.AddLast(new SendRabbitMqHandler());

                            pipeline.AddLast("heartbeat",
                                new IdleStateHandler(TimeSpan.FromMinutes(_clientTimeout), TimeSpan.FromMinutes(_clientTimeout),
                                    TimeSpan.FromMinutes(_clientTimeout)));
                            pipeline.AddLast(new HeartBeatServerHandler());
                        }));

            IChannel boundChannel = await serverBootstrap.BindAsync(port);

            Console.WriteLine($"Server started port:{port}");`
liuyugan commented 6 years ago

I found the root-cause: client side must invoke "Shutdown" method before invoke Close, when we close a socket connection.