Search code examples
iphonesdkreachability

iPhone SDK reachabilityWithAddress issue


I need to check connectivity with IP address only. I'm trying to do it with Apple Reachability class, using reachabilityWithAddress option. And my problem is I can put any IP address in callAddress.sin_addr.s_addr field and statusText always will be "reachble". How can I do to exactly check connectivity to IP address?

-(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];
 }

Solution

  • Instead of using the Reachability classes, go old-school and open a TCP connection to it. If the response to connect() is EHOSTUNREACH, then you can't. If the response is ECONNREFUSED, or a successful connection, then you can.