Netty创建upd服务端

xiaoxiao2021-02-28  106

这里需要netty的jar包文件,我已经上传到百度云,提供下载:

这里使用的是netty5

http://pan.baidu.com/s/1hr2VBzY

服务端:

package com.use.socket.udp.Server;

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 UdpServer { // 相比于TCP而言,UDP不存在客户端和服务端的实际链接,因此     // 不需要为连接(ChannelPipeline)设置handler     public void run(int port)throws Exception{         EventLoopGroup group = new NioEventLoopGroup();         try {             Bootstrap b = new Bootstrap();             b.group(group).channel(NioDatagramChannel.class)                     .option(ChannelOption.SO_BROADCAST,true)                     .handler(new UdpServerHandler());             b.bind(port).sync().channel().closeFuture().await();             System.out.println("启动成功");         }         finally {             group.shutdownGracefully();         }     }     public static void main(String[] args) throws Exception{         int port = 8080;         if(args.length>0){             try{                 port = Integer.parseInt(args[0]);             }             catch (NumberFormatException e){                 e.printStackTrace();             }         }         new UdpServer().run(port);     }

}

package com.use.socket.udp.Server; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.socket.DatagramPacket; import io.netty.util.CharsetUtil; import io.netty.util.internal.ThreadLocalRandom; public class UdpServerHandler extends SimpleChannelInboundHandler<DatagramPacket>{ private static final  String[] DICTIONARY={             "hello world",             "C++",             "C#",             "Java",             "python"     };     private String nextQuoto(){         // 线程安全的随机类:ThreadLocalRandom         int quoteId = ThreadLocalRandom.current().nextInt(DICTIONARY.length);         return DICTIONARY[quoteId];     }     @Override     public void messageReceived(ChannelHandlerContext channelHandlerContext,                                    DatagramPacket datagramPacket) throws Exception {         // 因为Netty对UDP进行了封装,所以接收到的是DatagramPacket对象。         String req = datagramPacket.content().toString(CharsetUtil.UTF_8);         System.out.println("udp service收到"+req);        /* if("aaa".equals(req)){             channelHandlerContext.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer(                     "resourlt:"+nextQuoto(),CharsetUtil.UTF_8),datagramPacket.sender()));         }else{         channelHandlerContext.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer(                      "resourlt:"+nextQuoto(),CharsetUtil.UTF_8),datagramPacket.sender()));         }*/     }     @Override     public void exceptionCaught(ChannelHandlerContext ctx,Throwable cause)throws Exception{         ctx.close();         cause.printStackTrace(); } }

可以使用工具测试:

查看eclipse中udp服务端数据:

可以接收到,自此,netty的udp服务端执行完毕

转载请注明原文地址: https://www.6miu.com/read-69334.html

最新回复(0)