我想获得使用C#连接到我的PC的所有机器的IP地址,但我不想使用Ping方法,因为它需要花费很多时间,特别是当IP地址的范围非常宽时。
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());
}
}
Note: This only gives you the active TCP connections .
Shorter version of the code above.
foreach (var c in IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections())
{
...
}
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.