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

前端 未结 7 715
一向
一向 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:15

    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).

    0 讨论(0)
  • 2020-11-29 01:16

    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:

    http://splinter.com.au/how-to-ping-a-server-in-objective-c-iphone

    0 讨论(0)
  • 2020-11-29 01:22

    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);
    
    //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

    0 讨论(0)
  • 2020-11-29 01:23

    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 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]]; 
        }
    }
    
    0 讨论(0)
  • 2020-11-29 01:33

    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.

    0 讨论(0)
  • 2020-11-29 01:35

    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.

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