How can I get a list of computers connected to the

2020-03-26 13:41发布

问题:

This question already has answers here:
Closed 7 years ago.

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.

回答1:

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;
    }