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