从iPhone ping到服务器端口?
如何从Objective C(iphone)ping到服务器端口?…首先,您应该看看这个问题 然后 我找到了这个密码从iPhone ping到服务器端口?,iphone,objective-c,Iphone,Objective C,如何从Objective C(iphone)ping到服务器端口?…首先,您应该看看这个问题 然后 我找到了这个密码 How to write a simple Ping method in Cocoa/Objective-C * Start * Kontakt How to write a simple Ping method in Cocoa/Objective-C Question: How to write a simple Ping method in
How to write a simple Ping method in Cocoa/Objective-C
* Start
* Kontakt
How to write a simple Ping method in Cocoa/Objective-C
Question: 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.
avatar rjstelling
Autor: rjstelling
Utworzono: 28 Apr 2009
Language: question language en
Answers:
1. 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
2. Let me try this again...this time logging in, and formatting better ;-) StreamSCNetworkCheckReachabilityByName is deprecated and NOT available for the iPhone. bool success = false; const char *host_name = [@"stackoverflow.com" cStringUsingEncoding:NSASCIIStringEncoding]; SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, host_name); SCNetworkReachabilityFlags flags; success = SCNetworkReachabilityGetFlags(reachability, &flags); 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
3. Look into CFHost and in particular CFHostGetReachability . There is sample CFHost code available, as well, which includes a routine to check host availability.
4. 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.
5. i tested with IP address but it not works properly: result is always YES! is it possibile that do not exist a simple echo with timeout class?
6. Hi, 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. This is particularly hell if there is a difference between the Mac OSX and iPhonew(for example the keychain).
7. 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". Am I misunderstanding something? Thanks. Here's my code just in case: From https://stackoverflow.com/questions/798454/how-to-write-a-simple-ping-method-in-cocoa-objective-c - (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]]; } }
8. 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 code from Apple.
首先你应该看看这个问题 然后 我找到了这个密码
How to write a simple Ping method in Cocoa/Objective-C
* Start
* Kontakt
How to write a simple Ping method in Cocoa/Objective-C
Question: 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.
avatar rjstelling
Autor: rjstelling
Utworzono: 28 Apr 2009
Language: question language en
Answers:
1. 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
2. Let me try this again...this time logging in, and formatting better ;-) StreamSCNetworkCheckReachabilityByName is deprecated and NOT available for the iPhone. bool success = false; const char *host_name = [@"stackoverflow.com" cStringUsingEncoding:NSASCIIStringEncoding]; SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, host_name); SCNetworkReachabilityFlags flags; success = SCNetworkReachabilityGetFlags(reachability, &flags); 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
3. Look into CFHost and in particular CFHostGetReachability . There is sample CFHost code available, as well, which includes a routine to check host availability.
4. 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.
5. i tested with IP address but it not works properly: result is always YES! is it possibile that do not exist a simple echo with timeout class?
6. Hi, 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. This is particularly hell if there is a difference between the Mac OSX and iPhonew(for example the keychain).
7. 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". Am I misunderstanding something? Thanks. Here's my code just in case: From https://stackoverflow.com/questions/798454/how-to-write-a-simple-ping-method-in-cocoa-objective-c - (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]]; } }
8. 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 code from Apple.
答案很简单
使用适用于MAc OS的SimplePing程序
将MAC的CFNetwork框架替换为iPhone的CFNetwork框架
并运行该程序
请记住,以iPhone的形式启动项目,然后复制SimplePing.h和.m,并在视图控制器中复制MAin的代码
将main.m(以下)中的此主功能替换为(bootom代码启动)
将此启动标记方法连接到按钮 简单的答案
使用适用于MAc OS的SimplePing程序
将MAC的CFNetwork框架替换为iPhone的CFNetwork框架
并运行该程序
请记住,以iPhone的形式启动项目,然后复制SimplePing.h和.m,并在视图控制器中复制MAin的代码
将main.m(以下)中的此主功能替换为(bootom代码启动)
将此启动标记方法连接到按钮 我也遇到了同样的问题,并最终围绕SimplePing编写了一个简单的包装器来实现这一点,为此我写了一个博客,github上有一些代码,希望能对这里的人有所帮助:
我也遇到了同样的问题,最后为SimplePing编写了一个简单的包装器来实现这一点,写了一篇关于它的博客,github上有一些代码,希望能对这里的人有所帮助: