Netty 5 不听 java
Netty five not listening java
我的 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();
}
}
`
Server.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();
}
任何其他需要的信息,请告诉我编辑 post
这是一个重复的问题。 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 操作。
我的 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();
}
}
`
Server.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();
}
任何其他需要的信息,请告诉我编辑 post
这是一个重复的问题。 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 操作。