美文网首页
26.Handler2,具体详见Netty_Lecture

26.Handler2,具体详见Netty_Lecture

作者: 未知的证明 | 来源:发表于2019-03-24 23:54 被阅读0次

    1.Server端

    public class MyServer {
    
        public static void main(String[] args) throws Exception {
            EventLoopGroup bossGroup = new NioEventLoopGroup(1);
            EventLoopGroup workerGroup = new NioEventLoopGroup();
    
            try {
                ServerBootstrap serverBootstrap = new ServerBootstrap();
                serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).
                        childHandler(new MyServerInitializer());
    
                ChannelFuture channelFuture = serverBootstrap.bind(8899).sync();
                channelFuture.channel().closeFuture().sync();
            } finally {
                bossGroup.shutdownGracefully();
                workerGroup.shutdownGracefully();
            }
        }
    }
    

    2.MyClientInitializer

    public class MyServerInitializer extends ChannelInitializer<SocketChannel> {
    
        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            System.out.println(this);
    
            ChannelPipeline pipeline = ch.pipeline();
    
            pipeline.addLast(new MyServerHandler());
        }
    }
    

    3.MyServerHandler

    public class MyServerHandler extends SimpleChannelInboundHandler<ByteBuf> {
    
        private int count;
    
        @Override
        protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
            byte[] buffer = new byte[msg.readableBytes()];
            msg.readBytes(buffer);
    
            String message = new String(buffer, Charset.forName("utf-8"));
    
            System.out.println("服务端接收到的消息内容: " + message);
            System.out.println("服务端接收到的消息数量: " + (++this.count));
    
            ByteBuf responseByteBuf = Unpooled.copiedBuffer(UUID.randomUUID().toString(), Charset.forName("utf-8"));
            ctx.writeAndFlush(responseByteBuf);
        }
    
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            cause.printStackTrace();
            ctx.close();
        }
    }
    

    4.Client

    public class MyClient {
    
        public static void main(String[] args) throws Exception{
            EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    
            try {
                Bootstrap bootstrap = new Bootstrap();
                bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class).
                        handler(new MyClientInitializer());
    
                ChannelFuture channelFuture = bootstrap.connect("localhost", 8899).sync();
                channelFuture.channel().closeFuture().sync();
            } finally {
                eventLoopGroup.shutdownGracefully();
            }
        }
    }
    

    5.MyClientInitializer

    public class MyClientInitializer extends ChannelInitializer<SocketChannel> {
    
        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
    
            pipeline.addLast(new MyClientHandler());
        }
    }
    
    

    6.MyClientHandler

    public class MyClientHandler extends SimpleChannelInboundHandler<ByteBuf> {
    
        private int count;
    
        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            for (int i = 0; i < 10; ++i) {
                ByteBuf buffer = Unpooled.copiedBuffer("sent from client ", Charset.forName("utf-8"));
                ctx.writeAndFlush(buffer);
            }
        }
    
        @Override
        protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
            byte[] buffer = new byte[msg.readableBytes()];
            msg.readBytes(buffer);
    
            String message = new String(buffer, Charset.forName("utf-8"));
    
            System.out.println("客户端接收到的消息内容: " + message);
            System.out.println("客户端接收到的消息数量: " + (++this.count));
        }
    
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            cause.printStackTrace();
            ctx.close();
        }
    }
    

    相关文章

      网友评论

          本文标题:26.Handler2,具体详见Netty_Lecture

          本文链接:https://www.haomeiwen.com/subject/ckhmvqtx.html