-5

How can I find all IP addresses of machines which are connected using LAN network.

foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces())
        {
            IPInterfaceProperties ipProps = netInterface.GetIPProperties();
            foreach (UnicastIPAddressInformation addr in ipProps.UnicastAddresses)
            {
                listBox1.Items.Add(addr.Address.ToString());
            }
        }

Here the code i am using.. but this code return me only my local ip and 127.0.0.1. Can't find other ip address.

Narendra
  • 13
  • 1
  • 3
  • 2
    please show some code... what have you tried ? what is not working ? – Yahia Dec 10 '12 at 08:01
  • What you are trying to do seems more like the job of a DHCP server... Please explain why you need to do this so people can help with alternatives if needed. – Ravi Y Dec 10 '12 at 08:02
  • 2
    Your level of effort is approaching 0. – leppie Dec 10 '12 at 08:04
  • @ryadavilli: a DHCP server configures network devices. Finding IP addresses on a local network isn't something you'd use a DHCP server for. – Osiris Dec 10 '12 at 08:06
  • check this link http://stackoverflow.com/questions/151231/how-do-i-get-the-local-network-ip-address-of-a-computer-programmatically-c – Sirwan Afifi Dec 10 '12 at 08:08

2 Answers2

2

In general, you can't. You can try pinging all machines in an IP address range (the local subnet, for example), but some active machines may not respond to ping. You can try connecting to open ports (web server port, Windows file sharing port, etc.) and you might find some machines, but don't expect to find all. And if the network is monitored, you may look like a hacker.

Depending on your network, you could try reverse DNS lookups for an IP address range. This works where DHCP is used and tied to a local DNS server, as is common because it's one way to access a host by name even when the dynamic IP address changes. When it works, this approach will return active leases with a host name (so some of the machines might not be currently active).

jimhark
  • 4,938
  • 2
  • 27
  • 28
0

There is a Ping class in System.Net namespace. But you can use it only if target computer doesn't running firewall.

using System.Net

NetworkInformation.Ping newping = new NetworkInformation.Ping();
NetworkInformation.PingReply reply = newping.Send("192.168.1.1");
if (reply.Status == NetworkInformation.IPStatus.Success)
{
    // Target computer is an active host
}

If the computer has firewall, you can't use simple network management protocol on your switches.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364