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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值