Link to home
Start Free TrialLog in
Avatar of PerryDK
PerryDK

asked on

Bandwidth usage given a process id

Given a process id I can get reference to Process object by

int processID = 0;
//initialize processID from whatever
System.Diagnostics.Process process =
  System.Diagnostics.Process.GetProcessById(processID);

Now what I'd like to do is figure out how much bandwidth that particular process is using up.  I know what ports in particular it is using but it would also be nice to know how to figure out what ports a process is using and there current state.

At any rate my main question how do I determine how many bytes, packets, etc that this process is either downloading or uploading?

Code is much appreciated as I just switched over to c# from c++.

Thanks
Avatar of Tom Knowlton
Tom Knowlton
Flag of United States of America image

You are talking about monitoring an FTP session while it is downloading something, fo example?
One thing you might try:

1)  Create your own VERY simple FTP client in C#.
2)  Hook into the process id (in Task Manager) while the FTP client is at rest.
3)  Make note of the amount of RAM used by the process while at rest.
4)  Start a download of a file who's size you know in advance.
5)  Record the additional RAM consumption and see if it adds up.

I am at a loss as to how you would monitor the bandwidth......very interesting problem!
Avatar of PerryDK
PerryDK

ASKER

No I'm not talking about FTP.

Lets say:
Internet BackGammon is running with pid 1
Some FTP Client with pid 2
Instant Messenger with pid 3
...

I can get the protocol, ports, pid associated with a given process by using
C:\Documents and Settings>netstat -n -o

Active Connections

  Proto  Local Address          Foreign Address        State           PID
  TCP    192.168.0.101:1248     192.168.0.1:5678       CLOSE_WAIT      696
  TCP    192.168.0.101:1700     192.168.0.1:5678       TIME_WAIT       0
  TCP    192.168.0.101:1701     192.168.0.1:5678       TIME_WAIT       0
 ...
  TCP    192.168.0.101:1800     192.168.0.1:5678       TIME_WAIT       0
  TCP    192.168.0.101:1801     192.168.0.1:5678       TIME_WAIT       0
  TCP    192.168.0.101:4927     207.46.106.140:1863    ESTABLISHED     852


I can also get overall statistics such as
C:\Documents and Settings>netstat -e
Interface Statistics

                           Received            Sent

Bytes                     220854897       158609049
Unicast packets              792485          943036
Non-unicast packets           85678            1832
Discards                          0               0
Errors                            0               0
Unknown protocols             10245

Now the problem lies in that the EthernetStatistics I get from using the netstat -e command are overall statistics.  What I'd like to do is either through some command line utility (must come with windows or is freeware) or by doing it programmitically in C# is:

C:\Documents and Settings>netstat -pid=1
Internet BackGammon Statistics
                           Received            Sent
Bytes                     220854897       158609049

Note the above does not work its just an example of the output I would like to create.

In otherwords I want to monitor the bandwidth usage of a given process.

I hope that helps explains things further.
Avatar of PerryDK

ASKER

Also would be nice to know how to get same info from netstat -e in C#

Currently I am executing netstat -e and redirecting the standard output...although there is probably a function call or something similar I could be using.
Avatar of PerryDK

ASKER

Never mind on last comment figured out how with PerformanceCounters...but would still very much like to know how to track the information down to a given process.
You should see if IIS has its own performance counters.  I'm thinking you are looking at OS performance counters.  I can't imagine that IIS doesn't have its own performance counters.  (if you don't see them by default you'll most likely have to turn them on, I would imagine they are off by default because they use system resources, why waste that information if no one is watching :).
Whoops I missed your comment "No I'm not talking about FTP."
Avatar of PerryDK

ASKER

You'll have to forgive me...I am very new to C# (4 days old)...I don't know what IIS is?  

Seeing that your last statment says:
Whoops I missed your comment "No I'm not talking about FTP."

I assume IIS has nothing to do with the question...if I am mistaken please inform.
ASKER CERTIFIED SOLUTION
Avatar of ErikPhilips
ErikPhilips
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial