ios - 列出本地网络中的设备名称
<p><p>我能够获取连接的设备 IP 和它们的 MAC 地址,并且还想获取设备的名称和服务(开放端口)。</p>
<p>有人可以帮我解决这个问题吗?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>这里是可以提供帮助的示例项目<a href="https://github.com/mongizaidi/LAN-Scan" rel="noreferrer noopener nofollow">An iOS Local Area Network / wifi Scanner</a> </p>
<p>您也可以尝试使用<a href="https://developer.apple.com/bonjour/" rel="noreferrer noopener nofollow">Bonjour</a> </p>
<p>这里 - <a href="https://stackoverflow.com/a/25993182/2012219" rel="noreferrer noopener nofollow">even more</a>关于局域网中设备的名称</p>
<hr/>
<p>我发现了几个变种:</p>
<p>1.</p>
<pre><code>struct hostent *he;
struct in_addr ipv4addr;
inet_pton(AF_INET, , &ipv4addr);
he = gethostbyaddr(&ipv4addr, sizeof ipv4addr, AF_INET);
if (he) {
printf("Host name: %s\n", he->h_name);
}
</code></pre>
<p>where - 比如 127.0.0.1 (ip)</p>
<p>2.</p>
<pre><code>- (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(, NULL, &hints, &result);
if (errorStatus != 0) return @[];
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 = ;
for (int currentIndex = 0; currentIndex < [(__bridge NSArray *)hostnamesRef count]; 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;
}
</code></pre>
<ol 开始=“3”>
3.
</ol>
<p>您可以从 <code>getifaddrs(&interfaces)</code> 获得其他信息,其中 <code>struct ifaddrs* interfaces</code> 喜欢</p>
<pre><code> unsigned char *ptr;
ptr = (unsigned char *)LLADDR((struct sockaddr_dl *)(temp_addr)->ifa_addr);
NSString *ip = ;
NSArray *hostsNames = ;
NSMutableDictionary *info = [NSMutableDictionary dictionaryWithDictionary:
@{
@"name": @(temp_addr->ifa_name),
@"flags": @(temp_addr->ifa_flags),
@"ip" : ip,
@"family": @(temp_addr->ifa_addr->sa_family),
@"mac" :", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)],
@"hostName" : hostsNames.count ? hostsNames : @""
}
];
</code></pre>
<p>结果你会得到类似的东西:</p>
<pre><code>{
family = 2;
flags = 32841;
hostName = (
localhost
);
ip = "127.0.0.1";
mac = "MAC";
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";
name = en1;
}
</code></pre>
<p>还有 <a href="http://blog.toshsoft.de/index.php?/archives/5-3-Ways-to-Resolve-a-Hostname-in-iOS.html" rel="noreferrer noopener nofollow">this post very interesting</a>并且可能有用(我稍后会调查)</p>
<p>关于 <code>ifa_name</code>(接口(interface))的一些有用信息</p>
<pre><code> // 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)
</code></pre></p>
<p style="font-size: 20px;">关于ios - 列出本地网络中的设备名称,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/32717953/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/32717953/
</a>
</p>
页:
[1]