EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast("in2", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
pipeline.addLast("in1", new TimeDecoder());
pipeline.addLast("out0", new LengthFieldPrepender(4));
pipeline.addLast("out1", new TimeEncoder());
pipeline.addLast("in3", new TimeServerHandler());
}
})
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
//绑定端口 开始接收连接
//绑定多个端口
ChannelFuture f = b.bind(port);
ChannelFuture f1 = b.bind(8001);
f.channel().closeFuture().sync();
f1.channel().closeFuture().sync();
绑定多个端口代码:
//绑定端口 开始接收连接
//绑定多个端口
ChannelFuture f = b.bind(port);
ChannelFuture f1 = b.bind(8001);
f.channel().closeFuture().sync();
f1.channel().closeFuture().sync();
亲测可用