iPhone UDP广播和响应
我需要从 iPhone 发出 UDP 广播,然后监听具有超时期限的 UDP 响应。我找到了 Apple 的 UDPEcho 示例,但我没有确定这是否是我所需要的。还发现这个示例发送但不接收。基本上,我需要做这样简单的事情:
//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
我希望有一个简单的解决方案,我不必重新发明轮子。我很感激任何关于实施这一最佳策略的建议!
I need to send out a UDP broadcast from an iPhone, and then listen for a UDP response with a timeout period. I have found Apple's UDPEcho example but I am not sure if it's what I need. Also found this example to send but not receive. Basically, I need to do something simple like this:
//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
I'm hoping for a simple solution where I don't have to reinvent the wheel. I appreciate any advice on the best strategy to implement this!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 cocoaAsyncSocket ,它比苹果原生类更容易使用。
它通过 AsyncUdpSocket 类支持 UDP。
You can use cocoaAsyncSocket which is easier to use than apple native classes.
It support UDP with AsyncUdpSocket class.
我会使用中央调度将“recvfrom”放在另一个线程上,如下所示:
I'd put 'recvfrom' on another thread using grand central dispatch, like this: