C# Sample to list all the active TCP and UDP connections using Windows Form appl

The sample would list the active TCP and UDP connections in a DataGridView. It shows information that describes an IPv4 TCP connection with IPv4 addresses, ports used by the TCP connection and the specific process ID PID) associated with connection along with the process name.

C# (328.3 KB)
5 Star
3,811 times
Add to favorites
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question

    1 Posts | Last post December 05, 2017
    • There's a typo in MIB_UDPTABLE_OWNER_PID. The type of the table field is UdpProcessRecord but it should be MIB_UDPROW_OWNER_PID. This bug causes an access violation when running in x64.
  • Suggestion to speed up
    1 Posts | Last post August 02, 2017
    • On each instance of TcpProcessRecord and UdpProcessRecord, in the constructor you run Process.GetProcessById which takes on my laptop 5-10 msec, therefore GetAllUdpConnections and GetAllTcpConnections with 100-200 connections takes 1000-3000 msec.
      If you query for all processes before GetExtendedTcpTable function in GetAllUdpConnections and GetAllTcpConnections functions and later select the processname from processes array, the functions will take just 10 msec in total.