Basically I would like to be able to check whether a particular host on my local network is "up".
The following line of code hangs when the host is unreachable, so I'd like to perform a check first before running it.
[_outputStream write:[data bytes] maxLength:[data length]];
A similar query I think is answered in the following link, but I think I need to use CFHostCreateWithAddress
rather than CFHostCreateWithName
Alternatives to NSHost in iPhone app
Here is my attempt at what I'm trying to do...
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");
}
Even when the host is up I get Not resolved.
Below is my attempt to use the Reachability class. My code is telling me the below address is reachable despite there being no host at the specified address.
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");
}