简体   繁体   中英

How to get the IP addresses of the machines are connected to my PC using without using ping methode C#

我想获得使用C#连接到我的PC的所有机器的IP地址,但我不想使用Ping方法,因为它需要花费很多时间,特别是当IP地址的范围非常宽时。

Getting all the active TCP connections

Use the IPGlobalProperties.GetActiveTcpConnections Method

using System.Net.NetworkInformation;

public static void ShowActiveTcpConnections()
{
    Console.WriteLine("Active TCP Connections");
    IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
    TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();
    foreach (TcpConnectionInformation c in connections)
    {
        Console.WriteLine("{0} <==> {1}",
                          c.LocalEndPoint.ToString(),
                          c.RemoteEndPoint.ToString());
    }
}

source:
https://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipglobalproperties.getactivetcpconnections.aspx

Note: This only gives you the active TCP connections .


Shorter version of the code above.

foreach (var c in IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections())
{
    ...
}

Getting all the machines connected to a network

It might be better to do a ping sweep. Takes a few seconds to do 254 ip addresses. Solution is here https://stackoverflow.com/a/4042887/3645638

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM