c++ 本地流量统计

wangyurong21 2012-08-17 01:49:26
#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);
...全文
303 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangyurong21 2012-08-20
  • 打赏
  • 举报
回复
请大家给一个比较精确的代码 求急
andy_1973 2012-08-18
  • 打赏
  • 举报
回复
这样获取的流量不够精确,只是个大概,偏差很大
Gloveing 2012-08-18
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

这样获取的流量不够精确,只是个大概,偏差很大
[/Quote]
你说的偏差是和什么比较而言?
cbzjzsb123 2012-08-17
  • 打赏
  • 举报
回复
然后呢?怎么啦?
Gloveing 2012-08-17
  • 打赏
  • 举报
回复
然后呢?怎么啦?
翅膀又硬了 2012-08-17
  • 打赏
  • 举报
回复
你在做什么?你想问什么

65,184

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧