zoukankan      html  css  js  c++  java
  • 用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 }




  • 相关阅读:
    restful架构风格设计准则(四)资源表示和资源访问
    洛谷P2178 [NOI2015]品酒大会(后缀自动机 线段树)
    HDU 6138 Fleet of the Eternal Throne(后缀自动机)
    BZOJ1278: 向量vector(计算几何 随机化乱搞)
    BZOJ2564: 集合的面积(闵可夫斯基和 凸包)
    POJ 1113 Wall(思维 计算几何 数学)
    POJ 3304 Segments(直线与线段相交)
    洛谷P1742 最小圆覆盖(计算几何)
    洛谷P4555 [国家集训队]最长双回文串(manacher 线段树)
    洛谷P3193 [HNOI2008]GT考试(dp 矩阵乘法)
  • 原文地址:https://www.cnblogs.com/broud/p/12658145.html
Copyright © 2011-2022 走看看