3

Sometimes I am getting this error

Error Domain=NSPOSIXErrorDomain Code=65 "No route to host" UserInfo={NSLocalizedDescription=No route to host, NSLocalizedFailureReason=Error in send() function.}

when opening udp socket in some routers. I don't know what causes this.

This is my code for opening a udp socket:

- (void)openSocket
{
    if(_asyncUdpSocket == nil)
    {
        _asyncUdpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
    }

    if(![_asyncUdpSocket isClosed])
    {
        return;
    }

    NSError *error;

    if (![_asyncUdpSocket enableBroadcast:YES error:&error])
    {
        NSLog(@"AsyncUdp: enable broadcast failed error: %@", error);
    }

    if (![_asyncUdpSocket bindToPort:_port error:&error])
    {
        NSLog(@"AsyncUdp: bind to port %li failed error: %@", (unsigned long)_port, error);
    }

    if (![_asyncUdpSocket joinMulticastGroup:_address error:&error])
    {
        NSLog(@"AsyncUdp: join multicast group %@ failed error: %@", _address, error);
    }

    if(![_asyncUdpSocket beginReceiving:&error])
    {
        NSLog(@"AsynUdp: begin receiving error: %@", error);
    }

    if(error != nil)
    {
        NSLog(@"Udp failed to open!");

        [_delegate onUdpSocketDidOpenfailed];
    }

    else
    {
        NSLog(@"UdpSocket - opend udp socket successful!");

        [_delegate onUdpSocketDidOpen];
    }
}

Take note that this happens really often in iPhone 7

JLT
  • 3,052
  • 9
  • 39
  • 86

1 Answers1

0

After updating Xcode to Version 12.5, I got the same issue when run a DLNA App project. I fixed it by downgrading Xcode to Version 12.4.

Michael
  • 415
  • 6
  • 16