Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get all the active TCP connections using .NET Framework (no unmanaged PE import!)?

Tags:

c#

.net

sockets

How can I get all the the active TCP connections using .NET Framework (no unmanaged PE import!)?

I'm getting into socket programming and would like to check this. In my research I found solutions by importing an unmanaged DLL file which I am not interested in.

like image 502
RollRoll Avatar asked Dec 10 '12 17:12

RollRoll


1 Answers

I'm surprised with the quantity of users telling me that was not possible to do with pure managed code... For future users who is wondering about that, find the details from the answer that worked fine for me:

//Don't forget this:
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());
    }
}

And call ShowActiveTcpConnections() to list it, awesome and beautiful.

Source: IPGlobalProperties.GetActiveTcpConnections Method (MSDN)

like image 194
RollRoll Avatar answered Oct 20 '22 01:10

RollRoll