上饶市公安局wvp平台
zhongrj
2023-03-07 9f8d7e3f8eb1e81e6eab411a415030085ff64da5
src/main/java/com/genersoft/iot/vmp/netty/server/UdpServer.java
@@ -1,10 +1,12 @@
package com.genersoft.iot.vmp.netty.server;
import com.genersoft.iot.vmp.netty.config.MyDecoder;
import com.genersoft.iot.vmp.netty.handle.UdpServerHandler;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
@@ -53,13 +55,16 @@
                            // TODO Auto-generated method stub
                            //创建一个执行Handler的容器
                            ChannelPipeline pipeline = ch.pipeline();
                            pipeline.addLast(new StringDecoder());
                            pipeline.addLast(new StringEncoder());
//                            pipeline.addLast(new StringDecoder());
                            pipeline.addLast("decoder",new MyDecoder());
//                            pipeline.addLast(new StringEncoder());
                            pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 4, 4, -8, 0));
                            //执行具体的自定义处理器
                            pipeline.addLast(group, "handler", new UdpServerHandler());
                        }
                    })//初始化处理器
                    //true / false 多播模式(UDP适用),可以向多个主机发送消息
                    .option(ChannelOption.SO_BROADCAST, true)
                    // 设置UDP读缓冲区为2M