Solved

Counting receieved/sent bytes for a network connection

Posted on 2007-11-16
11
846 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_
[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
  • +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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c++ reading data from file into two dimensional array 3 148
HTTPSendRequest with WinINet delays on first call 11 119
Link failure 16 92
what type of pointer is this pointer? 3 31
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

739 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