简体   繁体   English

在C中获取LAN网络上的所有主机?

[英]Get all hosts on LAN network in C?

Is it possible to get all the hosts on a LAN network(using C). 是否有可能(使用C)获取LAN网络上的所有主机。 I need to get the IP addresses and host names. 我需要获取IP地址和主机名。

As explained in this answer , the to ping all hosts in your subnet and see which respond or access the ARP cache. 如此答案中所述,ping子网中的所有主机并查看哪个主机响应或访问ARP缓存。 The first approach is accomplished by creating a raw socket using 第一种方法是通过使用以下方法创建原始套接字来完成的

int sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP));

However, raw sockets basically give you only the IP header so you need to construct the ICMP echo packet yourself. 但是,原始套接字基本上只为您提供IP标头,因此您需要自己构造ICMP回显数据包。 After you constructed it, then you just send them to every IP in the subnet using sendto() and wait for response. 构造完之后,您只需使用sendto()将它们发送到子网中的每个IP,然后等待响应。 For the second approach I suggest reading something like this . 对于第二种方法,我建议您阅读像这样

As for determining the host names, it depends on whether the hosts have DNS or NETBIOS names. 至于确定主机名,这取决于主机是DNS还是NETBIOS名称。 If you have a DNS configured on your LAN, you may get away with calling getaddrinfo() on the the addresses that respond. 如果您在LAN上配置了DNS,则可以通过在响应的地址上调用getaddrinfo()来摆脱getaddrinfo() Unfortunately, I do not have any idea how to find out the NETBIOS name of any given IP. 不幸的是,我不知道如何找出任何给定IP的NETBIOS名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM