我想使用 C# 获取连接到我的 PC 的所有机器的 IP 地址,但我不想使用 Ping 方法,因为它会花费很多时间,尤其是当 IP 地址范围很广时。
最佳答案
获取所有活跃的TCP连接
使用 IPGlobalProperties.GetActiveTcpConnections 方法
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.
上面代码的简化版本。
foreach (var c in IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections())
{
...
}
将所有机器连接到网络
执行 ping 扫描可能会更好。需要几秒钟来完成 254 个 ip 地址。解决方案在这里 https://stackoverflow.com/a/4042887/3645638
关于c# - 如何在不使用 ping method C# 的情况下使用连接到我的 PC 的机器的 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44276884/