【问题标题】:How to handle iregulare closed client connections如何处理不规则关闭的客户端连接
【发布时间】:2015-09-18 17:21:14
【问题描述】:

我使用 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

【问题讨论】:

    标签: netty


    【解决方案1】:

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

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
      // cause.printStackTrace();
      ctx.close();
    }
    

    【讨论】:

    • 是的,你也可以删除这个方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    • 2013-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多