Counting receieved/sent bytes for a network connection


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.

LVL 14
Who is Participating?
den4bConnect With a Mentor Commented:
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.

evilrixSenior Software Engineer (Avast)Commented:
Is this just for a programming exercise as there are plenty of tools already that will do this for you?
_Stilgar_Author Commented:
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?

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

evilrixSenior Software Engineer (Avast)Commented:
Loads of them... choose your weapon :)
You need to have a look at the Windows IP Helpers:

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.
Nathan PSystems ArchitectCommented:
DU Meter!
DU Meter provides an accurate account of the data which is flowing through your computer's network connection.

It is shareware though!

Its always been great, and it does logging and totals.
This is a free net traffic watcher, and there must be what you want very much.
_Stilgar_Author Commented:
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?).

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()
  Call GetIfTable() to collect new data;
  InPerTimer := MIB_IFROW.dwInOctets - LastInOctets;
  OutPerTimer := MIB_IFROW.dwOutOctets - LastOutOctets;
  LastInOctets := MIB_IFROW.dwInOctets;
  LastOutOctets := MIB_IFROW.dwOutOctets;

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.
_Stilgar_Author Commented:
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_Author Commented:
Better late than never. Thanks!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.