windows获取流量

来源:互联网 发布:网络借贷存管业务指引 编辑:程序博客网 时间:2024/05/17 02:27

一个轻量级的网络流量监控程序,只能在局域网中使用。

主要用到GetIfTable(),和GetIfEntry() 两个API。界面用QT做,可以隐藏在系统托盘中。

截图

核心代码如下

[cpp] view plaincopy
  1. void NetFlow::getFlow() {  
  2.     // 记录上一次函数调用时的总流量和时间,在后面计算流量速度的时候用到  
  3.     preInFlow = inFlow;  
  4.     preOutFlow = outFlow;  
  5.     preTime = currTime;  
  6.     inFlow = 0.0f;  
  7.     outFlow = 0.0f;  
  8.     currTime = GetTickCount();  
  9.     int i;  
  10.     pIfTable = (MIB_IFTABLE *) MALLOC(sizeof (MIB_IFTABLE));  
  11.     if (pIfTable == NULL) {  
  12.         printf("Error allocating memory needed to call GetIfTable/n");  
  13.         return;  
  14.     }  
  15.     dwSize = sizeof (MIB_IFTABLE);  
  16.     if (GetIfTable(pIfTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) {  
  17.         FREE(pIfTable);  
  18.         pIfTable = (MIB_IFTABLE *) MALLOC(dwSize);  
  19.         if (pIfTable == NULL) {  
  20.             printf("Error allocating memory/n");  
  21.             return;  
  22.         }  
  23.     }  
  24.     if ((dwRetVal = GetIfTable(pIfTable, &dwSize, 0)) == NO_ERROR) {  
  25.         if (pIfTable->dwNumEntries > 0) {  
  26.             pIfRow = (MIB_IFROW *) MALLOC(sizeof (MIB_IFROW));  
  27.             if (pIfRow == NULL) {  
  28.                 printf("Error allocating memory/n");  
  29.                 if (pIfTable != NULL) {  
  30.                     FREE(pIfTable);  
  31.                     pIfTable = NULL;  
  32.                 }  
  33.                 return;  
  34.             }  
  35.             printf("/tNum Entries: %ld/n/n", pIfTable->dwNumEntries);  
  36.             // 枚举所有接口  
  37.             for (i = 0; i < (int) pIfTable->dwNumEntries; i++) {  
  38.                 pIfRow->dwIndex = pIfTable->table[i].dwIndex;  
  39.                 if ((dwRetVal = GetIfEntry(pIfRow)) == NO_ERROR) {  
  40.                     // 排除环回接口的流量  
  41.                     if(pIfRow->dwType != MIB_IF_TYPE_LOOPBACK) {  
  42.                         inFlow += pIfRow->dwInOctets;  
  43.                         outFlow += pIfRow->dwOutOctets;                    
  44.                     }  
  45.                 }  
  46.                 else {  
  47.                     printf("GetIfEntry failed for index %d with error: %ld/n",  
  48.                            i, dwRetVal);  
  49.                 }  
  50.             }  
  51.         } else {  
  52.             printf("/tGetIfTable failed with error: %ld/n", dwRetVal);  
  53.         }  
  54.     }  
  55.     FREE(pIfTable);  
  56.     FREE(pIfRow);  
  57.     // 计算流量,速度=总流量 / 所经过的时间  
  58.     float inSpeed = (inFlow - preInFlow) / ((float)(currTime - preTime) / 1000.0f);  
  59.     float outSpeed = (outFlow - preOutFlow) / ((float)(currTime - preTime) / 1000.0f);  
  60.     char buffer[100];  
  61.     sprintf(buffer, "Download: %.2fk/s Upload: %.2fk/s", inSpeed / 1000.0f, outSpeed / 1000.0f);  
  62.     bodyEdit->setText(buffer);  
  63.     trayIcon->setToolTip(QString(buffer));  
  64. }  

下载地址:http://download.csdn.net/source/3040966

0 0
原创粉丝点击