出站的handler,为什么read方法入站被调用?

华雨枫华雨枫 · 2022-04-25 22:08
为什么ServerOutReadHandler extends ChannelOutboundHandlerAdapter,read方法入站出站都会被调用 public void start() throws InterruptedException { /*线程组*/ EventLoopGroup group = new NioEventLoopGroup(); try { /*服务端启动必备*/ ServerBootstrap b = new ServerBootstrap(); b.group(group) .channel(NioServerSocketChannel.class) .localAddress(new InetSocketAddress(port)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new EchoServerORHandler()); ch.pipeline().addLast(new ServerOutReadHandler()); } }); /*异步绑定到服务器,sync()会阻塞到完成*/ ChannelFuture f = b.bind().sync(); LOG.info("服务器启动完成。"); /*阻塞当前线程,直到服务器的ServerChannel被关闭*/ f.channel().closeFuture().sync(); } finally { group.shutdownGracefully().sync(); } } public class EchoClientORHandler extends SimpleChannelInboundHandler<ByteBuf> { /*读取到网络数据后进行业务处理,并关闭连接*/ @Override protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception { System.out.println("client Accept"+msg.toString(CharsetUtil.UTF_8)); //关闭连接 ///ctx.close(); } /*channel活跃后,做业务处理*/ @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { ctx.writeAndFlush(Unpooled.copiedBuffer( "Hello,Netty",CharsetUtil.UTF_8)); } }
0 个回答
暂无回答