【问题标题】:Multiple port server with Netty 4?使用 Netty 4 的多端口服务器?
【发布时间】:2023-03-10 10:46:01
【问题描述】:

我想在一个 Netty 应用程序中绑定两个服务器套接字(例如端口 8000、8001)。

我尝试合并 DiscardServer 和 EchoServer 示例进行测试。

但是在第一个服务器初始化代码中,

ChannelFuture f = bootstrap1.bind(port).sync();
f.channel().closeFuture().sync(); // <-- program blocks here

程序执行阻塞,因此第二个服务器初始化代码无法到达。

如何使用 Netty 4.0 启动两个不同的端口服务器?

【问题讨论】:

标签: netty


【解决方案1】:

只需评论子句

f.channel().closeFuture().sync(); // <-- program blocks here

【讨论】:

  • 补充一点,该行一直等到服务器套接字关闭。
  • 但是当我尝试在命令行中使用命令 telnet localhost 8081 时出现错误 Trying 127.0.0.1... telnet: Unable to connect to remote host: Connection refused
【解决方案2】:

谢谢大家!!有效。只是为了其他人,我不得不添加 f.channel().closeFuture().sync(); 在第二个 bind() 之后;

    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup)
    .channel(NioServerSocketChannel.class)
    .childHandler(new HttpServerInitializer(sslContext));
    b.bind(4443).sync();

    ServerBootstrap b1 = new ServerBootstrap();
    b1.group(bossGroupNonSSL, workerGroupNonSSL)
        .channel(NioServerSocketChannel.class)
        .childHandler(new HttpServerNonSSL());
    ChannelFuture f = b1.bind(4080).sync();

f.channel().closeFuture().sync();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 2014-10-10
    • 2015-02-10
    相关资源
    最近更新 更多