Hi
I wanted to fetch all IP addresses of all devices connected to LAN but below code is fetching only 192.168.1 series ip address only. But in my LAN there are
- 192.168.4
- 192.168.5
- 192.168.6
- 192.168.7
- 192.168.8
kindly help me. thank you. below is my code.
static string NetworkGateway()
{
string ip = null;
foreach (NetworkInterface f in NetworkInterface.GetAllNetworkInterfaces())
{
if (f.OperationalStatus == OperationalStatus.Up)
{
foreach (GatewayIPAddressInformation d in f.GetIPProperties().GatewayAddresses)
{
ip = d.Address.ToString();
}
}
}
return ip;
}
public void Ping_all()
{
string gate_ip = NetworkGateway();
//Extracting and pinging all other ip's.
string[] array = gate_ip.Split('.');
for (int i = 2; i <= 255; i++)
{
string ping_var = array[0] + "." + array[1] + "." + array[2] + "." + i;
//time in milliseconds
Ping(ping_var, 4, 4000);
}
}
public void Ping(string host, int attempts, int timeout)
{
for (int i = 0; i < attempts; i++)
{
new Thread(delegate()
{
try
{
System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
ping.PingCompleted += new PingCompletedEventHandler(PingCompleted);
ping.SendAsync(host, timeout, host);
}
catch
{
// Do nothing and let it try again until the attempts are exausted.
// Exceptions are thrown for normal ping failurs like address lookup
// failed. For this reason we are supressing errors.
}
}).Start();
}
}