网络连接不工作

Netty Connection not working

我有以下来源:https://hastebin.com/ovekebahij.java

bootstrap.group( eventLoopGroup )
                    .channel( serverSocketChannelClass )
                    .option( ChannelOption.SO_KEEPALIVE, true )
                    .handler( new ChannelInitializer<NioServerSocketChannel>() {
                        @Override
                        protected void initChannel( NioServerSocketChannel nioServerSocketChannel ) throws Exception {
                            callback.onSuccess( preparePipeline( nioServerSocketChannel ) );
                        }
                    });

我不知道为什么,但我的日志告诉我,服务器已成功启动。每次我尝试连接客户端时,它都说无法连接...有人有想法吗?

感谢大家的贡献。

这是因为您的服务器 bootstrap 方法没有阻塞,因此在绑定后关闭套接字。

您应该将代码更改为:

Channel channel = bootstrap.bind(...).sync().channel();
...
channel.closeFuture().sync()

这将确保该方法仅在套接字关闭后 return。