【发布时间】:2016-04-21 21:17:48
【问题描述】:
基本上我希望能够检查我本地网络上的特定主机是否“启动”。
当主机无法访问时,下面这行代码会挂起,所以我想在运行之前先进行检查。
[_outputStream write:[data bytes] maxLength:[data length]];
我认为在以下链接中回答了类似的查询,但我认为我需要使用CFHostCreateWithAddress 而不是CFHostCreateWithName
Alternatives to NSHost in iPhone app
这是我正在尝试做的事情......
Boolean result;
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_port = htons(80);
inet_pton(AF_INET, "192.168.1.31", &address.sin_addr);
CFDataRef sockData = CFDataCreate(NULL, &address, sizeof(address));
CFHostRef host = CFHostCreateWithAddress(NULL, sockData);
result = CFHostStartInfoResolution(host, kCFHostAddresses, NULL);
if (result == TRUE) {
NSLog(@"Resolved");
} else {
NSLog(@"Not resolved");
}
即使主机启动,我也得到 Not resolve。
下面是我尝试使用 Reachability 类。我的代码告诉我,尽管指定地址没有主机,但可以访问以下地址。
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_port = htons(80);
inet_pton(AF_INET, "192.168.1.31", &address.sin_addr);
Reachability *reachability = [Reachability reachabilityWithAddress:&address];
NetworkStatus reachabilitytoHost = [reachability currentReachabilityStatus];
if(reachabilitytoHost != NotReachable)
{
NSLog(@"Reachable");
}
else
{
NSLog(@"Not Reachable");
}
【问题讨论】:
标签: ios objective-c