• spirngboot使用netty实现UDP协议接收数据


    compile group: 'io.netty', name: 'netty-all', version: '4.1.42.Final'

     

    package com.test.udp;
    
    import io.netty.bootstrap.Bootstrap;
    import io.netty.channel.ChannelOption;
    import io.netty.channel.EventLoopGroup;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.nio.NioDatagramChannel;
    
    
    public class NettyServer {
        private final int port;
    
        public NettyServer(int port) {
            this.port = port;
        }
    
        public void start() throws Exception {
            EventLoopGroup bossGroup=new NioEventLoopGroup();
            try {
                //通过NioDatagramChannel创建Channel,并设置Socket参数支持广播
                //UDP相对于TCP不需要在客户端和服务端建立实际的连接,因此不需要为连接(ChannelPipeline)设置handler
                Bootstrap b=new Bootstrap();
                b.group(bossGroup)
                        .channel(NioDatagramChannel.class)
                        .option(ChannelOption.SO_BROADCAST, true)
                        .handler(new ServerHandler());
                b.bind(port).sync().channel().closeFuture().await();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            finally{
                bossGroup.shutdownGracefully();
            }
        }
    
    }
    

     启动类

    public class ThumbnailServiceApplication {
    
    	public static void main(String[] args) throws Exception {
    		SpringApplication.run(ThumbnailServiceApplication.class, args);
    		new NettyServer(12345).start();
    
    	}
    
    }
    

      

  • 相关阅读:
    LeetCode 560. Subarray Sum Equals K (子数组之和等于K)
    25、LinkedList特有方法
    24、List三个子类的特点
    23、数据结构之数组和链表
    22、Vector简介
    21、List遍历时修改元素的问题
    20、List集合中特有的方法
    19、集合概述
    18、Random类简介
    17、enum简介
  • 原文地址:https://www.cnblogs.com/james-roger/p/13572993.html
Copyright © 2020-2023  润新知