我只需要检查IP地址的连通性。我正在尝试使用reachabilityWithAddress选项对Apple Reachability类进行此操作。我的问题是我可以将任何IP地址放在callAddress.sin_addr.s_addr字段中,statusText始终为“ reachble”。如何正确检查与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];
}
最佳答案
不要使用“可达性”类,而去老式并打开与之的TCP连接。如果对connect()
的响应为EHOSTUNREACH
,则不能。如果响应为ECONNREFUSED
或成功连接,则可以。
关于iphone - iPhone SDK可达性WithAddress问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4070754/