iPhone UDP广播和响应

发布于 2024-12-07 10:50:52 字数 763 浏览 1 评论 0原文

我需要从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一笑百媚生 2024-12-14 10:50:52

您可以使用 cocoaAsyncSocket ,它比苹果原生类更容易使用。
它通过 AsyncUdpSocket 类支持 UDP。

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

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

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

浪漫之都 2024-12-14 10:50:52

我会使用中央调度将“recvfrom”放在另一个线程上,如下所示:

// Use grand central dispatch so we don't block the interface
dispatch_async(dispatch_get_global_queue(0, 0), ^{

    recvfrom(...) // Receive with a 2s timeout

    dispatch_async(dispatch_get_main_queue(), ^{ // The main thread stuff goes here

        if (received ok) {
            [self receivedData:some data];
        } else {
            [self timedOut];
        }

    });
});

I'd put 'recvfrom' on another thread using grand central dispatch, like this:

// Use grand central dispatch so we don't block the interface
dispatch_async(dispatch_get_global_queue(0, 0), ^{

    recvfrom(...) // Receive with a 2s timeout

    dispatch_async(dispatch_get_main_queue(), ^{ // The main thread stuff goes here

        if (received ok) {
            [self receivedData:some data];
        } else {
            [self timedOut];
        }

    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文