这里需要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();
}
}


可以使用工具测试:

Netty创建upd服务端

查看eclipse中udp服务端数据:

Netty创建upd服务端

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

相关文章:

  • 2021-11-28
  • 2021-12-20
  • 2021-09-17
  • 2021-05-16
  • 2021-07-25
  • 2022-01-19
  • 2021-04-19
  • 2022-03-01
猜你喜欢
  • 2021-05-30
  • 2021-12-17
  • 2021-05-01
  • 2022-12-23
  • 2021-10-12
  • 2021-11-17
  • 2021-10-08
相关资源
相似解决方案