Ios 列出设备';s在本地网络中的名称
我能够获取连接设备的IP和MAC地址,还想获取设备的名称和服务(开放端口)Ios 列出设备';s在本地网络中的名称,ios,objective-c,iphone,network-programming,netbios,Ios,Objective C,Iphone,Network Programming,Netbios,我能够获取连接设备的IP和MAC地址,还想获取设备的名称和服务(开放端口) 有人能帮我一下吗?这是一个很有帮助的示例项目 你也可以尝试使用 这里-关于lan中设备的名称 我发现了几个变种: 一, 其中[address UTF8String]-类似于127.0.0.1(ip) 二, 因此,您将得到如下结果: { family = 2; flags = 32841; hostName = ( localhost ); ip = "12
有人能帮我一下吗?这是一个很有帮助的示例项目 你也可以尝试使用 这里-关于lan中设备的名称
我发现了几个变种: 一, 其中[address UTF8String]-类似于127.0.0.1(ip) 二, 因此,您将得到如下结果:
{
family = 2;
flags = 32841;
hostName = (
localhost
);
ip = "127.0.0.1";
mac = "MAC[00:00:00:00:00:00]";
name = lo0;
}
{
family = 18;
flags = 34915;
hostName = ""; //here empty due to one of the following problem or due to CFHostStartInfoResolution return NO - o this point required a little bit more works to be done
ip = "6.3.6.0";
mac = "MAC[02:00:00:00:00:00]";
name = en1;
}
而且可能有用(我将在稍后进行调查)
关于ifa\U名称的一些有用信息(接口)
感谢您的回复,使用“wifi扫描仪”,我可以获取设备ip和mac,但不能获取设备名称。有了“你好”,我无法获得非苹果设备的信息。这就像是一个起点,你可以更深入地使用wifiScanner和Investment作者提供的代码——你可以从保存mac ip等的结构中获取设备名称。正如我提到的——这是起点。还可以看到我的updateHey@Kirill,我无法从“wifiScanner”获取设备名称,我已经浏览了代码,找到了下面的struct并尝试了各种可能性。结构addrinfo{int ai_flags;/*ai_PASSIVE,ai_CANONNAME,ai_NUMERICHOST/int ai_family;/PF_xxx/int ai_socktype;/SOCK_xxx/int ai_protocol;/0或IPPROTO_xxx(IPv4和IPv6/socklen_t ai_地址);/ai_地址长度/char*ai_CANONNAME;/hostname/struct sockaddr*ai_地址的规范名称*ai_二进制地址*/struct addrinfo*ai_下一步;};你能提供一些片段吗(您好或其他)感谢@kirill的努力,我已经尝试了你提供的所有解决方案,但没有成功,我一直在尝试使用bonjour,能够获取台式机和笔记本电脑的名称,但无法获取手机和其他设备的名称。我知道这篇文章太旧了,但你能帮我获取设备的IP和MAC地址吗。?
- (NSArray *)hostnamesForAddress:(NSString *)address {
// Get the host reference for the given address.
struct addrinfo hints;
struct addrinfo *result = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_NUMERICHOST;
hints.ai_family = PF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0;
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
int errorStatus = getaddrinfo([address cStringUsingEncoding:NSASCIIStringEncoding], NULL, &hints, &result);
if (errorStatus != 0) return @[[self getErrorDescription:errorStatus]];
CFDataRef addressRef = CFDataCreate(NULL, (UInt8 *)result->ai_addr, result->ai_addrlen);
if (addressRef == nil) return nil;
freeaddrinfo(result);
CFHostRef hostRef = CFHostCreateWithAddress(kCFAllocatorDefault, addressRef);
if (hostRef == nil) return nil;
CFRelease(addressRef);
BOOL isSuccess = CFHostStartInfoResolution(hostRef, kCFHostNames, NULL);
if (!isSuccess) return nil;
// Get the hostnames for the host reference.
CFArrayRef hostnamesRef = CFHostGetNames(hostRef, NULL);
NSMutableArray *hostnames = [NSMutableArray array];
for (int currentIndex = 0; currentIndex < [(__bridge NSArray *)hostnamesRef count]; currentIndex++) {
[hostnames addObject:[(__bridge NSArray *)hostnamesRef objectAtIndex:currentIndex]];
}
return hostnames;
}
- (NSString *)getErrorDescription:(NSInteger)errorCode
{
NSString *errorDescription = @"";;
switch (errorCode) {
case EAI_ADDRFAMILY: {
errorDescription = @" address family for hostname not supported";
break;
}
case EAI_AGAIN: {
errorDescription = @" temporary failure in name resolution";
break;
}
case EAI_BADFLAGS: {
errorDescription = @" invalid value for ai_flags";
break;
}
case EAI_FAIL: {
errorDescription = @" non-recoverable failure in name resolution";
break;
}
case EAI_FAMILY: {
errorDescription = @" ai_family not supported";
break;
}
case EAI_MEMORY: {
errorDescription = @" memory allocation failure";
break;
}
case EAI_NODATA: {
errorDescription = @" no address associated with hostname";
break;
}
case EAI_NONAME: {
errorDescription = @" hostname nor servname provided, or not known";
break;
}
case EAI_SERVICE: {
errorDescription = @" servname not supported for ai_socktype";
break;
}
case EAI_SOCKTYPE: {
errorDescription = @" ai_socktype not supported";
break;
}
case EAI_SYSTEM: {
errorDescription = @" system error returned in errno";
break;
}
case EAI_BADHINTS: {
errorDescription = @" invalid value for hints";
break;
}
case EAI_PROTOCOL: {
errorDescription = @" resolved protocol is unknown";
break;
}
case EAI_OVERFLOW: {
errorDescription = @" argument buffer overflow";
break;
}
}
return errorDescription;
}
unsigned char *ptr;
ptr = (unsigned char *)LLADDR((struct sockaddr_dl *)(temp_addr)->ifa_addr);
NSString *ip = [NSString stringWithUTF8String: inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
NSArray *hostsNames = [self hostnamesForAddress:ip];
NSMutableDictionary *info = [NSMutableDictionary dictionaryWithDictionary:
@{
@"name": @(temp_addr->ifa_name),
@"flags": @(temp_addr->ifa_flags),
@"ip" : ip,
@"family": @(temp_addr->ifa_addr->sa_family),
@"mac" : [NSString stringWithFormat:@"MAC[%02x:%02x:%02x:%02x:%02x:%02x]", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)],
@"hostName" : hostsNames.count ? hostsNames : @""
}
];
{
family = 2;
flags = 32841;
hostName = (
localhost
);
ip = "127.0.0.1";
mac = "MAC[00:00:00:00:00:00]";
name = lo0;
}
{
family = 18;
flags = 34915;
hostName = ""; //here empty due to one of the following problem or due to CFHostStartInfoResolution return NO - o this point required a little bit more works to be done
ip = "6.3.6.0";
mac = "MAC[02:00:00:00:00:00]";
name = en1;
}
// pdp_ip - interfaces are those that are used for 3G and cellular data
// lo = localhost
// en = ether
// eth - ethernet
// wlan, ww, wl - Wifi - Wireless LAN
// awdl - ???
// utun - ???
// ap - is used to represent currently active data connection, Wi-Fi, cellular data or bluetooth or for access point
// bridge - Active hotSpot connection
// sl -- serial line IP (slip)