Solved

Counting receieved/sent bytes for a network connection

Posted on 2007-11-16
11
836 Views
Last Modified: 2013-12-04
Hi,

I'm trying to find a way to have a small utility that will count all sent and recieved bytes for a network connection, for example Wi-Fi connection or some sort of a dial-up connection. How can I do that using Win32 API (or MFC/WTL/ATL classes if any)?

I assume this will involve enumerating the available network connection, and then hooking on them in order to count the bytes.

I will appreciate any help on this. I have no idea where to start. Also, if anyone knows of an existing utility that does that, that might be just as helpful.

Stilgar.
0
Comment
Question by:_Stilgar_
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 20296993
Is this just for a programming exercise as there are plenty of tools already that will do this for you?
0
 
LVL 14

Author Comment

by:_Stilgar_
ID: 20297011
I need a small tray utility to count those values and let me reset them whenever I want. I couldn't find anything like this, so I thought I'd just code one, for fun, unless its gonna turn ugly. Any idea how to do that?

Stilgar.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20297024
Loads of them... choose your weapon :)

http://www.snapfiles.com/freeware/network/fwnetmoni.html
0
 
LVL 6

Expert Comment

by:den4b
ID: 20297026
You need to have a look at the Windows IP Helpers:
http://msdn2.microsoft.com/en-us/library/aa366073.aspx

You need to continuously call GetIfTable() function which tells you all of the required information about network interfaces (byte counts, etc). You need to call it periodically, and subtract the latest values from the values received in the previous call to get the data transfer rate for the time period between 2 consequent calls to the function.
0
 
LVL 6

Expert Comment

by:LectricX
ID: 20297051
DU Meter!
DU Meter provides an accurate account of the data which is flowing through your computer's network connection.

http://www.hageltech.com/dumeter/

It is shareware though!

Its always been great, and it does logging and totals.
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:yuy2002
ID: 20297153
http://www.codeproject.com/internet/trafficwatcher.asp
This is a free net traffic watcher, and there must be what you want very much.
0
 
LVL 14

Author Comment

by:_Stilgar_
ID: 20297319
Okay, I got an apetite to try mine.

@den4b - How often should I call it? also, MIB_IFROW seem to contain the connection speed already? what should I be looking for when counting bytes - dwOutOctets and dwInOctets?

@yuy2002 - This one looks nice, but works with an external library I'm not familiar with. I want to keep this one as simple as possible without installing any drivers. All I need to know is the traffic that passes through a specific network driver, and the ports used, if possible. Also, I found this in his code:

      //since this program will only work with ethernet network adapters, we don't
      //check the network type...

And I'm not using LAN, but Wi-Fi or cellular dial-up (I'm not sure if this is considered RAS?).

Stilgar.
0
 
LVL 6

Expert Comment

by:den4b
ID: 20297412
MIB_IFROW.dwSpeed is the speed of the interface (in bits per second), but not the actual data transfer rate. For example, standard network card will have 100 Mbs speed. Wi-Fi could have 54 Mbs.

Things which you need to capture are dwInOctets and dwOutOctets. Use a timer, and set it to 1s for example. Every time the timer is triggered, collect new block of data via GetIfTable() and compare those two variables against the old ones.

Pseudo code:

procedure OnTimer()
begin
  Call GetIfTable() to collect new data;
  InPerTimer := MIB_IFROW.dwInOctets - LastInOctets;
  OutPerTimer := MIB_IFROW.dwOutOctets - LastOutOctets;
  LastInOctets := MIB_IFROW.dwInOctets;
  LastOutOctets := MIB_IFROW.dwOutOctets;
end;

So now, if your timer is set to 1 second, then InPerTimer variable will have "IN Bytes Per Second" value, and OutPerTimer will have "OUT Bytes Per Second".

Hope this helps.
0
 
LVL 14

Author Comment

by:_Stilgar_
ID: 20297475
Yes, thanks, except for one confusion. I'm not looking for the speed, but to sum up the in/out traffic of that specific connection. By now I know I can use MIB_IFROW.dwInOctets and MIB_IFROW.dwOutOctets to sum those up, but how do I enumerate all the available network connections, or check a spcific connection status (so when it is offline I wait for it to initialize, and when its active know when its offline, and in the meanwhile count the bytes)?

Stilgar.
0
 
LVL 6

Accepted Solution

by:
den4b earned 500 total points
ID: 20297571
GetIfTable() function gives you MIB_IFTABLE, which represents a collection of MIB_IFROW - one for each network interface.

MIB_IFROW.dwOperStatus will tell you the status on the interface (disabled, disconnected, connecting, etc). MIB_IFROW.dwInOctets and MIB_IFROW.dwOutOctets are already counters of all transferred in/out data. Look at the links below for more info.

GetIfTable(): http://msdn2.microsoft.com/en-us/library/aa365943.aspx
MIB_IFTABLE: http://msdn2.microsoft.com/en-us/library/aa366842.aspx
MIB_IFROW: http://msdn2.microsoft.com/en-us/library/aa366836.aspx
0
 
LVL 14

Author Closing Comment

by:_Stilgar_
ID: 31409514
Better late than never. Thanks!
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

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

912 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

27 Experts available now in Live!

Get 1:1 Help Now