如何处理iregulare关闭的客户端连接

时间:2015-09-16 08:17:50

标签: netty

我使用netty 4.0.23.Final测试了一个简单的服务器。这是telnet服务器的官方示例:

http://netty.io/4.0/xref/io/netty/example/telnet/package-summary.html

现在我的问题是,当我访问服务器并通过杀死pid来杀死客户端iregulary时,我得到以下错误抛出:

java.io.IOException: Connection closed by remote client
    at sun.nio.ch.SocketDispatcher.read0(Native Method)
    at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43)
    at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
    at sun.nio.ch.IOUtil.read(IOUtil.java:192)
    ...
    at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:881)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354)
    ...

原因很清楚,连接是关闭的。我也可以通过:

来处理这种状态
@see io.netty.channel.ChannelInboundHandlerAdapter#channelUnregistered(io.netty.channel.ChannelHandlerContext)

@Override
public void channelUnregistered(final ChannelHandlerContext ctx) throws Exception {
    final String host = ((InetSocketAddress) ctx.channel().remoteAddress()).getAddress().getHostAddress();
    final int port = ((InetSocketAddress) ctx.channel().remoteAddress()).getPort();
    System.out.println(String.format("unregistered host:%s port:%d", host, port));
    super.channelUnregistered(ctx);
}

但是:我怎样才能摆脱/捕获错误信息?我想捕获它,因为它没有显示在我的日志中。我如何更改netty示例?

非常感谢,欢迎任何帮助。

完整示例:http://netty.io/4.0/xref/io/netty/example/telnet/package-summary.html

1 个答案:

答案 0 :(得分:0)

好的,我发现了问题。捕获此异常已经实现。只需取消注释打印堆栈跟踪:

$login
相关问题