【问题标题】:iPhone DNS resolution, not all A records returnediPhone DNS解析,并非所有A记录都返回
【发布时间】:2011-07-02 01:17:20
【问题描述】:

我有以下代码可以检索 URL 的所有 DNS A 记录。当我从 UI 调用此代码时,它可以正常工作并返回所有 A 记录,但是,当从其他异步线程调用此代码时,它总是只返回一个 IP(一个 A 记录),有什么想法吗?

这是代码:

Boolean result;
CFHostRef hostRef = NULL;
CFArrayRef addresses = NULL;
NSString *hostname = [NSString stringWithCString:server];
hostRef = CFHostCreateWithName(kCFAllocatorDefault, (CFStringRef)hostname);
if (hostRef) {  
result = CFHostStartInfoResolution(hostRef, kCFHostAddresses, NULL); // pass an error instead of NULL here to find out why it failed
        if (result == TRUE) {
            addresses = CFHostGetAddressing(hostRef, &result);
        }

    }
    if (result == TRUE) 
    {
        NSLog(@"ResolveDNS - Resolved");


        for (CFIndex count = 0; count < CFArrayGetCount(addresses); count++)
        {
            char addr[256];
            struct sockaddr_in *sa = (struct sockaddr_in *)
            CFDataGetBytePtr((CFDataRef)CFArrayGetValueAtIndex(addresses, count));
            // inet_ntop will correctly display both IPv4 and IPv6 addresses
            if (inet_ntop(sa->sin_family, &sa->sin_addr, addr, sizeof(addr)))
                printf("%s:%d \n", addr, ntohs(sa->sin_port));

            [ipTmpArray addObject:[NSString stringWithCString:addr]];
        }

【问题讨论】:

    标签: iphone multithreading dns


    【解决方案1】:

    我所有的 iOS 设备(都在 4.2 或 4.3)上都发生了同样的情况,但在我的模拟器上却没有。 如果我再次启动相同的方法,有时我会获得更多 IP,有时甚至是全部。

    我怀疑这是由于 iOS,他们在记录以前的分辨率时进行了某种内存优化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-05
      • 2021-11-15
      • 2023-04-09
      • 1970-01-01
      • 2014-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多