Solved

Bandwidth usage given a process id

Posted on 2004-08-14
12
869 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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
 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
About delegates in c sharp 3 47
Reference issues in Visual Studio 3 30
InputLanguage 1 26
Getting error in connectionstring with Excel. 30 32
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 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