zoukankan      html  css  js  c++  java
  • 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();
    
    	}
    
    }
    

      

  • 相关阅读:
    cocos2d-x 纹理研究
    cocos2d-x 获取图片的某像素点的RGBA颜色
    cocos2d-x Menu、MenuItem
    cocos2d-x ScrollView、TableView
    cocos2d-x RenderTexture
    cocos2d-x NotificationCenter
    cocos2d-x ClippingNode
    cocos2d-x Animation
    JDK,JRE,JVM区别与联系(ZZ)
    SQL中join的用法
  • 原文地址:https://www.cnblogs.com/james-roger/p/13572993.html
Copyright © 2011-2022 走看看