【问题标题】:Netty - Channel instantly closedNetty - 频道立即关闭
【发布时间】:2013-09-02 01:29:55
【问题描述】:

我已经开始使用 netty 并且(显然)希望在客户端和服务器之间发送消息。由于我处于早期阶段,我对简单的东西有问题,在这种情况下它正在发送一条消息。这就是我创建服务器和客户端的方式:

客户:

public void run() throws Exception
{
    EventLoopGroup group = new NioEventLoopGroup();
    try
    {
        Bootstrap b = new Bootstrap();
        b.group(group)
                .channel(NioSocketChannel.class)
                .handler(new SecureChatClientInitializer());
        b.option(ChannelOption.SO_KEEPALIVE, true);

        // Start the connection attempt.
        ChannelFuture future = b.connect(new InetSocketAddress(host, port));
        Channel ch = future.awaitUninterruptibly().channel();
        ch.writeAndFlush("hi\r\n");

        // Wait until all messages are flushed before closing the channel.
        if (lastWriteFuture != null)
        {
            lastWriteFuture.sync();
        }
    } finally
    {
        // The connection is closed automatically on shutdown.
        group.shutdownGracefully();
    }
}

服务器:

public void run() throws InterruptedException
{
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try
    {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new SecureChatServerInitializer(sessionManager));
        b.option(ChannelOption.SO_KEEPALIVE, true);
        b.bind(port).sync().channel().closeFuture().sync();

    } finally
    {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

客户端初始化器:

public class SecureChatClientInitializer extends ChannelInitializer<SocketChannel>
{

    @Override
    public void initChannel(SocketChannel ch) throws Exception
    {
           ChannelPipeline pipeline = ch.pipeline();

        SSLEngine engine =
                SecureChatSslContextFactory.getClientContext().createSSLEngine();
        engine.setUseClientMode(true);
        pipeline.addLast("ssl", new SslHandler(engine));
        pipeline.addLast("framer", new DelimiterBasedFrameDecoder(
                8192, Delimiters.lineDelimiter()));
        pipeline.addLast("decoder", new StringDecoder());
        pipeline.addLast("encoder", new StringEncoder());
        pipeline.addLast("handler", new SecureChatClientHandler());
    }
}

服务器初始化器:

public class SecureChatServerInitializer extends ChannelInitializer<SocketChannel>
{

    ...

    @Override
    public void initChannel(SocketChannel ch) throws Exception
    {
        ChannelPipeline pipeline = ch.pipeline();

        SSLEngine engine =
                SecureChatSslContextFactory.getServerContext().createSSLEngine();
        engine.setUseClientMode(false);
        pipeline.addLast("ssl", new SslHandler(engine));
        pipeline.addLast("framer", new DelimiterBasedFrameDecoder(
                8192, Delimiters.lineDelimiter()));
        pipeline.addLast("decoder", new StringDecoder());
        pipeline.addLast("encoder", new StringEncoder());
        pipeline.addLast("handler", new SecureChatServerHandler(sessionManager));
    }
}

正如您在查看源代码时可能已经猜到的那样:是的,其中一部分来自 SecureChatExample。我编辑了它的一部分,不明白为什么它不再工作了。执行客户端时,我只收到一行错误消息:

java.nio.channels.ClosedChannelException

【问题讨论】:

    标签: java netty


    【解决方案1】:

    除此之外,您还可以像这样关闭频道:

      @Override
      public void channelActive(ChannelHandlerContext ctx) {
        ctx.channel().pipeline().remove(this);
      }
    

    【讨论】:

      【解决方案2】:

      只有在您真正想要退出客户端时才调用group.shutdownGracefully();。如果您从客户端中删除该行,Channel 将保持打开状态。

      另外,您只需在发送的所有消息的末尾加上 \n 后缀即可。

      【讨论】:

      • 做到了!谢谢! :D
      猜你喜欢
      • 2016-12-26
      • 2013-12-28
      • 2012-02-10
      • 2019-10-06
      • 2014-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多