ios App 发送广播失败解决

记录开发 ios App 使用 c++ 混编时遇到的问题:

开发环境

macOS Sonoma(最新版本14.3.1)

Xcode Version 15.2

ipadOS(最新版本17.3.1)

问题:在mac 上 和 ipad上测试,当 udp 发送广播,地址为 255.255.255.255 时 send失败,wireshark 抓包没有发送出去,改成单播地址就可以发成功。没更新ipad前 ios 15 上是可以发广播的,怀疑是升级到17后 安全性提高了?在网上搜说需要 申请组播广播权限,c++伪代码:

cpp 复制代码
 int port=0;
    WHUDP  *wh =new WHUDP();
    wh->ServerInit("0.0.0.0",  port);
    whls.push_back(wh);

    //发送
    for(int i=0;i<whls.size();i++){
        whls[i]->sendcmd("255.255.255.255", serverport,cmd);
    }



void WHUDP::ServerInit(const char *loaclip,int localport) //用于广播
{
    CreateSocket();
    uport = localport;
    //设置非阻塞
    setnonblocking( usock ,1);
    if (setbroadcast( usock ,1)<0) //if (::setsockopt( usock , SOL_SOCKET, SO_BROADCAST, (char FAR*)&opt, sizeof(opt)) <0)
    {
        debug_printf("setsockopt SO_BROADCAST failed ! [%ld]\n", GetLastErrorCode());
        ShutAndClose( usock );
        return  ;
    }

    if (setreuseaddr( usock ,1)<0) //if (::setsockopt( usock , SOL_SOCKET, SO_REUSEADDR, (char FAR*)&opt1, sizeof(opt1)) <0)
    {
        debug_printf("setsockopt SO_REUSEADDR failed ! [%ld]\n", GetLastErrorCode());
        ShutAndClose( usock );
        return  ;
    }
     if (false == BindAddrPort( loaclip , uport))
     {
         //cout << "bind error :" << GetLastError() <<endl;
         ShutAndClose(  usock );
     }
}


void WHUDP::sendcmd(const char *ip, unsigned short remoteport,char * cmd)
{
    char  szMsg[1024]={0};
    strcpy(szMsg,  cmd);

    //int nRet = ::sendto( usock , szMsg, 50, 0, (sockaddr*)&m_toAddr, sizeof(m_toAddr));
    int nRet =Send(szMsg,50,"255.255.255.255",remoteport);
    if (nRet <=0)
    {
        debug_printf("sendcmd sendto() failed ! [%ld]\n", GetLastErrorCode());

    }
    debug_printf("ok...\n");
}

解决:根据下面网站填写申请

https://developer.apple.com/contact/request/networking-multicast

等待邮件回复,通过后登录苹果开发者中心 查看证书修改,之前我没有申请过 证书就没有 Additional Capabilities 没有这一项,xcode 也没有 Multicast Networking ,现在有了 需要证书 和 xcode配置一下,广播地址255.255.255.255就可以发送成功了

证书勾选保存

xcode

相关推荐
Digitally6 小时前
如何将大型音频文件从 iPhone 发送到不同的设备
ios·iphone
吴Wu涛涛涛涛涛Tao11 小时前
Flutter 实现「可拖拽评论面板 + 回复输入框 + @高亮」的完整方案
android·flutter·ios
搜狐技术产品小编202316 小时前
CAEmitterLayer:iOS 中创建炫酷粒子效果的魔法工具
macos·ios·objective-c·cocoa
00后程序员张1 天前
iOS App 混淆与资源保护:iOS配置文件加密、ipa文件安全、代码与多媒体资源防护全流程指南
android·安全·ios·小程序·uni-app·cocoa·iphone
咕噜签名分发冰淇淋2 天前
内测分发是什么?
ios
2501_916007472 天前
Transporter App 使用全流程详解:iOS 应用 ipa 上传工具、 uni-app 应用发布指南
android·ios·小程序·https·uni-app·iphone·webview
白玉cfc2 天前
【OC】单例模式
开发语言·ios·单例模式·objective-c
Digitally2 天前
比较 iPhone:全面比较 iPhone 17 系列
android·ios·iphone
2501_915909062 天前
HTTPS 错误解析,常见 HTTPS 抓包失败、443 端口错误与 iOS 抓包调试全攻略
android·网络协议·ios·小程序·https·uni-app·iphone