empaquetar com.xiaofeiyang; importación io.netty.bootstrap.ServerBootstrap; importación io.netty.channel.ChannelFuture; importación io.netty.channel.ChannelInitializer; importación io.netty.channel.ChannelOption; importación io.netty.channel.EventLoopGroup; importación io.netty.channel.nio.NioEventLoopGroup; importación io.netty.channel.socket.SocketChannel; importación io.netty.channel.socket.nio.NioServerSocketChannel; importación io.netty.util.AttributeKey; / ** * @author : Yangchun * @description: * @date: Creado en 04/02/2020 12:23 * / público última clase NioServer { públicas estáticas void main (String [] args) lanza la excepción { EventLoopGroup bossGroup = nueva NioEventLoopGroup (1 ); EventLoopGroup workerGroup = nuevo NioEventLoopGroup (); tratar { ServerBootstrap b = nuevo ServerBootstrap (); b.group (bossGroup, workerGroup) .Channel (NioServerSocketChannel. clase ) .childOption (ChannelOption.TCP_NODELAY, cierto ) .childAttr (AttributeKey.newInstance ( "childAttr"), "childAttrValue" ) // .handler (nueva ServerHandler ()) .childHandler ( nueva ChannelInitializer <SocketChannel> () { @ Override pública vacío initchannel (SocketChannel ch) { // ch . .pipeline () addLast (nueva AuthHandler ()); // .. } }); ChannelFuture f = b.bind (8888 ) .sync (); . f.channel () closeFuture () de sincronización (.); } Finalmente { bossGroup.shutdownGracefully (); workerGroup.shutdownGracefully (); } } }
1. Crear un canal de servidor
enlazar()
initAndRegister ()
channelFactory.newChannel ()
Crear un canal a través del cual la reflexión ChannelFactory. ChannelFactory crear un ChannelFactory por NioServerSocketChannel.
Mediante la construcción de la Constitución por el método NioServerSocketChannel
newSocket ()
NioServerSocketChannelConfig ()
AbstractionNioChannel ()
configureBlocking (false)
2, el canal de inicialización
3, el selector de registro
4, puertos unidos