博客
关于我
LINUX编程实战指发送UDP消息
阅读量:796 次
发布时间:2023-02-05

本文共 823 字,大约阅读时间需要 2 分钟。

调试媒体服务器遇到被叫端媒体流抖动问题,需先从网络问题与服务器代码两个方面排查。为便于调试,我编写了一个UDP发送的小工具,支持根据指定IP、端口和发送时长,按照20ms间隔重复发送RTP流。

编程思路

  • 初始化本端UDP套接字:首先创建本地UDP套接字。
  • 设置非阻塞模式:使用fcntl函数设置O_NDELAY选项,确保发送操作不会阻塞。
  • socket选项设置:使用setsockopt函数设置SO_REUSEADDR选项,允许立即重复使用本地端口。
  • 发送缓冲区管理:定义发送缓冲区大小和长度,为后续数据发送做准备。
  • 以下是具体实现代码片段:

    memset(buf, 1, sizeof(buf));char *desip = argv[1];int destport = atoi(argv[2]);int sec = atoi(argv[3]);int count = sec * 1000 / 20;int fd = -1;fd = socket(AF_INET, SOCK_DGRAM, 0);fcntl(fd, F_SETFL, O_NDELAY);int reuse_addr_flag = 1;int success = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse_addr_flag, sizeof(reuse_addr_flag));if (success < 0) {    perror("udp setsockopt(SO_REUSEADDR): ");    close(fd);    return 0;}int sendBufSize, sendBufLength;sendBufSize = 4;

    以上代码实现了UDP套接字的创建与配置,确保了发送操作的高效性。通过设置SO_REUSEADDR选项,可以避免由于本地端口未释放而导致的发送失败。

    转载地址:http://llkfk.baihongyu.com/

    你可能感兴趣的文章
    npm学习(十一)之package-lock.json
    查看>>
    npm安装 出现 npm ERR! code ETIMEDOUT npm ERR! syscall connect npm ERR! errno ETIMEDOUT npm ERR! 解决方法
    查看>>
    npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
    查看>>
    npm安装教程
    查看>>
    npm报错Cannot find module ‘webpack‘ Require stack
    查看>>
    npm报错Failed at the node-sass@4.14.1 postinstall script
    查看>>
    npm报错fatal: Could not read from remote repository
    查看>>
    npm报错File to import not found or unreadable: @/assets/styles/global.scss.
    查看>>
    npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
    查看>>
    npm版本过高问题
    查看>>
    npm的“--force“和“--legacy-peer-deps“参数
    查看>>
    npm的安装和更新---npm工作笔记002
    查看>>
    npm的常用配置项---npm工作笔记004
    查看>>
    npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
    查看>>
    npm编译报错You may need an additional loader to handle the result of these loaders
    查看>>
    npm设置淘宝镜像、升级等
    查看>>
    npm设置源地址,npm官方地址
    查看>>
    npm配置安装最新淘宝镜像,旧镜像会errror
    查看>>
    NPM酷库052:sax,按流解析XML
    查看>>
    npm错误 gyp错误 vs版本不对 msvs_version不兼容
    查看>>