簡體   English   中英

如何在不使用ping方法C#的情況下將機器的IP地址連接到我的PC

[英]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地址的范圍非常寬時。

獲取所有活動的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());
    }
}

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

注意:這僅為您提供活動的TCP連接


上面代碼的較短版本。

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

讓所有機器連接到網絡

做ping掃可能更好。 需要幾秒鍾才能完成254個ip地址。 解決方案在這里https://stackoverflow.com/a/4042887/3645638

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM