【问题标题】:iPhone SDK reachabilityWithAddress issueiPhone SDK 可达性WithAddress 问题
【发布时间】:2010-11-01 16:15:00
【问题描述】:

我只需要检查与 IP 地址的连接。我正在尝试使用reachabilityWithAddress 选项使用Apple Reachability 类来做到这一点。我的问题是我可以在 callAddress.sin_addr.s_addr 字段中输入任何 IP 地址,并且 statusText 总是“可达”。如何准确检查与 IP 地址的连接?

-(BOOL)reachabilityTest {

 struct sockaddr_in callAddress;
 callAddress.sin_len = sizeof(callAddress);
 callAddress.sin_family = AF_INET;
 callAddress.sin_port = htons(24);
 callAddress.sin_addr.s_addr = inet_addr("212.83.3.190");
 Reachability *hostReach = [[Reachability reachabilityWithAddress:&callAddress] retain];

 NetworkStatus netStatus = [hostReach currentReachabilityStatus];

 if (netStatus == NotReachable)
 {
  NSLog(@"NotReachable");
  statusField.text = @"NOT reachable";
  return NO;  
 }

 if (netStatus == ReachableViaWiFi)
 {
  NSLog(@"ReachableViaWiFi");
  statusField.text = @"reachable";
  return YES;
 } 
 [Reachability release];
 }

【问题讨论】:

    标签: iphone sdk reachability


    【解决方案1】:

    不要使用 Reachability 类,而是使用老式的方法并打开一个 TCP 连接。如果对connect() 的响应是EHOSTUNREACH,那么你不能。如果响应是ECONNREFUSED,或者连接成功,那么就可以了。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    • 2011-05-14
    相关资源
    最近更新 更多