Netty ServerBootstrap 繫結多個埠(程式碼示例)
阿新 • • 發佈:2019-01-07
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();
親測可用