I'm trying to check device availability in WIFI
I have modified code from Maher Ali's book “Advanced IOS4 Programming” to use the IP address as follows
- (BOOL) networkConnected: (NSString *) addr { SCNetworkReachabilityFlags flags = 0; SCNetworkReachabilityRef netReachability; BOOL retrievedFlags = NO; // added the "if" and first part of if statement // if (hasLeadingNumberInString(addr)) { struct sockaddr_in the_addr; memset((void *)&the_addr, 0, sizeof(the_addr)); the_addr.sin_family = AF_INET; the_addr.sin_port = htons(1001); const char* server_addr = [addr UTF8String]; unsigned long ip_addr = inet_addr(server_addr); the_addr.sin_addr.s_addr = ip_addr; netReachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (struct sockaddr*)&the_addr); } else { netReachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, [addr UTF8String]); } if (netReachability) { retrievedFlags = SCNetworkReachabilityGetFlags(netReachability, &flags); CFRelease(netReachability); } if (!retrievedFlags || !flags) { return NO; } return YES; }
But it always returns NO using the IP address and port, which, as I know, I can connect to (i.e. if I skip this check, everything will be fine)
I am trying to connect to port "10.0.0.59" 1001, which I can do
it compiles - and I stepped (on ipod) through it, and it works through the code socket section.
netReachability returns as zero.
Relevant Headings Included
#import "Reachability.h" #include <CFNetwork/CFSocketStream.h> #include <arpa/inet.h>
Can anyone determine what I am doing wrong?
source share