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

前端 未结 7 716
一向
一向 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

    0 讨论(0)
提交回复
热议问题