美文网首页iOS
iOS 客户端如何获取域名IP

iOS 客户端如何获取域名IP

作者: dyg_zql_2006 | 来源:发表于2017-01-10 17:29 被阅读0次

    小功能,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];

    }

    }

    }

    }

    }

    相关文章

      网友评论

        本文标题:iOS 客户端如何获取域名IP

        本文链接:https://www.haomeiwen.com/subject/qqtlbttx.html