2 parky18 parky18 于 2014.03.16 19:14 提问

netty 系统空闲一段后,客户端与服务端的链接会自动断开如何处理

大伙有没有出现过这样的问题。用netty做集群服务器,包括客户端(clientBootstrap)和服务端(serverBootstrap),系统空闲3个多小时候,客户端与服务端的连接会自动断开(“远程主机强迫关闭了一个现有的连接”),系统重新后连接报异常:

java.lang.IllegalArgumentException: promise already done: DefaultChannelPromise@2038329b(failure(java.util.concurrent.CancellationException)
at io.netty.channel.DefaultChannelHandlerContext.validatePromise(DefaultChannelHandlerContext.java:806)
at io.netty.channel.DefaultChannelHandlerContext.connect(DefaultChannelHandlerContext.java:477)
at io.netty.channel.DefaultChannelHandlerContext.connect(DefaultChannelHandlerContext.java:467)
at io.netty.channel.DefaultChannelPipeline.connect(DefaultChannelPipeline.java:847)
at io.netty.channel.AbstractChannel.connect(AbstractChannel.java:199)
at io.netty.bootstrap.Bootstrap$2.run(Bootstrap.java:165)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:354)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:353)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:101)
at java.lang.Thread.run(Unknown Source)

最后:
Caused by: java.net.NoRouteToHostException: No route to host: no further information
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(Unknown Source)
at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:191)
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:279)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:461)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:378)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:350)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:101)

请问这个问题如何解决,我有做心跳处理,客户端每个几秒会请求服务端,这样都还会自动断开连接,好郁闷....

2个回答

pl_piaoling
pl_piaoling   2014.03.31 15:16

netty里面有一个自带的心跳handler,加上就行了

paladinosment
paladinosment   2017.02.10 19:06

可以两边都加上option选项。ChannelOption.SO_KEEPALIVE 设置成true就可以。。。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!