开发者

Check if a host is up on iOS SDK

I'm making an iOS app where I'd need to check if a host, for example, google.com, is available (or is up/down). I've been using this code:

NSString *host = theTextField.text;
        bool success = false;
        const char *host_name = [host
                                 cStringUsingEncodin开发者_如何学Cg:NSASCIIStringEncoding];

        SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL,
                                                                                    host_name);
        SCNetworkReachabilityFlags flags;
        success = SCNetworkReachabilityGetFlags(reachability, &flags);
        bool isAvailable = success && (flags & kSCNetworkFlagsReachable) && 
        !(flags & kSCNetworkFlagsConnectionRequired);
        if (isAvailable) {

            status.text = @"The host is up! :)";

        } else {
            status.text = @"The host is down! :(";
        }

    }

But I've a felling it doesn't works like it should. For example if I enter google.com it says it's up, okay, but if I enter the url of my webpage (which is down ATM) says it's up as well (the address is zad0xsis.net). If I enter a fake address it says it's down, so it may work right and I may be thinking odd things IDK.


zad0xsis.net is up for me.

The Reachability use a the same principle as the ping command line (DNS Resolution + Ping in fact).

I think you should use NSURLConnection to see if the webserver is up.


Have you tried ping zad0xsis.net from terminal? If you receive a successful response, then i believe it is about some networking stuff which i am not good at all.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜