find all ip address in a network

前端 未结 5 2040
逝去的感伤
逝去的感伤 2020-11-30 10:24

I am trying to do this C#. I need to find all ip address that are active in my network and show them in a list. I can ping all available (1...255) ip address in a network. B

5条回答
  •  天涯浪人
    2020-11-30 10:52

    Please refer to This link about Asynchronous Client Socket to learn how to ping faster.

    Edit: A quick snippet on how to accomplish this:

    private static void StartClient() {
            // Connect to a remote device.
            try {
                // Establish the remote endpoint for the socket.
                // The name of the 
                // remote device is "host.contoso.com".
                IPHostEntry ipHostInfo = Dns.Resolve("host.contoso.com");
                IPAddress ipAddress = ipHostInfo.AddressList[0];
                IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
    
                // Create a TCP/IP socket.
                Socket client = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream, ProtocolType.Tcp);
    
                // Connect to the remote endpoint.
                client.BeginConnect( remoteEP, 
                    new AsyncCallback(ConnectCallback), client);
                connectDone.WaitOne();
    
                // Send test data to the remote device.
                Send(client,"This is a test");
                sendDone.WaitOne();
    
                // Receive the response from the remote device.
                Receive(client);
                receiveDone.WaitOne();
    
                // Write the response to the console.
                Console.WriteLine("Response received : {0}", response);
    
                // Release the socket.
                client.Shutdown(SocketShutdown.Both);
                client.Close();
    
            } catch (Exception e) {
                Console.WriteLine(e.ToString());
            }
        }
    

    Taken from this Microsoft documentation.

提交回复
热议问题