netty最基础入门

首次接触之惑

知道netty是异步的io网络框架,但不知道怎么入手,本人喜欢从最基本的代码开始学习,就是实现server和client的最少太代是什么样的?

服务器的最少代码

1. HelloServer

public class {
    public static void main(String[] args) {
        ServerBootstrap ss = new ServerBootstrap();
        //如果不设置,java.lang.IllegalStateException: group not set
        ss.group(new NioEventLoopGroup());
        //如果不设置,java.lang.IllegalStateException: channel or channelFactory not set
        ss.channel(NioServerSocketChannel.class);
        //如果不设置,java.lang.IllegalStateException: childHandler not set
        ss.childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel socketChannel) throws Exception {
                socketChannel.pipeline().addLast(new HelloServerHandler());
            }
        });
        try {
            //Create a new Channel and bind it.
            //Waits for this future until it is done, and rethrows the cause of the failure if this future failed.
            ChannelFuture f = ss.bind(1234).sync();

            f.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

2. HelloServerHandler
HelloServerHandler的channelRead0是在接收到客户端的数据后如何处理的逻辑

public class HelloServerHandler extends SimpleChannelInboundHandler<String> {

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {
        System.out.println("接收到客户端的数据为:" + s);
        channelHandlerContext.writeAndFlush("writeAndFlush可以输入出客户端数据");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}

客户端的最少代码

1. HelloClient

public class HelloClient {
    public static String host = "127.0.0.1";
    public static int port = 1234;

    public static void main(String[] args) {
        EventLoopGroup group = new NioEventLoopGroup();

        Bootstrap b = new Bootstrap();
        b.group(group)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        socketChannel.pipeline().addLast(new HelloClientHandler());
                    }
                });

        try {
            ChannelFuture f = b.connect(host, port).sync();
            f.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            group.shutdownGracefully();
        }

    }
}

2. HelloClientHandler

public class HelloClientHandler extends SimpleChannelInboundHandler<String> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, String msg) throws Exception {
        System.out.println("接收服务端的数据 : " + msg);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("与服务器连接开始,可以向服务器写入数据");
        //ctx.writeAndFlush("xxxx")
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值