小功能,iOS客户端如何获取域名IP
上代码:
{
NSString *hostname = @"www.baidu.com";
CFHostRef hostRef = CFHostCreateWithName(kCFAllocatorDefault, (__bridge CFStringRef)hostname);
if (hostRef)
{
Boolean result = CFHostStartInfoResolution(hostRef, kCFHostAddresses, NULL);
if (result == TRUE)
{
NSArray *addresses = (__bridge NSArray*)CFHostGetAddressing(hostRef, &result);
tempDNS = [[NSMutableArray alloc] init];
for(int i = 0; i < addresses.count; i++)
{
struct sockaddr_in* remoteAddr;
CFDataRef saData = (CFDataRef)CFArrayGetValueAtIndex((__bridge CFArrayRef)addresses, i);
remoteAddr = (struct sockaddr_in*)CFDataGetBytePtr(saData);
if(remoteAddr != NULL)
{
const char *strIP41 = inet_ntoa(remoteAddr->sin_addr);
NSString *strDNS =[NSString stringWithCString:strIP41 encoding:NSASCIIStringEncoding];
NSLog(@"RESOLVED %d:<%@>", i, strDNS);
[tempDNS addObject:strDNS];
}
}
}
}
}