有时我会收到此错误
错误域=NSPOSIXErrorDomain 代码=65 "没有到主机的路由" UserInfo={NSLocalizedDescription=没有到主机的路由, NSLocalizedFailureReason=send() 函数中的错误。}
在某些路由器中打开 udp 套接字时。我不知道是什么原因造成的。
这是我打开 udp 套接字的代码:
- (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];
}
}
请注意,这在 iPhone 7 中经常发生