zoukankan      html  css  js  c++  java
  • Netty3实现服务端和客户端

    注意点

    1、netty目前版本可以分为两块,一块是3.x,另一块是4.x和5.x,这样分是因为4.x和5.x的api基本相同,但是3.x和4,5的版本api就发生很大的变化
    2、netty目前再分布式进程通信和游戏服务器开发运用的很广泛
     例如: hadoop、dubbo、akka等具有分布式功能的框架,底层RPC通信都是基于netty实现的,这些框架使用的版本通常都还在用netty3.x

    3、SimpleChannelHandler 处理消息接收和写
    {

      messageReceived接收消息

      channelConnected新连接,通常用来检测IP是否是黑名单

      channelDisconnected链接关闭,可以再用户断线的时候清楚用户的缓存数据等
      }

      channelDisconnected只有在连接建立后断开才会调用
      channelClosed无论连接是否成功都会调用关闭资源

    1、服务端代码

    package com.example.netty.lesson2;
    
    import org.jboss.netty.bootstrap.ServerBootstrap;
    
    import org.jboss.netty.channel.ChannelPipeline;
    import org.jboss.netty.channel.Channels;
    import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
    import org.jboss.netty.handler.codec.string.StringDecoder;
    import org.jboss.netty.handler.codec.string.StringEncoder;
    
    import java.net.InetSocketAddress;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    /**
     * @author yangwj
     * @date 2020/4/4 10:52
     */
    public class NettyServer {
        public static void main(String[] args) {
            //服务类
            ServerBootstrap bootstrap = new ServerBootstrap();
            //boss相当于大门的门卫,负责通报
            ExecutorService boss =  Executors.newCachedThreadPool();
            //work接收boss门卫的通报,然后进行数据分发
            ExecutorService work =  Executors.newCachedThreadPool();
    
            //设置nioserver工厂
            bootstrap.setFactory(new NioServerSocketChannelFactory(boss,work));
    
            //设置管道
            bootstrap.setPipelineFactory(()->{
                ChannelPipeline pipeLine = Channels.pipeline();
                //pipeLine可以认为是一个过滤器
                pipeLine.addLast("decoder",new StringDecoder()); //将接受的消息转为String
                pipeLine.addLast("encoder",new StringEncoder()); //将发送的消息转为String
                pipeLine.addLast("helloHandler",new HelloHandler());
                return pipeLine;
    
            });
            bootstrap.bind(new InetSocketAddress(51503));
            System.out.println("服务端start!");
        }
    }

    2、服务端handler

    package com.example.netty.lesson2;
    
    
    import org.jboss.netty.channel.*;
    
    /**
     * @author yangwj
     * @date 2020/4/4 11:09
     */
    public class HelloHandler extends SimpleChannelHandler {
    
        @Override
        public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
            System.out.println("打印server的信息:"+e.getMessage());
    //        ChannelBuffer message = (ChannelBuffer) e.getMessage();
    //        String msg = new String(message.array());
            String msg = (String) e.getMessage();
            System.out.println("打印server的信息:"+msg);
    
    //        ctx.getChannel().write("hi,你好!!");
            super.messageReceived(ctx, e);
        }
    
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
            System.out.println("exceptionCaught");
            super.exceptionCaught(ctx, e);
        }
    
        /**
         * 连接时触发
         * @param ctx
         * @param e
         * @throws Exception
         */
        @Override
        public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
            //可以设置黑名单
            System.out.println("channelConnected");
            super.channelConnected(ctx, e);
        }
    
        /**
         * 断开时首先触发这个
         * @param ctx
         * @param e
         * @throws Exception
         */
        @Override
        public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
            System.out.println("channelDisconnected");
            super.channelDisconnected(ctx, e);
        }
    
        /**
         * channelDisconnected执行完,才会触发这个
         * @param ctx
         * @param e
         * @throws Exception
         */
        @Override
        public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
            System.out.println("channelClosed");
            super.channelClosed(ctx, e);
        }
    }

    3、客户端代码

    package com.example.netty.lesson2;
    
    import org.jboss.netty.bootstrap.ClientBootstrap;
    import org.jboss.netty.channel.Channel;
    import org.jboss.netty.channel.ChannelFuture;
    import org.jboss.netty.channel.ChannelPipeline;
    import org.jboss.netty.channel.Channels;
    import org.jboss.netty.channel.socket.ClientSocketChannelFactory;
    import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
    import org.jboss.netty.handler.codec.string.StringDecoder;
    import org.jboss.netty.handler.codec.string.StringEncoder;
    
    import java.net.InetSocketAddress;
    import java.util.Scanner;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    /**
     * @author yangwj
     * @date 2020/4/4 12:38
     */
    public class NettyClient {
        public static void main(String[] args) {
            //服务类
            ClientBootstrap bootstrap = new ClientBootstrap();
            //线程池
            ExecutorService boss = Executors.newCachedThreadPool();
            ExecutorService work = Executors.newCachedThreadPool();
    
            //管道工厂
            bootstrap.setFactory(new NioClientSocketChannelFactory(boss,work));
            bootstrap.setPipelineFactory(()->{
                ChannelPipeline pipeLine = Channels.pipeline();
                pipeLine.addLast("decoder",new StringDecoder());
                pipeLine.addLast("encoder",new StringEncoder());
                pipeLine.addLast("hiHandler",new HiHandler());
                return pipeLine;
            });
            //连接服务端
            ChannelFuture connect =  bootstrap.connect(new InetSocketAddress("127.0.0.1",51503));
            Channel channel = connect.getChannel();
            System.out.println("客户端start");
            Scanner scanner = new Scanner(System.in);
            while (true){
                System.out.println("请输入:");
                channel.write(scanner.next());
            }
    
        }
    }

    4、客户端handler

    package com.example.netty.lesson2;
    
    import org.jboss.netty.channel.*;
    
    /**
     * @author yangwj
     * @date 2020/4/4 12:44
     */
    public class HiHandler extends SimpleChannelHandler {
        @Override
        public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
            System.out.println("打印server的信息:"+e.getMessage());
    //        ChannelBuffer message = (ChannelBuffer) e.getMessage();
    //        String msg = new String(message.array());
            String msg = (String) e.getMessage();
            System.out.println("打印server的信息:"+msg);
    
            ctx.getChannel().write("hi,你好!!");
            super.messageReceived(ctx, e);
        }
    
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
            System.out.println("exceptionCaught");
            super.exceptionCaught(ctx, e);
        }
    
        /**
         * 连接时触发
         * @param ctx
         * @param e
         * @throws Exception
         */
        @Override
        public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
            //可以设置黑名单
            System.out.println("channelConnected");
            super.channelConnected(ctx, e);
        }
    
        /**
         * 断开时首先触发这个
         * @param ctx
         * @param e
         * @throws Exception
         */
        @Override
        public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
            System.out.println("channelDisconnected");
            super.channelDisconnected(ctx, e);
        }
    
        /**
         * channelDisconnected执行完,才会触发这个
         * @param ctx
         * @param e
         * @throws Exception
         */
        @Override
        public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
            System.out.println("channelClosed");
            super.channelClosed(ctx, e);
        }
    }
  • 相关阅读:
    微信小程序实现滚动到指定位置
    微信小程序,scroll-view组件的使用,跳转到指定的锚点/定位跳转
    小程序,报渲染层错误。图片无法渲染。
    input Input 输入判断/正则
    js,某元素在浏览器页面浮动/飘动
    前端模块化
    npx create-react-app命令不成功,更改成淘宝镜像
    深入理解jdk和jre(转)
    Java学习路线图·影响一代又一代程序员的经典书籍!(转)
    ZAB协议(转)
  • 原文地址:https://www.cnblogs.com/ywjfx/p/12656941.html
Copyright © 2011-2022 走看看