I am trying to check the reachability of a device on WIFI
I have modified code from Maher Ali's book "Advanced IOS4 Programming" to use an 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 an IP address and port that I know I can connect to (i.e. if I skip this check, all goes well)
I am trying to connect to "10.0.0.59" port 1001 which I can do
it does compile - and I have stepped (on an ipod) through and it is running through the socket section of the code.
netReachability is returned as zero.
relevant headers included are
#import "Reachability.h"
#include <CFNetwork/CFSocketStream.h>
#include <arpa/inet.h>
Can anybody spot what I am doing wrong?