Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting per connection bandwidth statistics

I need to determine per-process network usage statistics similar to what TCPView can do.

Example http://img513.imageshack.us/img513/861/6601f15814544055a590e26.png

So before you shoot me for posting a duplicate of this question, or this question, I would like to point out that neither of those have a thorough answer that could help me actually do this.

I've been doing research, and there are clearly many ways to list out active connections and their associated processes, whether this be with netstat or other windows API's like IpHlpAPI.dll.

Now, from what Google'ing I've done, I have not found much - except for these vague terms: GetPerTcpConnectionEStats and GetPerTcp6ConnectionEStats. Presumably for TCP over IPv4 and IPv6 respectively. Now where I was reading its supposedly able to do what I need to do. However, that still leaves out UDP. And those are also not available on XP systems, which TCPViewer works on.

I would be satisfied with using those for TCP, but the problem is, I can't seem to find any examples of how to use them from C#.

So I guess it all boils down to these few questions:

  • Does anyone actually know how TCPView does it?
  • How do I use GetPerTcpConnectionEStats for the TCP? Or can it even accomplish what I'm suggesting?
  • Is there another known alternative that would work for UDP?

The whole point of this is to see the independent bandwidth usage of the processes themselves. Not calculate the total system bandwidth usage.

Thanks in advance for any and all answers.

like image 479
caesay Avatar asked Nov 05 '22 04:11

caesay


1 Answers

I spent a bit of time trying to figure out how I'd do something similar (latency vs bandwidth) but have had to give up due to a lack of time to invest. I thought I'd add my findings here in the hope we eventually get an answer.

You'll obviously need to use PInvoke and I'm afraid the specifics are far beyond my experience with it.

This Wrapper Library on CodeProject uses AllocateAndGetTcpExTableFromStack which only works on Windows XP and Server 2003. It still doesn't get you bandwidth though sorry.

The closest information I could find as a starting point (for Vista/Server 2008 or newer) is an example on pinvoke.net for GetExtendedTcpTable.

The tool PInvoke Interop Assistant might help determine how to call the unmanaged code.

If I were to keep trying this I'd try to figure out the following:

  1. How to call SetPerTcpConnectionEStats to enable collection of TcpConnectionEstatsBandwidth via the appropriate TCP_ESTATS_TYPE.
  2. I'd use IPGlobalProperties.GetActiveTcpConnections to get the information needed to create a MIB_TCPROW.
  3. Determine how to call GetPerTcpConnectionEStats to monitor the data.
like image 87
Hastarin Avatar answered Nov 15 '22 05:11

Hastarin