3

I want to check a server is live or not with ip, for example, 74.125.71.104 (Google's ip)

// allocate a reachability object

`struct sockaddr_in address;
address.sin_len = sizeof(address);
address.sin_family = AF_INET;
address.sin_port = htons(80);
address.sin_addr.s_addr = inet_addr("74.125.71.104");`

Reachability *reach = [Reachability reachabilityWithAddress:&address];

but those not working.

When I change to reachabilityWithHostname, it's working.

subba raj
  • 37
  • 11

1 Answers1

3

Please import #include <arpa/inet.h>

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
struct sockaddr_in address;
address.sin_len = sizeof(address);
address.sin_family = AF_INET;
address.sin_port = htons(8080);
address.sin_addr.s_addr = inet_addr("216.58.199.174"); //google ip
self.internetReachability = [Reachability reachabilityWithAddress:&address];
[self.internetReachability startNotifier];
[self updateInterfaceWithReachability:self.internetReachability];

EDIT

As per your comments your reachability blocks are not called. I always use notifications not much aware of reachability blocks. So I prefer using notifications as follow.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
struct sockaddr_in address;
address.sin_len = sizeof(address);
address.sin_family = AF_INET;
address.sin_port = htons(8080);
address.sin_addr.s_addr = inet_addr("216.58.199.174");
self.internetReachability = [Reachability reachabilityWithAddress:&address];
[self.internetReachability startNotifier];
[self updateInterfaceWithReachability:self.internetReachability];

Now whenever your internet status changes reachabilityChanged method will be triggerred with reachability instance :)

- (void) reachabilityChanged:(NSNotification *)note {
    Reachability* curReach = [note object];
    [self updateInterfaceWithReachability:curReach];
}

Finally implement updateInterfaceWithReachability as

- (void)updateInterfaceWithReachability:(Reachability *)reachability {
   NetworkStatus netStatus = [reachability currentReachabilityStatus];
            switch (netStatus)
            {
                case NotReachable: {
                    //not reachable
                }
                break;
                case ReachableViaWWAN:
                case ReachableViaWiFi:        {
                    //reachable via either 3g or wifi
                }
                break;
            }
}

Hope this helps.

Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78
  • 1
    Just be aware that some mobile providers have done away with iPv4 and nat everything over IPv6 and then static IP addresses might not work. – rckoenes Jun 10 '16 at 08:01
  • @rckoenes : Bingo :) – Sandeep Bhandari Jun 10 '16 at 08:02
  • 1
    Reachability *internetReachableFoo= [Reachability reachabilityWithHostName:@"74.125.71.104"]; internetReachableFoo.reachableBlock = ^(Reachability*reach) { dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"Yayyy, we have the interwebs!"); }); }; internetReachableFoo.unreachableBlock = ^(Reachability*reach) { dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"Someone broke the internet :("); }); }; [internetReachableFoo startNotifier]; My blocks are never called – Sowjanya lankisetty Jun 10 '16 at 08:58
  • As you said you are using delegate pattern, should i conform to any protocol – Sowjanya lankisetty Jun 10 '16 at 09:13
  • @sowjanya-lankisetty : Sorry I used the wrong word delegate :) Its not a delegate pattern its notification pattern that am using :) If you see I have added a notification to kReachabilityChangedNotification. Whenever network status changes you will recieve the notification :) – Sandeep Bhandari Jun 10 '16 at 09:15
  • @sowjanya-lankisetty : Thanks for pointing out typo :) I have updated my answer :) – Sandeep Bhandari Jun 10 '16 at 09:16
  • Code flow is working, i added above snippet. But when i am checking for not reachable ip it is showing as it can be reachable, even for the ip which i cant ping from terminal. Check this with any ip that is not in your connectivity range. – Sowjanya lankisetty Jun 10 '16 at 09:22
  • on MAC Device.I am writing this for MAC Desktop app not for iOS – Sowjanya lankisetty Jun 10 '16 at 09:32
  • @sowjanya-lankisetty : I did little bit of search and figured out using reachabilityWithHostName gives wrong status for ip. Lemme update the working code with ip :) – Sandeep Bhandari Jun 10 '16 at 09:34
  • @sowjanya-lankisetty : Have a look at the updated code :) Tried pinging valid google ip got correct status when tried pinging not reachable ip gave status as not connected – Sandeep Bhandari Jun 10 '16 at 09:36
  • I am going to case ReachableViaWiFi: when i am giving not reachable ip. I followed your instructions as it is, dont know why this happening. – Sowjanya lankisetty Jun 10 '16 at 09:42
  • Could you please upvote my question @SandeepBhandari, so that we can continue discussion in chat – Sowjanya lankisetty Jun 10 '16 at 09:58
  • @sowjanya-lankisetty : I did – Sandeep Bhandari Jun 10 '16 at 10:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/114339/discussion-between-sowjanya-lankisetty-and-sandeep-bhandari). – Sowjanya lankisetty Jun 10 '16 at 11:00