How to write a simple Ping method in Cocoa/Objective-C
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: 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 this work on the iPhone? I'm leaning towards a solution using NSNetServices , is this a good idea? The method only needs to ping a few times and return the average and -1 if the host is down or unreachable. 回答1: The code below seems to be working synchronously: const char * hostName = [@ "stackoverflow.com" cStringUsingEncoding : NSASCIIStringEncoding ]; SCNetworkConnectionFlags flags = 0 ; if (