c++ 本地流量统计
#include "stdafx.h "
#include <windows.h>
#include <Iphlpapi.h>
#pragma comment(lib, "Iphlpapi.lib ")
int main(int argc, char* argv[])
{
PMIB_IFTABLE m_pTable = NULL;
DWORD m_dwAdapters = 0;
ULONG uRetCode = GetIfTable(m_pTable, &m_dwAdapters, TRUE);
if (uRetCode == ERROR_NOT_SUPPORTED)
{
return (-1);
}
if (uRetCode == ERROR_INSUFFICIENT_BUFFER)
{
m_pTable = (PMIB_IFTABLE)new BYTE[65535];//MIB_IFTABLE[m_dwAdapters];
}
DWORD dwLastIn = 0;
DWORD dwLastOut = 0;
DWORD dwBandIn = 0;
DWORD dwBandOut = 0;
do
{
GetIfTable(m_pTable, &m_dwAdapters, TRUE);
DWORD dwInOctets = 0;
DWORD dwOutOctets = 0;
for (UINT i = 0; i < m_pTable-> dwNumEntries; i++)
{
MIB_IFROW Row = m_pTable-> table[i];
dwInOctets += Row.dwInOctets;
dwOutOctets += Row.dwOutOctets;
}
dwBandIn = dwInOctets - dwLastIn;
dwBandOut = dwOutOctets - dwLastOut;
if (dwLastIn <= 0)
{
dwBandIn = 0;
}
else
{
dwBandIn = dwBandIn / 1024;
}
if (dwLastOut <= 0)
{
dwBandOut = 0;
}
else
{
dwBandOut = dwBandOut / 1024;
}
dwLastIn = dwInOctets;
dwLastOut = dwOutOctets;
printf( "收到字节: %u bytes\n ", dwLastIn);
printf( "发送字节: %u bytes\n ", dwLastOut);
printf( "下行速度: %u KB\n ", dwBandIn);
printf( "上行速度: %u KB\n ", dwBandOut);
printf( "--------------------------\n ");
Sleep(1000);
}while (TRUE);
delete [] m_pTable;
return (0);