Hello I need list IP addresses of all connected hosts in my LAN. What is the simpliest way to do this?
You could ping the adress range and note if a host responded. Of course, this will require the host to respond to the ping packets.
You would need to build an address range (e.g. 192.168.0.1 - 192.168.255.254) and ping each of those addresses. If a response is received, then that host is active.
Asynchronous Ping Tutorial:
http://www.geekpedia.com/tutorial234_Asynchronous-Ping-using-Csharp.html
However, some newer operating systems block ping requests (ICMP). This would need to be disabled in the firewall on each computer for you to receive a response.
You'll have to do a ping sweep. There's a Ping class in the System.Net namespace. Example follows. Also this is only possible if your computers don't have firewalls running. If they've got a firewall enabled, there's no way to determine this information short of doing SNMP queries on your switches.
System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingReply rep = p.Send("192.168.1.1");
if (rep.Status == System.Net.NetworkInformation.IPStatus.Success)
{
//host is active
}
The other issue is to determine how large your network is. In most home situations, your network mask will be 24 bits. This means that its set to 255.255.255.0. If your gateway is 192.168.1.1, this means that valid addresses on your network will be 192.168.1.1 to 192.168.1.254. Here's an IP Calculator to help. You'll have to loop through each address and ping the address using the Ping class and check the PingReply.
If you're just looking for the information and aren't concerned with how you get it, you can use NMap. The command would be as follows
nmap -sP 192.168.1.0/24
EDIT:
As far as speed goes, since you're on a local network, you can cut down the timeout interval considerably as your machines shouldn't take more than 100 milliseconds to reply. You can also use SendAsync to ping them all in parallel. The following program will ping 254 address in under half a second.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;
using System.Diagnostics;
using System.Net;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static CountdownEvent countdown;
static void Main(string[] args)
{
countdown = new CountdownEvent(1);
Stopwatch sw = new Stopwatch();
sw.Start();
string ipBase = "10.10.5.";
for (int i = 1; i < 255; i++)
{
string ip = ipBase + i.ToString();
Ping p = new Ping();
p.PingCompleted += new PingCompletedEventHandler(p_PingCompleted);
countdown.AddCount();
p.SendAsync(ip, 100, ip);
}
countdown.Signal();
countdown.Wait();
sw.Stop();
TimeSpan span = new TimeSpan(sw.ElapsedTicks);
Console.WriteLine("Took {0} milliseconds", sw.ElapsedMilliseconds);
Console.ReadLine();
}
static void p_PingCompleted(object sender, PingCompletedEventArgs e)
{
string ip = (string)e.UserState;
//Console.WriteLine("{0}:\t{1} ({2})", ip, e.Reply.Status, e.Reply.RoundtripTime);
if (e.Reply.Status == IPStatus.Success)
{
Console.WriteLine("{0} is up: ({1} ms)", ip, e.Reply.RoundtripTime);
}
countdown.Signal();
}
}
}
Instead of arranging a wild ping-party, you could possibly (I really don't know exactly!) use NetBIOS (request the netbios nametable somehow?) http://technet.microsoft.com/en-us/library/cc757216%28WS.10%29.aspx. Or why not ask your DHCP server?