美文网首页
2.Server & Client

2.Server & Client

作者: 八颗小牙坏脾气 | 来源:发表于2018-12-12 23:58 被阅读0次

    服务端和客户端请求应答

    1.Server

    1.1 启动类

    public class Server {
        public static void main(String[] args) throws Exception{
            EventLoopGroup boss = new NioEventLoopGroup();
            EventLoopGroup worker = new NioEventLoopGroup();
    
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            try {
                serverBootstrap.group(boss, worker)
                        .channel(NioServerSocketChannel.class)
                        .childHandler(new ServerChannelInitializer());
    
                ChannelFuture future = serverBootstrap.bind(8888).sync();
                future.channel().closeFuture().sync();
            } finally {
                boss.shutdownGracefully();
                worker.shutdownGracefully();
            }
        }
    }
    

    1.2 ChannelInitializer

    public class ServerChannelInitializer extends ChannelInitializer<SocketChannel> {
        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
            pipeline.addLast(new LengthFieldPrepender(4));
            pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
            pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
            pipeline.addLast(new ServerHandler());
        }
    }
    

    1.3 ServerHandler

    public class ServerHandler extends SimpleChannelInboundHandler<String> {
    
        @Override
        protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
            Thread.sleep(1000);
            System.out.println(ctx.channel().remoteAddress() + ",获取到数据 " + msg);
            ctx.channel().writeAndFlush("from server" + UUID.randomUUID());
        }
    
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            cause.printStackTrace();
            ctx.close();
        }
    }
    

    2.Client

    2.1 启动类

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

    2.2 ChannelInitializer

    public class ClientInitializer extends ChannelInitializer<SocketChannel> {
        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
            pipeline.addLast(new LengthFieldPrepender(4));
            pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
            pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
            pipeline.addLast(new ClientHandler());
        }
    }
    

    2.3 ChannelHandler

    public class ClientHandler extends SimpleChannelInboundHandler<String> {
    
        @Override
        protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
            Thread.sleep(1000);
            System.out.println(ctx.channel().remoteAddress() + "," + msg);
            ctx.channel().writeAndFlush("from client:" + LocalDateTime.now());
        }
    
    // 这个不行
    //    @Override
    //    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
    //        ctx.writeAndFlush("start");
    //    }
    
        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            ctx.writeAndFlush("start");
        }
    
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            cause.printStackTrace();
            ctx.close();
        }
    }
    

    相关文章

      网友评论

          本文标题:2.Server & Client

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