How to get the number of open TCP connections held by a particular process in .NET.

I need to monitor the number of open TCP connections stored in one process on a 64-bit Windows 7 machine, with .NET 4.0.

This article describes the undocumented Win32 API method "AllocateAndGetTcpExTableFromStack", which performs this task in 32-bit windows:

http://www.codeproject.com/KB/IP/iphlpapi.aspx?display=Print

But this fails on my machine (presumably because I'm on the 64th bit):

Could not find an entry point named "AllocateAndGetTcpExTableFromStack" in the dll 'iphlpapi.dll'.

How can we do this on 64-bit Windows?

+4
source share
1 answer

You can start with System.Net.NetworkInformation . In particular, IPGlobalProperties.GetActiveTcpConnections .

However, you will find that this library does not provide a PID, so there is no way to narrow it down by the specific process associated with each connection. I suppose it will be a lot easier to just parse the output of the netstat -ano console into a collection of managed objects and use a simple LINQ query to pull the right connections based on the process id. However, I would not recommend this if you intend to do this often, as this will be a very slow method.

You can also try PInvoke for GetExtendedTcpTable() in the DLL you mentioned above. I do not have my x64 window for testing, but it may be a simple denunciation of the specific function you are trying to call.

+4
source

Source: https://habr.com/ru/post/895573/


All Articles