0

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");
}
Community
  • 1
  • 1
Wayne
  • 1
  • 1

2 Answers2

1

Add Reachability Class to your project.

 #import "Reachability.h"

also add SystemConfiguration framework.

Reachability *reachability = [Reachability reachabilityWithHostName:@"www.example.com"];
NetworkStatus reachabilitytoHost = [reachability currentReachabilityStatus];
if(reachabilitytoHost != NotReachable)
{
    //reachable
}
else
{
    // not reachable
}

Check sample code here: https://developer.apple.com/library/ios/samplecode/Reachability/Introduction/Intro.html

For more info: https://developer.apple.com/library/ios/samplecode/Reachability/Listings/Reachability_Reachability_h.html

Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51
  • I tried using the Reachability class until I realised that as I understand, it's not exactly what I'm looking for... "Note: Reachability cannot tell your application if you can connect to a particular host, only that an interface is available that might allow a connection" So basically, Reachability won't tell me if there is a host at the specified address, just that there is a potentional route to that host if it exists. I'm pretty sure I found this to the case when I tried it. Reachability was telling me the host was reachable even though there was no machine at the specified address. – Wayne Apr 22 '16 at 15:28
0

Take a look at the Reachability class by Tony Million: https://github.com/tonymillion/Reachability

From the Read Me:

    // Allocate a reachability object
Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];

// Set the blocks
reach.reachableBlock = ^(Reachability*reach)
{
    // keep in mind this is called on a background thread
    // and if you are updating the UI it needs to happen
    // on the main thread, like this:

    dispatch_async(dispatch_get_main_queue(), ^{
      NSLog(@"REACHABLE!");
    });
};

reach.unreachableBlock = ^(Reachability*reach)
{
    NSLog(@"UNREACHABLE!");
};

// Start the notifier, which will cause the reachability object to retain itself!
[reach startNotifier];

Where you could obviously replace the www.google.com with whatever address you would like to test.

trdavidson
  • 1,051
  • 12
  • 25
  • I tried using the Reachability class until I realised that as I understand, it's not exactly what I'm looking for... "Note: Reachability cannot tell your application if you can connect to a particular host, only that an interface is available that might allow a connection" So basically, Reachability won't tell me if there is a host at the specified address, just that there is a potentional route to that host if it exists. I'm pretty sure I found this to the case when I tried it. Reachability was telling me the host was reachable even though there was no machine at the specified address. – Wayne Apr 22 '16 at 15:33
  • Uhm, ok, well in that case have you tried simply using the WKWebView class and try to open the URL you are looking for? Use the delegates to see if it gives you an error response or not. – trdavidson Apr 22 '16 at 20:02