文章目录
netty版本
- netty版本:
io.netty:netty-all:4.1.33.Final
服务端多端口绑定
-
服务端单进程多端口绑定,如果要停止某一个端口的监听,一定不能停止EventLoop和Accept线程,单独关闭某一个
Channel
即可。package cn.jannal.netty; /** * 使用telnet 127.0.0.1 6000 测试 */ public final class Server { private EventLoopGroup bossGroup = new NioEventLoopGroup(); private EventLoopGroup workerGroup = new NioEventLoopGroup(); private ServerBootstrap bootstrap = new ServerBootstrap(); private ChannelFuture[] ChannelFutures = null; private int beginPort = 0; private int endPort = 0; public Server(int beginPort, int endPort) { this.beginPort = beginPort; this.endPort = endPort; } public static void main(String[] args) { Server server = new Server(6000, 8000); server.start(); try { Thread.sleep(2000); } catch (InterruptedException e) { } server.stopServerChannel(6000); } public void start() { System.out.println("server starting...."); bootstrap.group(bossGroup, workerGroup); bootstrap.channel(NioServerSocketChannel.class); bootstrap.childOption(ChannelOption.SO_REUSEADDR, true); bootstrap.childHandler(new ChannelInitializer() { @Override protected void initChannel(Channel ch) throws Exception { ch.pipeline().addLast(new CountHandler()); } }); if (ChannelFutures == null) { ChannelFutures = new ChannelFuture