9

I am writing a program where you connect, for various reasons, to other computers in a LAN. However, rather than having to input the IP address for multiple computers (a pain in the butt), I was wondering if there is a way to list the IP addresses of all the computers in a LAN. I have researched all day, and as of yet have found nothing suitable. Is this because nothing of this sort exists? Thank you in advance.

EDIT: It would seem that with the many views this post is getting, I should post my actual solution. In general, the naming conventions for computers IP addresses on a LAN are the same. example being 192.168.2.*, * being replaced with any valid number. My program detects the IP address, displays it to the user, then asks for the first 3 blocks of IP. It then sequentially scans up to 200 in the given IP naming convention by pinging and waiting for a response. No response, no computer. It can do everything you can do with an IP once it knows it has a computer behind it.

Bloodyaugust
  • 2,463
  • 8
  • 30
  • 46
  • You could get the subnet, and loop through the available addresses to ping them. But that wouldn't help for the ones that are just turned off. If you had access, you could look at DHCP lease files – OMG Ponies Jan 03 '10 at 03:45
  • 1
    `ARPing`, as Jason described, is the only cross-platform and firewall-defensible solution. BlueRaja's WNetEnumResource() approach is reasonable if you're limited to Windows boxen. The scanning/nmap approaches have too many dragons with host-based firewalls. There is not a magic bullet/API call. The iphelper API's SendARP() is likely your best bet: http://msdn.microsoft.com/en-us/library/aa366358%28VS.85%29.aspx – J.J. Jan 03 '10 at 03:59
  • Why not 254? (255 in larger subnets) – Deanna Jul 24 '12 at 13:40

5 Answers5

7

1) Read the subnet mask and calculate all the IP addresses in the subnet mask you are in. Then you can either user ICMP ping (standard ping) or ARP ping to list all the valid IP addresses. ARP Ping is much reliable in a subnet setting.

2) You can nmap to list all the hosts

nmap -nsP 192.168.10.1/254 | grep ^Host
Chandra Patni
  • 17,347
  • 10
  • 55
  • 65
6

You're not really going to find anything more reliable than pinging or arpinging addresses on the same subset. I implemented this for a certain piece of software back in the day on my first internship and, last time I checked (to be fair it was several years ago), that is what they were still using for this functionality. I take that to mean that they haven't found anything better.

It is not hard to find the source code for these and translate them to C#. ping, arping. Alternatively, you just shell out to a command prompt and execute ping and then parse the results.

jason
  • 236,483
  • 35
  • 423
  • 525
  • Actually, `System.Net.NetworkInformation.Ping` should do the trick. No need to translate anything from C. – GeReV Jul 15 '10 at 10:27
2

Any host discovery tool can help you here. In particular Nmap will certainly give you this information although it might be overkill in this situation. Google for "ping scan" and you should get some useful results.

ennuikiller
  • 46,381
  • 14
  • 112
  • 137
2

See WNetOpenEnum() and WNetEnumResource(), here.

BlueRaja - Danny Pflughoeft
  • 84,206
  • 33
  • 197
  • 283
0

Can you just look at the IP and subnet mask on the network adapter and ping every address? Whichever ones respond can be queried to see if it has whatever you need to connect.

SqlRyan
  • 33,116
  • 33
  • 114
  • 199