How to write a simple Ping method in Cocoa/Objective-C

前端 未结 7 747
一向
一向 2020-11-29 00:52

I need to write a simple ping method in Cocoa/Objective-C. It also needs to work on the iPhone.

I found an example that uses icmp, will thi

7条回答
  •  情深已故
    2020-11-29 01:36

    The code below seems to be working synchronously:

    const char *hostName = [@"stackoverflow.com"
                            cStringUsingEncoding:NSASCIIStringEncoding];
    SCNetworkConnectionFlags flags = 0;
    if (SCNetworkCheckReachabilityByName(hostName, &flags) && flags > 0) {
      NSLog(@"Host is reachable: %d", flags);
    }
    else {
      NSLog(@"Host is unreachable");
    }
    

    Note: SystemConfiguration.framework is required

提交回复
热议问题