在C#中使用GetActiveTcpConnections()时如何确定哪个端点启动了连接

时间:2015-12-15 17:38:46

标签: c#

此代码工作正常,但我想知道是否可以获取有关谁发起每个连接的信息?就像在哪个端点发起连接一样。这可能吗?

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());
    }
}

1 个答案:

答案 0 :(得分:0)

在列表端口上收到TCP连接。入站连接将从GetActiveTcpListeners()返回,并可从GetActiveTcpConnections()中扣除,以获得出站连接。

https://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipglobalproperties.getactivetcplisteners(v=vs.110).aspx

您可以查看http://www.timvw.be/2007/09/09/build-your-own-netstatexe-with-c/

这将告诉您如何获得该过程:

Console.WriteLine(&#34; {0}&#34;,processModule.FileName);