iPhone SDK reachabilityWithAddress issue

若如初见. 提交于 2019-11-28 05:06:09

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/4070754/iphone-sdk-reachabilitywithaddress-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!