Netty 处理Tcp Http Udp
2017-09-05 12:59
369 查看
说明
netty5 监听 tcp端口,http端口,udp端口 分别处理消息1.启动类方法
启动netty 监听端口事件public void init(int httpport,int tcpport,int udpport) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try{ //http和tcp设置 ServerBootstrap bootstrap=new ServerBootstrap(); bootstrap.group(bossGroup,workerGroup); bootstrap.channel(NioServerSocketChannel.class); bootstrap.option(ChannelOption.SO_BACKLOG,1024); //UDP 设置 Bootstrap Bootstrap2 = new Bootstrap();//udp不能使用ServerBootstrap Bootstrap2.group(workerGroup); Bootstrap2 .channel(NioDatagramChannel.class);//设置UDP通道 Bootstrap2 .option(ChannelOption.SO_BROADCAST, true);// 支持广播 Bootstrap2 .option(ChannelOption.SO_BACKLOG, 128); Bootstrap2 .option(ChannelOption.SO_RCVBUF, 1024 * 1024);// 设置UDP读缓冲区为1M Bootstrap2 .option(ChannelOption.SO_SNDBUF, 1024 * 1024);// 设置UDP写缓冲区为1M List ports = Arrays.asList(httpport,tcpport,udpport); Collection channels = new ArrayList(ports.size()); for (int pot : ports) { Channel serverChannel=null; if (pot==udpport){ Bootstrap2 .handler(udpServerInitializer);//初始化处理器 serverChannel = Bootstrap2.bind(pot).sync().channel(); }else if (pot==httpport){ bootstrap.childHandler(httpServerInitializer); serverChannel = bootstrap.bind(pot).sync().channel(); }else if (pot==tcpport){ bootstrap.childHandler(tcpServerInitializer); serverChannel = bootstrap.bind(pot).sync().channel(); } channels.add(serverChannel); } for (Channel ch : channels) { ch.closeFuture().sync(); } // future.channel().closeFuture().sync(); }catch(Exception e){ System.out.println("Server服务端报异常---------"+e.getMessage()); }finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
2.http处理方法格式化化协议处理
@Autowired private HttpServerHandler httpServerHandler; @Override protected void initChannel(SocketChannel socketChannel) throws Exception { /* 处理http处理 */ if (socketChannel.localAddress().getPort()== ConfigProperties.getInt("http.port")){ socketChannel.pipeline().addLast(new HttpServerCodec());/*HTTP 服务的解码器*/ socketChannel.pipeline().addLast(new HttpObjectAggregator(2048));/*HTTP 消息的合并处理*/ socketChannel.pipeline().addLast(httpServerHandler); //处理tcp请求 }else{ log.info("请求端口出错:"+socketChannel.localAddress().getPort()); } }
3.tcp处理方法格式化协议处理
@Autowired private TcpServerHandler tcpServerHandler; @Autowired private TcpHeartBeatServerHandler tcpHeartBeatServerHandler; @Override protected void initChannel(SocketChannel socketChannel) throws Exception { /* 处理tcp处理 */ if (socketChannel.localAddress().getPort()== ConfigProperties.getInt("tcp.port")){ socketChannel.pipeline().addLast(new ByteArrayDecoder()); socketChannel.pipeline().addLast(new ByteArrayEncoder()); socketChannel.pipeline().addLast(tcpServerHandler); //心跳包 socketChannel.pipeline().addLast(new IdleStateHandler(300,0,0, TimeUnit.SECONDS)); socketChannel.pipeline().addLast("hearbeata",tcpHeartBeatServerHandler); //心跳事件 }else{ } }
4.udp 处理方法 格式化协议处理
@Autowired private UdpChatServerHandler udpChatServerHandler; @Autowired private UdpHeartBeatServerHandler udpHeartBeatServerHandler; @Override protected void initChannel(NioDatagramChannel nioDatagramChannel) throws Exception { ChannelPipeline pipeline = nioDatagramChannel.pipeline(); pipeline.addLast("handler",udpChatServerHandler);//消息处理器 // pipeline.addLast("ackHandler", new UdpAckServerHandler());//ack处理器 pipeline.addLast("timeout", new IdleStateHandler(180, 0, 0, TimeUnit.SECONDS));// //此两项为添加心跳机制,60秒查看一次在线的客户端channel是否空闲 pipeline.addLast("hearbeat",udpHeartBeatServerHandler);// 心跳处理handler }
5.注意
框架采用netty5集成spring,mybatis在协议处理中需要写自己处理消息的方法处理等业务
相关文章推荐
- SOCKET,TCP/UDP,HTTP,FTP网络协议之间的关系
- 1小时教你理解HTTP,TCP,UDP,Socket,WebSocket
- TCP、UDP和HTTP
- libjingle源码解析(4)-【PseudoTcp】建立UDP之上的TCP(2):对交互数据流的处理
- Http TCP/IP UDP socket 解析
- Netty中处理TCP粘包和拆包
- TCP、UDP、HTTP、SOCKET介绍
- EasyPlayerPro Windows流媒体播放器(RTSP/RTMP/HTTP/HLS/File/TCP/RTP/UDP都能播)发布啦
- 常见通信协议区别——tcp、udp、rpc、http、socket
- 通信协议——Http、TCP、UDP
- HTTP TCP UDP
- HTTP、TCP、UDP、Socket
- linux C++ 同时监听多个端口 同时处理TCP和UDP服务请求的回射服务器
- tcp和udp包穿透防火墙-Httptunnel
- Nginx--官网中文翻译(中英文对比)--13-nginx如何处理会话How nginx processes a TCPUDP session
- TCP、UDP、HTTP、SOCKET之间的区别
- TCP、UDP、IP 、HTTP协议分析
- Netty处理HTTP之GET,POST请求
- 想知道为什么QQ传文件比MSN快吗?想知道TCP/IP、UDP、Http、Socket的区别吗?
- java netty使用DelimiterBasedFrameDecoder处理tcp粘包问题