Solved

Bandwidth usage given a process id

Posted on 2004-08-14
12
903 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
12 Comments
 
LVL 5

Expert Comment

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

Expert Comment

by:Tom 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

688 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