Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Counting receieved/sent bytes for a network connection

Posted on 2007-11-16
11
Medium Priority
?
858 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
Industry Leaders: 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:Nathan P
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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

721 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