在基于本地Mac的IPv6 DNS64 / NAT64网络中检查可达性为0.0.0.0时,不会发生错误

时间:2016-05-11 06:03:48

标签: ios ipv6

在本地基于Mac的IPv6 DNS64 / NAT64网络中检查到达0.0.0.0的可达性时,请按照以下文章your_app_and_next_generation_networks,在 What Break 段中发生错误(You&# 39;没有连接到互联网)

但是,在创建基于本地Mac的IPv6 DNS64 / NAT64网络之后,我尝试通过以下代码检查到达0.0.0.0的可达性:

Reachability *reachability = [Reachability reachabilityForInternetConnection]; [reachability startNotifier];

然后没有显示错误!

这里是来自Reachability Github

reachabilityForInternetConnection方法的实现
+(instancetype)reachabilityForInternetConnection {
  struct sockaddr_in zeroAddress;
  bzero(&zeroAddress, sizeof(zeroAddress));
  zeroAddress.sin_len = sizeof(zeroAddress);
  zeroAddress.sin_family = AF_INET;

  return [self reachabilityWithAddress:&zeroAddress];
}

+(instancetype)reachabilityWithAddress:(void *)hostAddress {
   SCNetworkReachabilityRef ref = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr*)hostAddress);

   if (ref) 
    {
     id reachability = [[self alloc] initWithReachabilityRef:ref];
     return reachability;
    }

   return nil;
}

1 个答案:

答案 0 :(得分:1)

如下面的代码所示,如​​果我们使用有效的全局IP,它就不起作用。

struct sockaddr_in addr;
addr.sin_len = INET_ADDRSTRLEN;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("XXX.XXX.XXX.XXX"); // valid global ip instead of 0.0.0.0
Reachability* reachability = [Reachability reachabilityWithAddress:&addr];
[reachability startNotifier];
NSLog(@"reachable[%d]", reachability.currentReachabilityStatus); // when ipv6, it isNotReachable and when ipv4, it is ReachableViaWiFi