25

I want to Ping or Lookup a particular IP address of server if it is connected or available at that particular time or not in my iPhone application.

I searched a lot over internet but could not find any relevant result for same.

Please help me as I am new to this field.

Thanks in advance.

P.J
  • 6,547
  • 9
  • 44
  • 74

3 Answers3

28

Apple provides reachability classes you can use the class and

You can download reachability.h and Reachability.m

And use this as

Reachability* reachability = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
NetworkStatus netStatus = [reachability currentReachabilityStatus];

netStatus gives the reachabilty of the server.

nswamy
  • 1,041
  • 9
  • 16
  • change @"www.apple.com" to ip , say @"12.1.12.1" – nswamy Nov 15 '11 at 12:41
  • also change to `reachabilityWithAddress` – nswamy Nov 15 '11 at 12:45
  • Thanks for your reply... It requires (const struct sockaddr_in *) as parameter. Can you please tell me how can I convert IP Address string to sockaddr_in. Thanks a lot.. – P.J Nov 15 '11 at 12:49
  • 3
    this should help you `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("12.1.12.1");` – nswamy Nov 15 '11 at 16:21
  • FYI - Reachability.m / .h can now be found here ( above links broken ): https://developer.apple.com/library/ios/samplecode/Reachability/Listings/Reachability_Reachability_m.html#//apple_ref/doc/uid/DTS40007324-Reachability_Reachability_m-DontLinkElementID_9 – sean2078 Aug 28 '16 at 01:46
  • 2
    From the document: A remote host is considered reachable when a data packet, sent by an application into the network stack, can leave the local device. Reachability does not guarantee that the data packet will actually be received by the host. So not really. – funct7 Jan 17 '17 at 00:35
  • The reachability for host provided by Apple is a bit of a joke. The most charitable I can be is "it's horribly misnamed". – occulus Aug 18 '17 at 10:58
17

If you want to ping the server, try this:

SimplePing

It allows for you to ping by either hostname or IP.

driedler
  • 3,750
  • 33
  • 26
  • 1
    It doesn't work on iOS. `- (void)simplePing:(SimplePing *)pinger didReceivePingResponsePacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber` never gets called, even after adding `NSAppTransportSecurityNSAllowsArbitraryLoads` to Info.plist. – Jaybo Mar 08 '17 at 08:54
1

You need Apple's Reachability Sample Application.

Manali
  • 577
  • 4
  • 11
  • 2
    Thanks for your reply. But code for Reachability checks for if Internet connection/WIFI is available or not, but there is no option for checking if server with particular IP Address which does not have host name is available or not. Please help..... – P.J Nov 15 '11 at 07:35
  • No...not necessarily. That's part of what it does. – 0xSina Nov 15 '11 at 07:36
  • It doesn't works it we put IP Address as it asks for host name not IP Address – P.J Nov 15 '11 at 09:12