用netty的udp方式做单播或广播

业务逻辑:接收上端端口发送的音频流,然后用UDP方式以单播的方式发送给指定的机器,因为用的是netty框架在百度上找了很多都关于UDP方法都是客户端发送,服务端收后处理再返回给客户端根本没办实现我想要的方法,最后根据别人的思路自己做了一些修改。

UDPServer 服务端

 1 package com.jetosend.escb.common.netty;
 2 import io.netty.bootstrap.Bootstrap;
 3 import io.netty.channel.ChannelOption;
 4 import io.netty.channel.EventLoopGroup;
 5 import io.netty.channel.nio.NioEventLoopGroup;
 6 import io.netty.channel.socket.nio.NioDatagramChannel;
 7 import lombok.extern.log4j.Log4j2;
 8 import org.springframework.stereotype.Component;
 9 
10 @Component
11 @Log4j2
12 public class UdpServer {
13     public void start() throws Exception{
14         EventLoopGroup group = new NioEventLoopGroup();
15         Bootstrap b = new Bootstrap();
16         //由于我们用的是UDP协议,所以要用NioDatagramChannel来创建
17         b.group(group).channel(NioDatagramChannel.class)
18                 .option(ChannelOption.SO_BROADCAST, true)//支持广播
19                 .handler(new UdpServerHandler());//ChineseProverbServerHandler是业务处理类
20         b.bind(5000).sync().channel().closeFuture().await();
21         log.info(port);
22     }
23 }

UdpServerHandler 服务端处理程序
我的处理方法是在服务端接收到音频流后直接用客户端的程序做处理,具体方法:
 1 package com.jetosend.escb.common.netty;
 2 import io.netty.bootstrap.Bootstrap;
 3 import io.netty.buffer.ByteBuf;
 4 import io.netty.buffer.Unpooled;
 5 import io.netty.channel.*;
 6 import io.netty.channel.nio.NioEventLoopGroup;
 7 import io.netty.channel.socket.DatagramPacket;
 8 import io.netty.channel.socket.nio.NioDatagramChannel;
 9 import io.netty.util.ReferenceCountUtil;
10 import java.net.InetSocketAddress;
11 
12 public class UdpServerHandler extends SimpleChannelInboundHandler<DatagramPacket> {
13 
14     public static Channel[] ch=new Channel[252];
15    public static int first;
16     @Override
17     protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception
18     {
19         if(first==0) {
20             first=1;
21             EventLoopGroup group = new NioEventLoopGroup();
22             Bootstrap b = new Bootstrap();
23             b.group(group).channel(NioDatagramChannel.class)
24                     .option(ChannelOption.SO_BROADCAST, true)
25                     .handler(new UdpClientHandler());
26             for(int i=0; i<252; i++ ) ch[i] = b.bind(0).sync().channel();
27             // 向网段内所有机器广播发UDP
28         }
29         ByteBuf buf = (ByteBuf) packet.copy().content();
30         //广播方式发送
31         ch[0].writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer(buf), new InetSocketAddress("255.255.255.255", 4000))).sync();
32         //单播方式发送
33         /*
34         for(int i=2; i<254; i++)
35         {
36             String ip="192.168.1."+i;
37             ch[i-2].writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer(buf), new InetSocketAddress(ip, port))).sync();
38         }
39          */
40         ReferenceCountUtil.release(buf);
41     }
42     @Override
43     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
44             throws Exception {
45         ctx.close();
46         cause.printStackTrace();
47     }
48 }




原文地址:https://www.cnblogs.com/broud/p/12658145.html