如何在 Cocoa/Objective-C 中编写一个简单的 Ping 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/798454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write a simple Ping method in Cocoa/Objective-C
提问by Richard Stelling
I need to write a simple pingmethod in Cocoa/Objective-C. It also needs to work on the iPhone.
我需要ping在 Cocoa/Objective-C 中编写一个简单的方法。它还需要在 iPhone 上运行。
I found an example that uses icmp, will this work on the iPhone?
我找到了一个使用 的示例,icmp这可以在 iPhone 上使用吗?
I'm leaning towards a solution using NSNetServices, is this a good idea?
我倾向于使用 的解决方案NSNetServices,这是个好主意吗?
The method only needs to pinga few times and return the average and -1 if the host is down or unreachable.
ping如果主机关闭或无法访问,该方法只需要几次并返回平均值和 -1。
采纳答案by Vadim
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.frameworkis required
注:SystemConfiguration.framework必填
回答by Gene Myers
Let me try this again...this time logging in, and formatting better ;-)
让我再试一次...这次登录,并更好地格式化;-)
StreamSCNetworkCheckReachabilityByName is deprecated and NOT available for the iPhone.
StreamSCNetworkCheckReachabilityByName 已弃用,不适用于 iPhone。
bool success = false;
const char *host_name = [@"stackoverflow.com"
cStringUsingEncoding:NSASCIIStringEncoding];
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL,
host_name);
SCNetworkReachabilityFlags flags;
success = SCNetworkReachabilityGetFlags(reachability, &flags);
//prevents memory leak per Carlos Guzman's comment
CFRelease(reachability);
bool isAvailable = success && (flags & kSCNetworkFlagsReachable) &&
!(flags & kSCNetworkFlagsConnectionRequired);
if (isAvailable) {
NSLog(@"Host is reachable: %d", flags);
}else{
NSLog(@"Host is unreachable");
}
Note: SystemConfiguration.framework is required
注意:需要 SystemConfiguration.framework
回答by Chris
I had this same problem, and ended up writing a simple wrapper around SimplePing to achieve this, wrote a blog about it and there's some code on github, hopefully will help someone here:
我遇到了同样的问题,最终围绕 SimplePing 编写了一个简单的包装器来实现这一点,写了一篇关于它的博客,并且在 github 上有一些代码,希望能帮助这里的人:
http://splinter.com.au/how-to-ping-a-server-in-objective-c-iphone
http://splinter.com.au/how-to-ping-a-server-in-objective-c-iphone
回答by Zhami
You are not missing anything -- "Reachability" doesn't actually test that the target domain is in fact reachable, it only assesses if there is a pathway out of the machine by which the target domain is potentially reachable. So long as you have some outbound connection (e.g., an active wirless or wired connection), and a routing configuration that leads to the target, then the site is "reachable" as far as SCNetworkReachability is concerned.
你没有遗漏任何东西——“可达性”实际上并没有测试目标域实际上是可访问的,它只是评估是否有一条离开机器的路径,目标域可能通过它来访问。只要您有一些出站连接(例如,活动的无线或有线连接)和通向目标的路由配置,那么就 SCNetworkReachability 而言,该站点是“可达的”。
回答by Monobono
Pinging on the iPhone works a bit different than on other platforms, due to the fact that you don't have root access. See this sample codefrom Apple.
由于您没有 root 访问权限,因此在 iPhone 上 Ping 的工作方式与在其他平台上略有不同。请参阅Apple 的此示例代码。
回答by Chris Bennet
The answer Gene Myers posted works using "SCNetworkReachabilityCreateWithName" for me - but only in the simulator. On my device (iPod w/OS 2.2.1) it always returns "Host is reachable" even for nonsense addresses like "zzz".
Gene Myers 为我发布的答案使用“SCNetworkReachabilityCreateWithName” - 但仅在模拟器中。在我的设备(带操作系统 2.2.1 的 iPod)上,即使对于“zzz”这样的无意义地址,它也总是返回“主机可访问”。
Am I misunderstanding something? Thanks.
我误解了什么吗?谢谢。
Here's my code just in case:
这是我的代码以防万一:
From How to write a simple Ping method in Cocoa/Objective-C
从如何在 Cocoa/Objective-C 中编写一个简单的 Ping 方法
- (IBAction) TestReachability:(id)sender
{
bool success = false;
const char *host_name = [ipAddressText.textcStringUsingEncoding:NSASCIIStringEncoding];
NSString *imageConnectionSuccess = @"Connected.png";
NSString *imageConnectionFailed = @"NotConnected.png";
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL,
host_name);
SCNetworkReachabilityFlags flags;
success = SCNetworkReachabilityGetFlags(reachability, &flags);
bool isAvailable = success && (flags & kSCNetworkFlagsReachable) &&
!(flags & kSCNetworkFlagsConnectionRequired);
if (isAvailable)
{
NSLog([NSString stringWithFormat: @"'%s' is reachable, flags: %x", host_name, flags]);
[imageView setImage: [UIImage imageNamed:imageConnectionSuccess]];
}
else
{
NSLog([NSString stringWithFormat: @"'%s' is not reachable", host_name]);
[imageView setImage: [UIImage imageNamed:imageConnectionFailed]];
}
}
回答by Jark
Please take note that there is an difference between the simulator and the actual iPhone. The simulator is not a true simulator like the one supplied by Android, it uses Mac OSX classes for most of the functions.
请注意,模拟器和实际 iPhone 之间存在差异。该模拟器不像 Android 提供的那样是真正的模拟器,它使用 Mac OSX 类来实现大部分功能。
This is particularly hell if there is a difference between the Mac OSX and iPhonew(for example the keychain).
如果 Mac OSX 和 iPhonew 之间存在差异(例如钥匙串),这尤其糟糕。

