Netty

1. NettyServer

public class NettyServer {

    public void init() {

    }

    public static void main(String[] args) throws InterruptedException {
        //创建两个事件循环组
        /**
         * 给定两个事件循环组,主事件循环组boss是接收客户端连接,将连接的客户端请求给工作线程组
         * 工作线程组worker负责接收主线程组给的用户连接,进行读写处理
         */

        EventLoopGroup boss = new NioEventLoopGroup(1);
        EventLoopGroup worker = new NioEventLoopGroup(5);

        //启动辅助类 ServerBootstrap
        ServerBootstrap bootstrap = new ServerBootstrap();

        bootstrap
                .group(boss, worker) //注册主线程组合工作线程组
                .channel(NioServerSocketChannel.class)//主线程组处理的channel的类型
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline(); //ChannelPipeLine
                        pipeline.addLast(new StringEncoder()); //添加的是channelHandler
                        pipeline.addLast(new StringDecoder());
                        pipeline.addLast(new ServerHandler());
                        //inboundchannelHandler ->接收客户端的消息Handler
                        //outbondchannelHandler
                    }
                });

        //真正启动服务端  同步启动:会阻塞直至服务端启动
        Channel channel = bootstrap.bind(6666).sync().channel();
        System.out.println("服务端启动");

        //关闭资源
        channel.closeFuture().sync();
        boss.shutdownGracefully();
        worker.shutdownGracefully();
    }
}

2.NettyClient

public class NettyClient {

    public void init() {

    }

    public static void main(String[] args) throws InterruptedException {
        //实例化事件循环组
        NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup();
        // 创建启动辅助类
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(eventLoopGroup)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
                        pipeline.addLast(new StringEncoder());
                        pipeline.addLast(new StringDecoder());
                        //接收服务端的返回数据 -》 继承channelinboundHandle的类
                        pipeline.addLast(new ClientHandler());
                    }
                });

        //同步连接服务端
        Channel channel = bootstrap.connect("127.0.0.1", 6666).sync().channel();
        System.out.println("客户端连接上服务器");

        //给服务端发送数据
        channel.writeAndFlush("hello");

        //关闭资源
        channel.closeFuture().sync();
        eventLoopGroup.shutdownGracefully();

        System.out.println("客户端执行完成");
    }
}

3.ServerHandler

public class ServerHandler extends SimpleChannelInboundHandler<String> {

    //接收用户发送的消息
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        System.out.println("发放");
    }

    //接收用户发送的消息 并回复
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        System.out.println(ctx.channel().remoteAddress()+":"+msg);
        ctx.channel().writeAndFlush("RECV: " + msg);  //回复
    }

    /**
     * 用户上线消息
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        System.out.println(ctx.channel().remoteAddress()+" 上线");
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx)  {
        System.out.println(ctx.channel().remoteAddress()+" 下线");

    }
}

4.ClientHandler

public class ClientHandler extends SimpleChannelInboundHandler<String> {


    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {

    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println(ctx.channel().remoteAddress()+":"+msg);

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值