用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 }

 






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM