我试图在iOS上的本地wifi网络上找到Windows计算机的IP(名为TEST)以用于smb目的。我在这台Windows机器上安装了bonjour,如果我在我的mac终端上输入以下命令(在同一个wifi网络上):
ping TEST.local
我获得了成功(' 64字节来自10.0.1.19:icmp_seq = 285 ttl = 128 time = 3.237 ms')。注意ip(10.0.1.19):它正确解析主机名!到目前为止没问题!
现在,我试图通过使用CFHostCreateWithName在iOS上做同样的事情:
NSString *host = @"TEST.local";
CFHostRef hostref = CFHostCreateWithName(nil/*or kCFAllocatorDefault, same result...*/,(__bridge CFStringRef)host);
CFStreamError *err;
Boolean lookup = CFHostStartInfoResolution(hostref, kCFHostAddresses, err);
NSArray* addresses = (__bridge NSArray*)CFHostGetAddressing(hostref, &lookup);
id first = addresses.firstObject;
if(first!=nil) {
struct in_addr *firstaddress = (__bridge struct in_addr *)first;
NSString *strDNS = [NSString stringWithUTF8String:inet_ntoa(*(firstaddress))];
}
结果不同于:
所以主机解析在mac上很好,但在连接到同一网络的iOS设备上不起作用...我错过了什么吗?
答案 0 :(得分:1)
基本上,您可以使用以下命令获取主机名:
for (int j = 0; j < 100; j++){
在您的情况下,您必须以这种方式修改它:
NSString* hostname = @"Mac-mini.local";
CFHostRef hostRef = CFHostCreateWithName(kCFAllocatorDefault, (__bridge CFStringRef)hostname);
Boolean lookup = CFHostStartInfoResolution(hostRef, kCFHostAddresses, NULL);
if (lookup) {
CFArrayRef addresses = CFHostGetAddressing(hostRef, &lookup);
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 stringWithUTF8String:inet_ntoa(remoteAddr->sin_addr)];
NSLog(@"dns:%@",strDNS);
}
}
CFRelease(addresses);
}