我需要从 iPhone 发出 UDP 广播,然后监听具有超时期限的 UDP 响应。我找到了苹果的UDP回显示例 http://developer.apple.com/library/mac/#samplecode/UDPEcho/Introduction/Intro.html但我不确定这是否是我所需要的。还发现这个例子 http://splinter.com.au/sending-a-udp-broadcast-packet-in-c-objective发送但不接收。基本上,我需要做一些简单的事情,如下所示:
//send the broadcast
SendUDP("255.255.255.255", targetPort, myData);
//A blocking call to get the data. Timeout value will be short, 2 seconds at most
//An asynchronous option is ok, if it's necessary.
Response = GetFirstUDPResponse(receptionPort, timeoutValue);
//process the response
if(Response == null)
//we timed out
else
//process response
我希望有一个简单的解决方案,让我不必重新发明轮子。我很感激任何关于实施这一最佳策略的建议!
您可以使用可可AsyncSocket https://github.com/robbiehanson/CocoaAsyncSocket这比苹果原生类更容易使用。
它通过 AsyncUdpSocket 类支持 UDP。
AsyncUdpSocket 是一个 UDP/IP 套接字网络库,它包装
CFSocket。它的工作方式几乎与 TCP 版本完全相同,但是
专为 UDP 设计。这包括排队非阻塞
发送/接收操作,完整的委托支持,基于运行循环,
自包含类,并支持 IPv4 和 IPv6
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)