• java Netty通信例子


    个人理解:Netty是对socket的进一步封装

    例子1:

    1 启动

    调用 start()

    public class GdsApplication {
        public static void main(String[] args) {
            ApplicationContext applicationContext = SpringApplication.run(GdsApplication.class, args);
            new JniNettyServer("224.1.2.1", 55198).start();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2 JniNettyServer

    调用JniNettyHander类进行处理

    @Slf4j
    public class JniNettyServer {
    
        private InetSocketAddress groupAddress;
        private ChannelFuture f;
    
        public JniNettyServer(String host, int port) {
            groupAddress = new InetSocketAddress(host, port);
        }
    
    
        public JniNettyServer() {
        }
    
        public boolean isConnect() {
            return f.channel().isActive();
        }
    
    
        public void oneChannlStart(int port){
            try {
                EventLoopGroup group = new NioEventLoopGroup();
                Bootstrap b = new Bootstrap();
                b.group(group)
                        .channel(NioDatagramChannel.class)
                        .handler(new JniNettyHander());
                ChannelFuture sync = b.bind(port).sync();
                f.channel().closeFuture().sync();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
    
        }
    
        public void start1(){
            InetSocketAddress groupAddress = new InetSocketAddress("224.1.1.5", 55198);
            System.setProperty("java.net.preferIPv4Stack", "true");
            EventLoopGroup group = new NioEventLoopGroup();
            try {
                NetworkInterface ni = NetUtil.LOOPBACK_IF;
    
                Enumeration<InetAddress> addresses = ni.getInetAddresses();
                InetAddress localAddress = null;
                while (addresses.hasMoreElements()) {
                    InetAddress address = addresses.nextElement();
                    if (address instanceof Inet4Address){
                        localAddress = address;
                    }
                }
                System.out.println(localAddress);
                Bootstrap bootstrap = new Bootstrap();
                //设置NioDatagramChannel
                bootstrap.group(group).channel(NioDatagramChannel.class)
                        .localAddress(localAddress,groupAddress.getPort())
                        //设置Option 组播
                        .option(ChannelOption.IP_MULTICAST_IF, ni)
                        //设置Option 地址
                        .option(ChannelOption.IP_MULTICAST_ADDR, localAddress)
                        //设置地址
                        .option(ChannelOption.SO_REUSEADDR, true)
                        .handler(new JniNettyHander());
    
                //获取NioDatagramChannel
                NioDatagramChannel channel = (NioDatagramChannel) bootstrap.bind(groupAddress.getPort()).sync().channel();
                //加入组
                channel.joinGroup(groupAddress, ni).sync();
                //关闭Channel
                channel.closeFuture().await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                group.shutdownGracefully();//优雅退出
            }
        }
    
        public void start() {
            EventLoopGroup group = new NioEventLoopGroup();
            try {
                InetAddress localAddress= OsInfoUtils.getLinuxLocalInetAddress();
                NetworkInterface ni = NetworkInterface.getByInetAddress(localAddress);
    //            System.setProperty("java.net.preferIPv4Stack", "true");
    //            NetworkInterface ni = NetUtil.LOOPBACK_IF;
    //            Enumeration<InetAddress> addresses = ni.getInetAddresses();
    //            InetAddress localAddress = null;
    //            while (addresses.hasMoreElements()) {
    //                InetAddress address = addresses.nextElement();
    //                if (address instanceof Inet4Address){
    //                    localAddress = address;
    //                }
    //            }
    
    			/*String name = SystemPropManager.getInstance().getSysPropVal("UDP_NI_NAME");
    			NetworkInterface ni = NetworkInterface.getByName(name);
    			if(null==ni){
    				System.out.println("组播【server】NetworkInterface为空。ni:" + name);
    				return;
    			}
    			// NetworkInterface ni = NetUtil.LOOPBACK_IF;
    			Enumeration<InetAddress> addresses = ni.getInetAddresses();
    			InetAddress localAddress = null;
    			while (addresses.hasMoreElements()) {
    				InetAddress address = addresses.nextElement();
    				if (address instanceof Inet4Address) {
    					localAddress = address;
    				}
    			}*/
    
                Bootstrap b = new Bootstrap();
                b.group(group).channelFactory(new ChannelFactory<NioDatagramChannel>() {
                    public NioDatagramChannel newChannel() {
                        return new NioDatagramChannel(InternetProtocolFamily.IPv4);
                    }
                }).option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(2048 * 64))
                        .localAddress(localAddress, groupAddress.getPort()).option(ChannelOption.IP_MULTICAST_IF, ni)
                        .option(ChannelOption.SO_REUSEADDR, true).handler(new ChannelInitializer<NioDatagramChannel>() {
                    @Override
                    public void initChannel(NioDatagramChannel ch) throws Exception {
                        ch.pipeline().addLast(new JniNettyHander());
                    }
                });
    
                NioDatagramChannel ch = (NioDatagramChannel) b.bind(groupAddress.getPort()).sync().channel();
                ch.joinGroup(groupAddress, ni).sync();
                // System.out.println("server");
    
                // ch.closeFuture().await();
    
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // group.shutdownGracefully();
            }
        }
    
        public static void main(String[] args) {
    
            new JniNettyServer("224.0.0.20",9000).start();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145

    3、调用的方法

    重写channelRead0()方法

    public class JniNettyHander extends SimpleChannelInboundHandler<DatagramPacket> {
    
      @Override
        protected void channelRead0(ChannelHandlerContext channelHandlerContext, DatagramPacket msg) throws Exception {
    	//业务逻辑  需要推送的消息处理
    
    	}
      @Override
        public void channelReadComplete(ChannelHandlerContext ctx) {
            ctx.flush();
        }
    
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
            cause.printStackTrace();
            // We don't close the channel because we can keep serving requests.
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    例子2:

    1、启动

    调用NettyService.start() 配置文件中读取IP 端口

    @SpringBootApplication
    public class SocketApplication {
        private static final Logger log = LoggerFactory.getLogger(SocketApplication.class);
    
        public static void main( String[] args ) {
            SpringApplication.run(SocketApplication.class,args);
    
            try {
                Properties properties =  PropertiesLoaderUtils.loadAllProperties("application.properties");
                String host = properties.getProperty("netty.host");
                String port = properties.getProperty("netty.port");
                if (StringUtils.isEmpty(host) || StringUtils.isEmpty(port)) {
                    log.error("netty地址未配置");
                    System.exit(0);
                }
                new NettyService(host, Integer.valueOf(port)).start();
            } catch (Exception e) {
                log.error("Netty server start error:", e);
                System.exit(0);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    2、NettyService

    实现 start() 调用UdpServerHandler

    @Slf4j
    public class NettyService {
    
        private String host;
    
        private final int port;
    
        public NettyService(String host, int port) {
            this.host = host;
            this.port = port;
        }
    
        public void start() throws Exception {
            // 主线程组
    //        EventLoopGroup bossGroup = new NioEventLoopGroup();
            // 工作线程组  处理读写事件
            EventLoopGroup group = new NioEventLoopGroup();
            try {
                //创建客户端
                Bootstrap bootstrap = new Bootstrap();
                // 绑定线程池
                bootstrap.group(group)
                        // 指定使用的channel
                        .channel(NioDatagramChannel.class) //使用 NioSocketChannel 作为客户端的通道实现
                        .option(ChannelOption.SO_BROADCAST, true)
                        .option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(1024*1024))
                        // 绑定监听端口
                        .localAddress(this.host, this.port)
                        // 绑定客户端连接时候触发操作
                        .handler(new UdpServerHandler());
                // 服务器异步创建绑定
                ChannelFuture cf = bootstrap.bind().sync();
                log.info(NettyService.class + "已启动,正在监听: " + cf.channel().localAddress());
                // 关闭服务器通道   //异步关闭通道(不是直接关闭,只是监听有关闭行为后在关闭)
                cf.channel().closeFuture().sync();
            } finally {
                group.shutdownGracefully().sync(); // 释放线程池资源
    //            bossGroup.shutdownGracefully().sync();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    3、触发操作 UdpServerHandler

    重写channelRead0()

    public class UdpServerHandler extends SimpleChannelInboundHandler<DatagramPacket> {
        private static final Logger log = LoggerFactory.getLogger(UdpServerHandler.class);
    
        private WebSocketServer webSocketServer = SpringContextUtil.getBean(WebSocketServer.class);
    
        @Override
        public void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
    
            String receiveMsg = packet.content().toString(CharsetUtil.UTF_8);
    
            //判断接受到的UDP消息是否正确(未实现)
            if (!StringUtils.isEmpty(receiveMsg)) {
    //            log.info("Received UDP Msg:" + receiveMsg);
                webSocketServer.sendInfo(receiveMsg);
            } else {
                log.error("Received UDP messsage error : null");
            }
        }
    
        @Override
        public void channelReadComplete(ChannelHandlerContext ctx) {
            ctx.flush();
        }
    
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
            cause.printStackTrace();
            // We don't close the channel because we can keep serving requests.
        }
    
        @Override
        public void channelActive(ChannelHandlerContext ctx) {
            log.info("与判读建立连接");
            //添加到channelGroup通道组
    //        NettyChannelHandlerPool.channelGroup.add(ctx.channel());
    
    //        ctx.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8));
    //        ctx.pipeline().addLast(new StringDecoder(Charset.forName("GBK")));
            NettyChannelComponent.ctx = ctx;
    //        NettyChannelComponent.clients.add(ctx.channel());
        }
    
        @Override
        public void channelInactive(ChannelHandlerContext ctx) {
            log.info("与判读断开连接");
            //从channelGroup通道组删除
    //        ChannelHandlerPool.channelGroup.remove(ctx.channel());
            NettyChannelComponent.ctx = null;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
  • 相关阅读:
    【Java】JDBC基础使用教程
    【MySQL从0到1】第六篇:内置函数
    【集训DAY5】堆箱子【数学】
    力扣:96.不同的二叉搜索树
    安美数字酒店宽带运营系统SQL注入漏洞复现 [附POC]
    在UniApp中使用uni.makePhoneCall方法调起电话拨打功能
    Python 中的迭代器(iter、next)与生成器(yield)解析
    深度学习之tf.keras.preprocessing.image_dataset_from_directory()函数
    AtCoder Beginner Contest 350 (ABCDEF题)视频讲解
    Pandas数据分析:快速图表可视化各类操作详解+实例代码(三)
  • 原文地址:https://blog.csdn.net/qq_36628003/article/details/125540751