netty源码阅读之服务端启动

你的名字 2022-05-17 02:09 269阅读 0赞

netty服务端启动分为以下几个过程:

1、服务端channel的创建

2、服务端channel的初始化

3、注册selector

4、服务端端口的绑定

我将通过以下用户的代码,后面分几篇文章为大家讲解:

  1. public final class Server {
  2. public static void main(String[] args) throws Exception {
  3. EventLoopGroup bossGroup = new NioEventLoopGroup(1);
  4. EventLoopGroup workerGroup = new NioEventLoopGroup();
  5. try {
  6. ServerBootstrap b = new ServerBootstrap();
  7. b.group(bossGroup, workerGroup)
  8. .channel(NioServerSocketChannel.class)
  9. .childOption(ChannelOption.TCP_NODELAY, true)
  10. .childAttr(AttributeKey.newInstance("childAttr"), "childAttrValue")
  11. .handler(new ServerHandler())
  12. .childHandler(new ChannelInitializer<SocketChannel>() {
  13. @Override
  14. public void initChannel(SocketChannel ch) {
  15. ch.pipeline().addLast(new AuthHandler());
  16. //..
  17. }
  18. });
  19. ChannelFuture f = b.bind(8888).sync();
  20. f.channel().closeFuture().sync();
  21. } finally {
  22. bossGroup.shutdownGracefully();
  23. workerGroup.shutdownGracefully();
  24. }
  25. }
  26. }

看完下面几篇文章,需要回答以下几个问题?

1、服务端的socket在哪里初始化?

2、在哪里进行accept连接?

发表评论

表情:
评论列表 (有 0 条评论,269人围观)

还没有评论,来说两句吧...

相关阅读