• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 890
  • Last Modified:

Counting receieved/sent bytes for a network connection

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
_Stilgar_
Asked:
_Stilgar_
  • 4
  • 3
  • 2
  • +2
1 Solution
 
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?
0
 
_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?

Stilgar.
0
 
evilrixSenior Software Engineer (Avast)Commented:
Loads of them... choose your weapon :)

http://www.snapfiles.com/freeware/network/fwnetmoni.html
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
den4bCommented:
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
 
Nathan PSystems ArchitectCommented:
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
 
yuy2002Commented:
http://www.codeproject.com/internet/trafficwatcher.asp
This is a free net traffic watcher, and there must be what you want very much.
0
 
_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?).

Stilgar.
0
 
den4bCommented:
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
 
_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.
0
 
den4bCommented:
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
 
_Stilgar_Author Commented:
Better late than never. Thanks!
0
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.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now