Exception in thread "main" java.lang.IllegalStateException: childHandler not set
at io.netty.bootstrap.ServerBootstrap.validate(ServerBootstrap.java:166)
at io.netty.bootstrap.ServerBootstrap.validate(ServerBootstrap.java:45)
at io.netty.bootstrap.AbstractBootstrap.bind(AbstractBootstrap.java:234)
at com.lius.netty.nettyTcp.nettyServerSocket.main(nettyServerSocket.java:34)
解释:childHandler没有设置
解决方法:在Bootstrap或者ServerBootstrap添加方法childHandler
public class nettyServerSocket { public static void main(String[] args) throws InterruptedException { NioEventLoopGroup accept = new NioEventLoopGroup(); NioEventLoopGroup work = new NioEventLoopGroup(); ServerBootstrap server = new ServerBootstrap(); server.group(accept, work) .option(ChannelOption.TCP_NODELAY, true) .channel(NioServerSocketChannel.class) .localAddress(9091) // 添加childHandler解决childHandler not set报错问题 .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { // TODO Auto-generated method stub ch.pipeline().addLast(new HttpServerCodec()); ch.pipeline().addLast(new serverHandler()); } }); server.bind().sync(); // TODO Auto-generated catch block accept.shutdownGracefully().sync(); work.shutdownGracefully().sync(); } }