在 iOS 上查找 windows 本地主机名的 IP 地址
Finding IP address of windows local hostname on iOS
我正在尝试在 iOS 上的本地 wifi 网络上查找 windows 计算机(名为 TEST)的 IP 以用于 smb 目的。我在 windows machine 上安装了 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))];
}
结果不同:
- 首先是returns2个地址,都是一样的,都是废话(不在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)];
}
我正在尝试在 iOS 上的本地 wifi 网络上查找 windows 计算机(名为 TEST)的 IP 以用于 smb 目的。我在 windows machine 上安装了 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))];
}
结果不同:
- 首先是returns2个地址,都是一样的,都是废话(不在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)];
}