如何获取本地网络上所有设备的名称和 ip
How to get the names and ips of all devices on my local network?
我的目标是列出本地网络上所有设备的名称和IP。
列出iPhone上本地网络上的所有计算机名称,类似于这篇文章,但没有得到答复。
我已经研究了Apples Bonjour,但并不认为这是我所需要的。它不会显示网络上的所有设备。
我也尝试了下面的代码,但我得到奇怪的输出(如下)。
int sock;
struct ifconf ifconf;
struct ifreq ifreq[30];
int interfaces;
int i;
// Create a socket or return an error.
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
diep("socket");
// Point ifconf's ifc_buf to our array of interface ifreqs.
ifconf.ifc_buf = (char *) ifreq;
// Set ifconf's ifc_len to the length of our array of interface ifreqs.
ifconf.ifc_len = sizeof ifreq;
// Populate ifconf.ifc_buf (ifreq) with a list of interface names and addresses.
if (ioctl(sock, SIOCGIFCONF, &ifconf) == -1)
diep("ioctl");
// Divide the length of the interface list by the size of each entry.
// This gives us the number of interfaces on the system.
interfaces = ifconf.ifc_len / sizeof(ifreq[0]);
// Print a heading that includes the total # of interfaces.
printf("IF(%d)tIPn", interfaces);
// Loop through the array of interfaces, printing each one's name and IP.
for (i = 0; i < interfaces; i++) {
char ip[INET_ADDRSTRLEN];
struct sockaddr_in *address = (struct sockaddr_in *) &ifreq[i].ifr_addr;
// Convert the binary IP address into a readable string.
if (!inet_ntop(AF_INET, &address->sin_addr, ip, sizeof(ip)))
diep("inet_ntop");
printf("FOUND: %st%sn", ifreq[i].ifr_name, ip);
}
close(sock);
这是输出,我无法真正理解。
FOUND: lo0 24.3.0.0
FOUND: 28.30.0.0
FOUND: 0.0.0.0
FOUND: 0.0.0.0
FOUND: 0.0.0.1
FOUND: ip0 112.100.112.95
FOUND: pdp_ip1 255.7.0.0
FOUND: 20.18.4.0
FOUND: ip2 0.0.0.0
FOUND: ap1256372272J221253 0.0.0.0
FOUND: 105.112.50.0
FOUND: 254.74.145.171
FOUND: ip2 0.0.0.0
FOUND: ip2 101.110.49.142
FOUND: awdl0 6.5.6.0
FOUND: 273Y311 28.30.0.0
我希望看到这样的输出:
My-Mac 192.168.0.21
My-iPad 192.168.0.22
Mum's iPhone 192.168.0.23
Objective-C或C++中的解决方案将是有用的。谢谢。
刚刚又挖出了这个项目...这是我尝试按照@PRSingh要求共享代码......希望对您有所帮助。
在我的 .h 中定义
NSNetServiceBrowser *serviceBrowser;
NSNetService *service;
NSMutableArray* snames;
NSMutableArray* sips;
UIActivityIndicatorView* spinner;
在我的.m中
#pragma mark - NSNetServiceDelegate Methods
- (void)netServiceWillResolve:(NSNetService *)sender {
NSLog(@"netServiceWillResolve");
serviceBrowser = [[NSNetServiceBrowser alloc] init];
if(!serviceBrowser) {
NSLog(@"The NSNetServiceBrowser couldn't be allocated and initialized.");
}
serviceBrowser.delegate = self;
[serviceBrowser searchForServicesOfType:@"_type._tcp." inDomain:@"local."];
}
- (void)netServiceWillPublish:(NSNetService *)netService
{
NSLog(@"netServiceWillPublish");
}
- (void)netServiceDidPublish:(NSNetService *)sender {
NSLog(@"netServiceDidPublish");
}
- (void)netService:(NSNetService *)sender didNotPublish:(NSDictionary *)errorDict {
NSLog(@"didNotPublish");
}
- (void)netServiceDidResolveAddress:(NSNetService *)netService {
NSLog(@"netServiceDidResolveAddress");
for (NSData* data in [netService addresses]) {
char addressBuffer[100];
struct sockaddr_in* socketAddress = (struct sockaddr_in*) [data bytes];
int sockFamily = socketAddress->sin_family;
if (sockFamily == AF_INET /*|| sockFamily == AF_INET6*/) {
const char* addressStr = inet_ntop(sockFamily,
&(socketAddress->sin_addr), addressBuffer,
sizeof(addressBuffer));
// int port = ntohs(socketAddress->sin_port);
if (addressStr){
if(![sips containsObject:[NSString stringWithFormat:@"%s",addressStr]]){
[snames addObject:netService.name];
[sips addObject:[NSString stringWithFormat:@"%s",addressStr]];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
}
}
}
}
}
- (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict {
NSLog(@"didNotResolve: %@",errorDict);
}
- (void)netService:(NSNetService *)sender didUpdateTXTRecordData:(NSData *)data {
NSLog(@"didUpdateTXTRecordData");
}
- (void)netServiceDidStop:(NSNetService *)netService {
NSLog(@"netServiceDidStop");
}
#pragma mark - NSNetServiceBrowserDelegate Methods
- (void)netServiceBrowser:(NSNetServiceBrowser *)netServiceBrowser didFindService:(NSNetService *)netService moreComing:(BOOL)moreServicesComing {
NSLog(@"didFindService: %@ lenght:%d",netService.name,[netService.name length]);
[services addObject:netService];
}
- (void)netServiceBrowser:(NSNetServiceBrowser *)netServiceBrowser didRemoveService:(NSNetService *)netService moreComing:(BOOL)moreServicesComing {
[services removeObject:netService];
[netService stop];
int index = [snames indexOfObject:netService.name];
[snames removeObjectAtIndex:index];
[sips removeObjectAtIndex:index];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
NSLog(@"didRemoveService");
}
为了做到这一点,我相信网络上的所有设备都需要运行"_type_.tcp"的"类型"(或你给它的任何名称)。您可以试用该类型,以查看本地网络上的哪些设备已使用某些服务类型 (https://developer.apple.com/library/mac/qa/qa1312/_index.html)。
相关文章:
- 如何使用 Boost Asio 在 Android 上获取我的本地 udp IP 地址?
- 从网址获取 IP (C++)
- PHP Profiler语言 - 使用 C++ 从 PHP 扩展获取客户端 IP 和 URL
- 如何使用 libcurl 在本地 IP 上获取 HTML?
- 如何从 boost::asio::ssl::stream<boost::asio::ip::tcp::socket> 获取本机套接字文件描述器?
- 如何从sk_buff获取IP选项
- 如何使用 asio 库获取 IP 地址的主机名
- 从适用于 SQL Server 的 Azure 帐户获取 IP
- 可视化如何使用C++程序获取DHCPV6服务器IP.我尝试使用DHCPv6RequestParams,但总是收到错误代码
- 如何从活动的 udp 连接获取远程 IP 地址和端口
- 如何在FD_SET内获取套接字的IP地址
- 使用Boost库中的PC中获取我的以太网设备IP地址的列表
- 如何获取其他人可以使用的IP来连接到我的c 中的套接字构建
- 如何在Windows上绕过主机文件获取真实的IP地址
- 使用DHCP获取IP地址,网关,掩码和广播信息
- 在网络C 上获取计算机的IP地址
- 提升::ASIO 获取客户端 IP
- 从IP地址获取完整的计算机名称,例如 abc.xyz.xvvx.net
- Qt - 从IP Adreess获取主机名和MAC地址
- 试图从IP摄像机获取视频流时崩溃