iPhone UDP广播和响应 [英] iPhone UDP broadcast and response

查看:177
本文介绍了iPhone UDP广播和响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从iPhone发送UDP广播,然后在超时期限内侦听UDP响应。我找到了Apple的 UDPEcho示例,但我不是确定这是否是我需要的。还发现此示例发送但未收到。基本上,我需要做这样简单的事情:

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!

推荐答案

您可以使用 cocoaAsyncSocket 比苹果本机类更容易使用。

它支持UDP与AsyncUdpSocket类。

You can use cocoaAsyncSocket which is easier to use than apple native classes.
It support UDP with AsyncUdpSocket class.


AsyncUdpSocket是一个UDP / IP套接字网络库,包装
CFSocket。它几乎与TCP版本完全相同,但是专为UDP设计的
。这包括排队的非阻塞
发送/接收操作,完全委托支持,基于运行循环,
自包含类,以及对IPv4和IPv6的支持

AsyncUdpSocket is a UDP/IP socket networking library that wraps CFSocket. It works almost exactly like the TCP version, but is designed specifically for UDP. This includes queued non-blocking send/receive operations, full delegate support, run-loop based, self-contained class, and support for IPv4 and IPv6

这篇关于iPhone UDP广播和响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆