ios - Testing connectivity / availability of IP Address in Objective C -
basically able check whether particular host on local network "up".
the following line of code hangs when host unreachable, i'd perform check first before running it.
[_outputstream write:[data bytes] maxlength:[data length]];
a similar query think answered in following link, think need use cfhostcreatewithaddress
rather cfhostcreatewithname
alternatives nshost in iphone app
here attempt @ i'm trying do...
boolean result; struct sockaddr_in address; address.sin_family = af_inet; address.sin_port = htons(80); inet_pton(af_inet, "192.168.1.31", &address.sin_addr); cfdataref sockdata = cfdatacreate(null, &address, sizeof(address)); cfhostref host = cfhostcreatewithaddress(null, sockdata); result = cfhoststartinforesolution(host, kcfhostaddresses, null); if (result == true) { nslog(@"resolved"); } else { nslog(@"not resolved"); }
even when host not resolved.
below attempt use reachability class. code telling me below address reachable despite there being no host @ specified address.
struct sockaddr_in address; address.sin_family = af_inet; address.sin_port = htons(80); inet_pton(af_inet, "192.168.1.31", &address.sin_addr); reachability *reachability = [reachability reachabilitywithaddress:&address]; networkstatus reachabilitytohost = [reachability currentreachabilitystatus]; if(reachabilitytohost != notreachable) { nslog(@"reachable"); } else { nslog(@"not reachable"); }
add reachability class project.
#import "reachability.h"
also add systemconfiguration
framework.
reachability *reachability = [reachability reachabilitywithhostname:@"www.example.com"]; networkstatus reachabilitytohost = [reachability currentreachabilitystatus]; if(reachabilitytohost != notreachable) { //reachable } else { // not reachable }
check sample code here: https://developer.apple.com/library/ios/samplecode/reachability/introduction/intro.html
for more info: https://developer.apple.com/library/ios/samplecode/reachability/listings/reachability_reachability_h.html
Comments
Post a Comment