netty源碼-server端綁定端口流程


僅用於記錄在分析netty源碼的日志

源碼調用關系圖

netty綁定端口

Netty Server示例

        EventLoopGroup boss = new NioEventLoopGroup(1);
        EventLoopGroup io = new NioEventLoopGroup();
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(boss, io);
        bootstrap.channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<NioSocketChannel>() {
            @Override
            protected void initChannel(NioSocketChannel ch) throws Exception {

            }
        });
        bootstrap.bind(25001).sync().addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess()) {
                    System.out.println("啟動成功");
                } else {
                    future.cause().printStackTrace();
                }
            }
        });

代碼執行到bootstrap.bind(25001)時,netty內部的綁定端口如下:

  1. AbstractBootstrap --> bind() --> doBind() --> doBind0()
  2. NioServerSocketChannel的bind方法在父類AbstractChannel類,所以channel的調用關系:AbstractChannel --> bind()
  3. DefaultChannelPipeline --> bind()
  4. AbstractChannelHandlerContext --> bind()
  5. HeadContext --> bind()
  6. AbstractChannel.AbstractUnsafe --> bind(),然后調用AbstractChannel --> doBind(),而他的實現類看下一步
  7. NioServerSocketChannel --> doBind()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM