gpt4 book ai didi

java - Netty 5不听java

转载 作者:行者123 更新时间:2023-12-02 04:20:48 24 4
gpt4 key购买 nike

我的 netty 5 服务器有问题,它在我启动后立即关闭,这不应该发生,它应该监听端口并继续运行我使用 netty 5 alpha 2 所以这是我的代码:

网络管理器:

public class NetworkManager {

private ChannelHandler handler;
private ChannelInitializer initializer;
private ServerBootstrap bootstrap;
private SocketAddress address;

// Executors
private EventLoopGroup bossGroup;
private EventLoopGroup workerGroup;

public NetworkManager(int port) {
handler = new ChannelHandler();
initializer = new ChannelInitializer(handler);
bootstrap = new ServerBootstrap();
address = new InetSocketAddress(port);
bossGroup = new NioEventLoopGroup();
workerGroup = new NioEventLoopGroup();
}

public void init() {
bootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_REUSEADDR, true)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childOption(ChannelOption.TCP_NODELAY, true)
.childOption(ChannelOption.SO_BACKLOG, 128)
.childHandler(initializer);
}

public void bind() {
try {
bootstrap.bind(address).sync();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

public void destroy() {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}

`

服务器.java:`

public static void main(String[] args) {
init();
start();
}

private static NetworkManager network;

public static void init() {
Settings.init();
network = new NetworkManager(Settings.networkPort);
network.init();
}

public static void start() {
network.bind();

}

任何其他需要的信息,请告诉我,我会编辑帖子

最佳答案

这是一个重复的问题。 See here

简而言之:

您忘记添加等待关闭操作:

// Bind and start to accept incoming connections.
ChannelFuture f = b.bind(PORT).sync();
// Wait until the server socket is closed.
// In this example, this does not happen, but you can do that to gracefully
// shut down your server.
f.channel().closeFuture().sync();

您现在要做的就是启动服务器进行监听,然后在关闭执行程序后完成您自己的主程序。

你必须阻塞某些东西,对服务器来说最好的方法是等待 closeFuture 操作。

关于java - Netty 5不听java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32795739/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com