我使用以下代码,CFHostStartInfoResolution
方法始终returns false
,
Boolean result;
CFHostRef hostRef;
CFArrayRef addresses;
NSString *strHostName=@"192.149.252.125";
BOOL bResolved;
CFDataRef myDataRef = (__bridge CFDataRef)[strHostName dataUsingEncoding:NSASCIIStringEncoding];
hostRef = CFHostCreateWithAddress(kCFAllocatorDefault, myDataRef);
if (hostRef) {
CFStreamError *e = NULL;
result = CFHostStartInfoResolution(hostRef, kCFHostNames, e);
if (result == TRUE) {
// Get the array of IP the hostname resolved to
addresses = CFHostGetNames(hostRef, &result);
}
}
bResolved = result == TRUE ? true : false;
if(bResolved)
{
struct sockaddr_in* remoteAddr;
for(int i = 0; i < CFArrayGetCount(addresses); i++)
{
CFDataRef saData = (CFDataRef)CFArrayGetValueAtIndex(addresses, i);
remoteAddr = (struct sockaddr_in*)CFDataGetBytePtr(saData);
if(remoteAddr != NULL)
{
NSString *strDNS =[NSString stringWithCString:inet_ntoa(remoteAddr->sin_addr) encoding:NSASCIIStringEncoding];
NSLog(@"RESOLVED %d:<%@>", i, strDNS);
}
}
}
}