monitor bytes transferred over network?

Windows specific questions.
Post Reply
DamageX
Posts: 130
Joined: Nov 21, 2009 8:42

monitor bytes transferred over network?

Post by DamageX »

Does anyone know how to count the total size of data transferred through a network adaptor? Would it be necessary to use WMI?

There are already programs that do this, but they all stink. The best one I found was Shaplus bandwidth meter, but it causes 24/7 disk activity so the HDD can't power down :( In the debugger, this program does everything through MSVBVM60.DLL, I have no idea how it works...

My old Athlon with VIA ethernet adaptor would show the total bytes transferred on the adaptor status page. But everything else will only show 'packets' and I don't know how to see the statistics in bytes.
UEZ
Posts: 1079
Joined: May 05, 2017 19:59
Location: Germany

Re: monitor bytes transferred over network?

Post by UEZ »

Try something like this here:

Code: Select all

'Coded by UEZ build 2020-06-25
#Include "windows.bi"
#Include "win/iphlpapi.bi"

Declare Function Convert(Bytes As Longint) As String

Const IF_TYPE_ETHERNET_CSMACD = 6, IF_TYPE_IEEE80211 = 71
Dim As _MIB_IFTABLE Ptr pIfTable, pMemIfTable 'https://docs.microsoft.com/en-us/windows/win32/api/ifmib/ns-ifmib-mib_ifrow
Dim As _MIB_IFROW Ptr pIfRow
Dim As DWORD dwSize = 0

GetIfTable(pIfTable, @dwSize, FALSE) 'first call to get the dwSize value
pMemIfTable = Allocate(dwSize)
GetIfTable(Cast(_MIB_IFTABLE Ptr, pMemIfTable), @dwSize, True)

'display some adapters
For i As Ushort = 0 To pMemIfTable->dwNumEntries - 1
	If (pMemIfTable->table(i).dwType = IF_TYPE_ETHERNET_CSMACD Or pMemIfTable->table(i).dwType = IF_TYPE_IEEE80211) _
		And pMemIfTable->table(i).dwOperStatus = IF_OPER_STATUS_OPERATIONAL Then
		? i & ": ";
		For j As Ushort = 0 To pMemIfTable->table(i).dwDescrLen 'get all ethernet/wlan adapters names which are operational
			? Chr(pMemIfTable->table(i).bDescr(j));
		Next
		?
	End If
Next

Dim As Single fTimer = Timer
Dim As Ushort iAdapter = 3 '<--- change adapter according to the displayed list
? 
? "Example using table " & iAdapter & " adapter:"
Dim as Integer iPosCursor = Csrlin
Dim As ULongint iBytesIn, iBytesOut

Do
	If Timer - fTimer > 0.99 Then
		GetIfTable(Cast(_MIB_IFTABLE Ptr, pMemIfTable), @dwSize, True)
		iBytesIn = pMemIfTable->table(iAdapter).dwInOctets
		iBytesOut = pMemIfTable->table(iAdapter).dwOutOctets
		Locate iPosCursor, 1, 0
		? "Sent:", iBytesOut & " bytes (" & Convert(iBytesOut) & ")          "
		? "Received:", iBytesIn & " bytes (" & Convert(iBytesIn) & ")          "
		fTimer = Timer
	End If
	
	Sleep(10)
Loop Until Len(Inkey())

Deallocate(pMemIfTable)
End

Function Convert(Bytes As Longint) As String
	Dim As String aSuf(0 To ...) = {"B", "KB", "MB", "GB", "TB", "PB", "EB"}
	If Bytes = 0 Then Return "0 " & aSuf(0)
	Dim As ULongint b = Abs(Bytes)
	Dim As Ubyte p = Log(b) / Log(1024)
	Dim As Single num = b / (1024 ^ p)
	Return Str(num) & " " & aSuf(p)
End Function
You need to adjust the adapter in line 29 according to the list which is displayed on the 1st run.
DamageX
Posts: 130
Joined: Nov 21, 2009 8:42

Re: monitor bytes transferred over network?

Post by DamageX »

This is exactly what I wanted. Thanks :)
Post Reply