iPhone UDP 广播和响应

iPhone UDP broadcast and response(iPhone UDP 广播和响应)

本文介绍了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 这是比苹果原生类更容易使用.
它支持带有 AsyncUdpSocket 类的 UDP.

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 广播和响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:iPhone UDP 广播和响应

基础教程推荐