I am aware that not all devices respond to ICMP or pings, so the most suitable way to go seems to send an ARP Request to all the IPs possible on LAN, from 192.168.0.0 to 192.168.255.255, but that means requesting over 65000 IPs and it takes a hell lot of time. I want to find another instance of my program in order to sync their contents, but I am not satisfied with the ARP method. So far I've found these nice piece of code here in SO:
[DllImport("iphlpapi.dll", ExactSpelling = true)]
public static extern int SendARP(int DestIP, int SrcIP, byte[] pMacAddr, ref uint PhyAddrLen);
static void Main(string[] args)
{
List<IPAddress> ipAddressList = new List<IPAddress>();
for (int i = 0; i <= 255; i++)
{
for (int s = 0; s <= 255; s++)
{
ipAddressList.Add(IPAddress.Parse("192.168." + i + "." + s));
}
}
foreach (IPAddress ip in ipAddressList)
{
Thread thread = new Thread(() => SendArpRequest(ip));
thread.Start();
}
}
static void SendArpRequest(IPAddress dst)
{
byte[] macAddr = new byte[6];
uint macAddrLen = (uint)macAddr.Length;
int uintAddress = BitConverter.ToInt32(dst.GetAddressBytes(), 0);
if (SendARP(uintAddress, 0, macAddr, ref macAddrLen) == 0)
{
Console.WriteLine("{0} responded to ping", dst.ToString());
}
}
but as you can see, ARP Requesting all that range would take a long time. 10 to 15 minutes on my Laptop. Of course, in most cases you would find the desired IP a lot faster, I mean it's not that you are going to be so unlucky that a PC would fall on 192.168.255.255 or so, but still, it takes some minutes. This would only be done just one time, as most of the time PCs and Laptops use preferred IPs that would be kept for days or months unless changed, so as long as it keeps working on that IP, another ARP fetching won't be needed. So far the best option is to have this "Hot start" happen for the first time and decorate it with a loading screen, but I'd like to know if there's a much more faster way to achieve this. Also, I'm only looking for Windows PCs since it's just between instances of the same app.