package com.xiaofeiyang; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.util.AttributeKey; /** * @author: yangchun * @description: * @date: Created in 2020-04-02 12:23 */ public final class NioServer { 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) .childOption(ChannelOption.TCP_NODELAY, true) .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(); } } }
1. Create a server channel
bind()
initAndRegister ()
channelFactory.newChannel()
Create a channel through which channelFactory reflection. channelFactory create a channelFactory by NioServerSocketChannel.
By constructing the constitution by the method NioServerSocketChannel
newSocket ()
NioServerSocketChannelConfig()
AbstractionNioChannel()
configureBlocking(false)
2, initialization channel
3, registration selector
4, bonded ports