| | |
| | | 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; |
| | |
| | | // 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 |