Ping or otherwise tell if a device is on the network by MAC in C#

萝らか妹 提交于 2019-11-30 03:40:51

A different approach is to use ping and arp tools. Since ARP packets can only stay in the same broadcast domain, you could ping your network's broadcast address and every client will reply with an ARP response. Each of those responses are cached in your ARP table, which you can view with the command arp -a. So the rundown:

rem Clear ARP cache
netsh interface ip delete arpcache

rem Ping broadcast addr for network 192.168.1.0
ping -n 1 192.168.1.255

rem View ARP cache to see if the MAC addr is listed
arp -a

Some of these can be done in managed code, such as in the System.Net.NetworkInformation namespace.

Note: Clearing the ARP cache may have a marginal affect on network performance by clearing cached entries of other local servers. However, the cache is usually cleared every 20 minutes or less anyway.

You could use the RARP protocol to broadcast a request of who owns that MAC address. The owner will respond with its IP address. I don't know of a RARP tool or library to recommend, so you may consider writing your own network layer to send/receive these packets.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!