iPhone UDP broadcast and response

前端 未结 2 1469
自闭症患者
自闭症患者 2021-02-04 15:12

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 wh

2条回答
  •  我在风中等你
    2021-02-04 16:13

    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];
            }
    
        });
    });
    

提交回复
热议问题