【发布时间】: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