在iOS上查找windows本地主机名的IP地址



我试图在iOS上的本地wifi网络上找到windows计算机(名为TEST)的IP用于smb目的。我已经在这台windows机器上安装了bonjour,如果我在我的mac终端上输入以下命令(在相同的wifi网络上):

ping TEST.local

我得到一个成功('64 bytes from 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))];
}

结果如下:

  • 首先它返回2个地址,都是相同的,并且是无意义的(不在10.0.1.19附近,通常以8开头)
  • 之后,没有结果,并出现错误(err): err->domain = 12和err->error = 8。看起来像是某种形式的缓存,不确定…

所以主机解析在mac上很好,但在连接到相同网络的iOS设备上不起作用…我错过什么了吗?

基本上,您可以使用以下命令获取主机名:

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);
}

在你的情况下,你必须这样修改:

NSString *host = @"TEST.local";
CFHostRef hostref = CFHostCreateWithName(kCFAllocatorDefault,(__bridge CFStringRef)host);
CFStreamError *err;
Boolean lookup = CFHostStartInfoResolution(hostref, kCFHostAddresses, err);
NSArray* addresses = (__bridge NSArray*)CFHostGetAddressing(hostref, &lookup);
if(addresses.firstObject!=nil) {
    struct sockaddr_in *firstaddress = (struct sockaddr_in*)CFDataGetBytePtr((__bridge CFDataRef)addresses.firstObject);
    NSString *strDNS = [NSString stringWithUTF8String:inet_ntoa(firstaddress->sin_addr)];
}

相关内容

  • 没有找到相关文章

最新更新