ping to web server from iphone
I am developing an aplication which needs to download some data from webserver.So i need to first ping to the web server to check whether the service is available after checking the internet connectivity .I used the following code to check server availability
BOOL success = NO;
const char *host_name = [@"http://192.168.1.7:8080/TestWeb/webresources/"
cStringUsingEncoding:NSASCIIStringEncoding];
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL,
host_name);
SCNetworkReachabilityFlags flags;
success = SCNetworkReachabilityGetFlags(reachability, &flags);
BOOL isAvailable = success && (flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired);
return isAvailable;
where my server is uploaded locally.But this code always return an invalid result,but it works correctly if i gave some exixting site开发者_JAVA百科s like google.com.Please let me know whats i am doing wrong..
I would use NSURLConnection to ping the server. If the connection can't be made, you will get the response in the delegate method connectionDidFailWithError.
Check out the Apple documentation, they have a good code example: link text
精彩评论