iPhone SDK reachabilityWithAddress问题 [英] iPhone SDK reachabilityWithAddress issue
问题描述
我只需要检查与IP地址的连接。我正在尝试使用reachabilityWithAddress选项使用Apple Reachability类。我的问题是我可以在callAddress.sin_addr.s_addr字段中放置任何IP地址,而statusText始终是可伸缩的。如何准确检查与IP地址的连接?
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];
}
推荐答案
而不是使用Reachability类,去老派并打开一个TCP连接。如果对 connect()
的响应是 EHOSTUNREACH
,那么你不能。如果响应是 ECONNREFUSED
,或者连接成功,那么你可以。
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.
这篇关于iPhone SDK reachabilityWithAddress问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!