Netty-初体验

Netty-初体验,第1张

Netty-初体验

目录

服务端程序

 客户端程序


在深入学习Netty之前,首先编写一段简单的服务端和客户端程序,来体验一下Netty是如何进行交互的。

服务端程序

public class NettyServer {

    public static void main(String[] args) throws InterruptedException {
        NioEventLoopGroup boss = new NioEventLoopGroup();
        NioEventLoopGroup work = new NioEventLoopGroup();

        try {
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(work, boss)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 1024)
                    .childHandler(new ChannelInitializer() {
                        @Override
                        protected void initChannel(SocketChannel ch) {
                            ch.pipeline().addLast(new ServerChannelHandler());
                        }
                    });
            ChannelFuture sync = bootstrap.bind(9910).sync();
            sync.channel().closeFuture().sync();
        } finally {
            work.shutdownGracefully();
            boss.shutdownGracefully();
        }
    }

    private static class ServerChannelHandler extends ChannelInboundHandlerAdapter {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) {
            if (msg instanceof ByteBuf) {
                ByteBuf buf = (ByteBuf) msg;
                byte[] bytes = new byte[buf.readableBytes()];
                buf.getBytes(0, bytes, 0, buf.readableBytes());
                System.out.println("Server accept message : " + new String(bytes, Charset.defaultCharset()));
                ctx.writeAndFlush(Unpooled.wrappedBuffer("this is server ... ".getBytes(Charset.defaultCharset())));
            }
        }
    }
}

大家可以先运行main方法,然后在浏览器中访问如下地址:

然后查看开发工具的控制台,可以看到如下图所示的输出内容:

 如果看到如图所示的内容,那么恭喜你,你的第一个Netty的服务端程序编写成功了。

 客户端程序

public class NettyClient {

    public static void main(String[] args) throws InterruptedException {
        NioEventLoopGroup work = new NioEventLoopGroup();

        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(work)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer() {
                        @Override
                        protected void initChannel(SocketChannel ch) {
                            ch.pipeline().addLast(new ClientChannelHandler());
                        }
                    });
            ChannelFuture sync = bootstrap.connect("127.0.0.1", 9910).sync();
            sync.channel().closeFuture().sync();
        } finally {
            work.shutdownGracefully();
        }
    }

    private static class ClientChannelHandler extends ChannelDuplexHandler {

        @Override
        public void channelActive(ChannelHandlerContext ctx) {
            ctx.writeAndFlush(Unpooled.wrappedBuffer("this is client ...".getBytes(Charset.defaultCharset())));
        }

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) {
            if (msg instanceof ByteBuf) {
                ByteBuf buf = (ByteBuf) msg;
                byte[] bytes = new byte[buf.readableBytes()];
                buf.getBytes(0, bytes, 0, buf.readableBytes());
                System.out.println("Client accept message : " + new String(bytes, Charset.defaultCharset()));
                ctx.close();
            }
        }
    }
}

启动客户端程序后,查看控制台有内容输出,表示你的客户端程序也成功了。 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5719740.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存