iPhone SDK reachabilityWithAddress问题

时间:2010-11-01 16:15:01

标签: iphone sdk reachability

我只需要检查与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];
 }

1 个答案:

答案 0 :(得分:4)

不使用Reachability类,而是使用old-school并打开TCP连接。如果对connect()的回复是EHOSTUNREACH,那么您就不能。如果响应为ECONNREFUSED或成功连接,则可以。