public final class Server {
public static void main(String[] args) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)//设置服务端的sockerChannel
.childOption(ChannelOption.TCP_NODELAY, true)//给每一个客户端连接设置tcps属性
.childAttr(AttributeKey.newInstance("childAttr"), "childAttrValue")//客户端连接时绑定一些基本属性
.handler(new ServerHandler())//服务端启动时的逻辑
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) {
ch.pipeline().addLast(new AuthHandler());//每个数据流读写的处理逻辑
//..
}
});
ChannelFuture f = b.bind(8888).sync();//服务端绑定端口
f.channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
netty服务端启动
最新推荐文章于 2024-08-23 15:54:50 发布