2

Possible Duplicate:
Get a list of all computers on a network w/o DNS

I am creating an application where you can send information between computers on a network. The computers listen for connections and can send information to another computer that is on the network. I need to know how to get a list of hostnames or IP addresses on the network the computer is connected to.

Community
  • 1
  • 1
Phoenix Logan
  • 1,238
  • 3
  • 19
  • 31

1 Answers1

8

I don't know where I got this code from anymore. The last piece of code is from me (getIPAddress())

It reads your ip to get the base ip of the network.

Credits go to the author:

static void Main(string[] args)
    {
        string ipBase = getIPAddress();
        string [] ipParts = ipBase.Split('.');
        ipBase = ipParts[0] + "." + ipParts[1] + "." + ipParts[2] + ".";
        for (int i = 1; i < 255; i++)
        {
            string ip = ipBase + i.ToString();

            Ping p = new Ping();
            p.PingCompleted += new PingCompletedEventHandler(p_PingCompleted);
            p.SendAsync(ip, 100, ip);
        }
        Console.ReadLine();
    }

    static void p_PingCompleted(object sender, PingCompletedEventArgs e)
    {
        string ip = (string)e.UserState;
        if (e.Reply != null && e.Reply.Status == IPStatus.Success)
        {
            if (resolveNames)
            {
                string name;
                try
                {
                    IPHostEntry hostEntry = Dns.GetHostEntry(ip);
                    name = hostEntry.HostName;
                }
                catch (SocketException ex)
                {
                    name = "?";
                }
                Console.WriteLine("{0} ({1}) is up: ({2} ms)", ip, name, e.Reply.RoundtripTime);
            }
            else
            {
                Console.WriteLine("{0} is up: ({1} ms)", ip, e.Reply.RoundtripTime);
            }
            lock (lockObj)
            {
                upCount++;
            }
        }
        else if (e.Reply == null)
        {
            Console.WriteLine("Pinging {0} failed. (Null Reply object?)", ip);
        }
    }

    public static string getIPAddress()
    {
        IPHostEntry host;
        string localIP = "";
        host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (IPAddress ip in host.AddressList)
        {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
            {
                localIP = ip.ToString();
            }
        }
        return localIP;
    }
jAC
  • 5,195
  • 6
  • 40
  • 55
  • Thanks! This is a very good implementation, but is there a way I can have it throw an error if the computer is not connected to a network (either just not connected to the Internet or connected to the Internet with a direct connection)? – Phoenix Logan Jan 06 '13 at 20:07
  • I think you will find here, what you are looking for: http://stackoverflow.com/questions/314213/checking-network-status-in-c-sharp – jAC Jan 06 '13 at 20:23
  • Thank you... I'll make use of this... :) – Phoenix Logan Jan 06 '13 at 22:14