Solved

Bandwidth usage given a process id

Posted on 2004-08-14
12
850 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:PerryDK
  • 4
  • 3
  • 2
  • +1
12 Comments
 
LVL 5

Expert Comment

by:knowlton
ID: 11806309
You are talking about monitoring an FTP session while it is downloading something, fo example?
0
 
LVL 5

Expert Comment

by:knowlton
ID: 11806318
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!
0
 
LVL 4

Author Comment

by:PerryDK
ID: 11806905
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.
0
 
LVL 4

Author Comment

by:PerryDK
ID: 11806967
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.
0
 
LVL 4

Author Comment

by:PerryDK
ID: 11817530
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:ErikPhilips
ID: 11817840
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 :).
0
 
LVL 4

Expert Comment

by:ErikPhilips
ID: 11817856
Whoops I missed your comment "No I'm not talking about FTP."
0
 
LVL 4

Author Comment

by:PerryDK
ID: 11817872
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.
0
 
LVL 4

Accepted Solution

by:
ErikPhilips earned 250 total points
ID: 11817880
Microsoft Internet Information Server (IIS web/ftp server) with counters you could read.  You could read how much data in/out of the FTP server.  But then I realized thats not what you wanted.
0
 
LVL 4

Assisted Solution

by:Eran_R
Eran_R earned 250 total points
ID: 11873622
Take a look at the crazy technology called "Winsock 2 Layered Service Providers" that enable writing one to gather bandwidth data per process.

http://www.microsoft.com/msj/0599/LayeredService/LayeredService.aspx

It's quite difficult to master, but you could write an LSP (Layered Service Provider) that adds metering for Socket I/O on a per-process basis
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now