Reputation: 3308
I want to measure the bandwidth networking by process in my C# Application.
I've try to find this information in the Process Object, but, this information not exist.
I've try too to use Performance Counter, but I don't find the bandwidth information by process.
Have you an idea for measures the Network Banding ( network bytes send, network bytes received) by process for application work on Windows XP to Windows 8 ?
I know Windows have this information, because when we open the "resources monitor", I can select a specific application in the Network Tab, and we can see the number of bytes send and received in real time.
Thanks.
Upvotes: 1
Views: 1405
Reputation: 755
A simple example for GetTcpTable2()
in C#.
[DllImport("iphlpapi.dll", SetLastError = true)]
static extern int GetTcpTable2(byte[] pTcpTable, out int pdwSize, bool bOrder);
static void Main(string[] args) {
int pdwSize = 20000;
byte[] buffer = new byte[pdwSize];
int res = GetTcpTable2(buffer, out pdwSize, true);
if (res != 0) {
buffer = new byte[pdwSize];
res = GetTcpTable2(buffer, out pdwSize, true);
if (res != 0) throw new Exception(res);
}
int nOffset = 0;
var dwNumEntries = Convert.ToInt32(buffer[nOffset]); nOffset += 4;
Console.WriteLine("total connection: {0}", dwNumEntries);
for (int i = 0; i < dwNumEntries; i++) {
Console.WriteLine("*****************");
int st = Convert.ToInt32(buffer[nOffset]);
Console.WriteLine("state: {0}", st); nOffset += 4;
string LocalAdrr = buffer[nOffset].ToString() + "." + buffer[nOffset + 1].ToString() + "."
+ buffer[nOffset + 2].ToString() + "." + buffer[nOffset + 3].ToString();
Console.WriteLine("local ip: {0}", LocalAdrr); nOffset += 4;
int LocalPort = (((int)buffer[nOffset]) << 8) + (((int)buffer[nOffset + 1])) +
(((int)buffer[nOffset + 2]) << 24) + (((int)buffer[nOffset + 3]) << 16);
Console.WriteLine("local port: {0}", LocalPort); nOffset += 4;
string RemoteAdrr = buffer[nOffset].ToString() + "." + buffer[nOffset + 1].ToString() + "."
+ buffer[nOffset + 2].ToString() + "." + buffer[nOffset + 3].ToString();
Console.WriteLine("remote ip: {0}", RemoteAdrr); nOffset += 4;
int RemotePort = (RemoteAdrr == "0.0.0.0") ? 0
: RemotePort = (((int)buffer[nOffset]) << 8) + (((int)buffer[nOffset + 1])) +
(((int)buffer[nOffset + 2]) << 24) + (((int)buffer[nOffset + 3]) << 16);
Console.WriteLine("remote port: {0}", RemotePort); nOffset += 4;
int pid = BitConverter.ToInt32(buffer.Skip(nOffset).Take(4).ToArray(), 0);
Console.WriteLine("pid: {0}", pid); nOffset += 4;
int OffloadState = Convert.ToInt32(buffer[nOffset]);
Console.WriteLine("OffloadState: {0}", OffloadState); nOffset += 4;
}
}
Upvotes: 2
Reputation: 29678
As far as I know you cannot do this with pure C# you will have to make some P/Invoke calls into the IP Helper library.
Because this is quite involved I'm going to briefly outline the API calls you'll need to make.
Firstly, this will not work on Windows XP as some of the API are new (which is why Resource Monitor doesn't exist for that version). If you need Windows XP support you're going to have to use something like WinPCap I'm afraid. On Vista+ things get better with the newer network information calls we get to make.
There are separate calls for IPv4, IPv6, TCP and UDP. For brevity I'll just outline the IPv4/TCP set as once you understand the process it can easily be replicated to fill in the other parts.
To start you'll need to call GetTcpTable2()
. This gets you the table of connections currently active on the machine. You'll end up with a MIB_TCPTABLE2
and a bunch of MIB_TCPROW2
structures. Each structure represents a connection.
The MIB_TCPROW2
structure has a PID field which you can match to your current process ID.
So that means continually polling for the connection table, iterate it and dump out the rows relating to your process.
For each of these rows you will then have to call GetPerTcpConnectionEStats()
. There's two stat types you will be interested in, the first TcpConnectionEstatsData
returns information on the number of bytes sent and received. The second TcpConnectionEstatsBandwidth
returns information on the bandwidth of the connection.
The reason I haven't shown any code for this is that its quite involved and involves a lot of structure marshaling, but hopefully I have given you more to go on than you currently have.
Here are links to MSDN for those functions:
GetTcpTable2()
- http://msdn.microsoft.com/en-us/library/bb408406(v=vs.85).aspx
GetPerTcpConnectionEStats()
- http://msdn.microsoft.com/en-us/library/bb485738(v=vs.85).aspx
Upvotes: 6