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!
I'd put 'recvfrom' on another thread using grand central dispatch, like this:
You can use cocoaAsyncSocket which is easier to use than apple native classes.
It support UDP with AsyncUdpSocket class.