栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Netty-初体验

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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();
            }
        }
    }
}

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/724190.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号