IPHLPAPI流量统计

int GetTraffic(void)
{
     DWORD dwSize = 0;
     DWORD dwRetVal = 0;
     int i = 0;
     int j = 0;
     /* variables used for GetIfTable and GetIfEntry */
     MIB_IFTABLE *pIfTable;
     MIB_IFROW *pIfRow;

     // Allocate memory for our pointers.
     pIfTable = (MIB_IFTABLE *) MALLOC(sizeof (MIB_IFTABLE));
     if (pIfTable == NULL) 
     {
        return 0;
     }
     // Make an initial call to GetIfTable to get the
     // necessary size into dwSize
     dwSize = sizeof (MIB_IFTABLE);
     if (GetIfTable(pIfTable, &dwSize, FALSE) == ERROR_INSUFFICIENT_BUFFER) 
     {
          FREE(pIfTable);
          pIfTable = (MIB_IFTABLE *) MALLOC(dwSize);
          if (pIfTable == NULL) 
          {
             return 0;
          }
     }
     // Make a second call to GetIfTable to get the actual
     // data we want.
     if ((dwRetVal = GetIfTable(pIfTable, &dwSize, FALSE)) == NO_ERROR) 
     {
     <span style="font-family: Arial, Helvetica, sans-serif;">         for (i = 0; i < (int) pIfTable->dwNumEntries; i++) </span>
        {
           pIfRow = (MIB_IFROW *) & pIfTable->table[i];
           if ((pIfRow->dwType == IF_TYPE_ETHERNET_CSMACD) || (pIfRow->dwType == IF_TYPE_PPP))//以太网
           {
               m_lConnection = TRUE;
               if (strstr(Descr,"cmwap") || strstr(Descr,"cmnet"))//接入点是cmwap
               {       sprintf(m_sNowTraffic,"%1.2f",pIfRow->dwInOctets / 1024.0);//现在的流量
                       m_bReturn = TRUE;
               }
            }
            if (pIfTable != NULL) 
            {
                FREE(pIfTable);
                pIfTable = NULL;
            }
        }
     }
     else
     {
         if (pIfTable != NULL) 
         {
             FREE(pIfTable);
             pIfTable = NULL;
         }  
         return 0;
         // Here you can use FormatMessage to find out why 
         // it failed.
      }

      if (pIfTable != NULL) 
      {
           FREE(pIfTable);
           pIfTable = NULL;
      }
}


typedef struct _MIB_IFROW
{
    WCHAR   wszName[MAX_INTERFACE_NAME_LEN];
    DWORD    dwIndex;
    DWORD    dwType;
    DWORD    dwMtu;
    DWORD    dwSpeed;
    DWORD    dwPhysAddrLen;
    BYTE    bPhysAddr[MAXLEN_PHYSADDR];
    DWORD    dwAdminStatus;
    DWORD    dwOperStatus;
    DWORD    dwLastChange;
    DWORD    dwInOctets;
    DWORD    dwInUcastPkts;
    DWORD    dwInNUcastPkts;
    DWORD    dwInDiscards;
    DWORD    dwInErrors;
    DWORD    dwInUnknownProtos;
    DWORD    dwOutOctets;
    DWORD    dwOutUcastPkts;
    DWORD    dwOutNUcastPkts;
    DWORD    dwOutDiscards;
    DWORD    dwOutErrors;
    DWORD    dwOutQLen;
    DWORD    dwDescrLen;
    BYTE    bDescr[MAXLEN_IFDESCR];
} MIB_IFROW,*PMIB_IFROW;

typedef struct _MIB_IFTABLE
{
    DWORD     dwNumEntries;
    MIB_IFROW table[ANY_SIZE];
} MIB_IFTABLE, *PMIB_IFTABLE;

<pre class="libCScode" name="code" style="white-space: pre-wrap; word-wrap: break-word;"><p><span style="font-size:14px;">wzsName:包含了该接口的名字(多字节的),具体也不知道哈意思,就是一串数字,有懂的和我说一下</span></p><p><span style="font-size:14px;">dwIndex:该接口的索引值,比如有多个网卡的时候,每个网卡都有一个索引值,是会随着网卡正在被使用的个数变化的</span></p><p><span style="font-size:14px;">dwType:该接口的类型,这个类型是被IANA(是个什么协会吧)定义的,有以下几种:</span></p><p><span style="font-size:14px;"></span></p><table border="0"><tbody><tr><th>Value</th><th>Meaning</th></tr><tr><td><a target=_blank id="IF_TYPE_OTHER" style="color: rgb(51, 102, 153);"></a><dl><dt>IF_TYPE_OTHER </dt><dt>1 </dt></dl></td><td><p>Some other type of network interface.</p></td></tr><tr><td><a target=_blank id="IF_TYPE_ETHERNET_CSMACD" style="color: rgb(51, 102, 153);"></a><dl><dt>IF_TYPE_ETHERNET_CSMACD </dt><dt>6 </dt></dl></td><td><p>An Ethernet network interface.</p></td></tr><tr><td><a target=_blank id="IF_TYPE_ISO88025_TOKENRING" style="color: rgb(51, 102, 153);"></a><dl><dt>IF_TYPE_ISO88025_TOKENRING </dt><dt>9 </dt></dl></td><td><p>A token ring network interface.</p></td></tr><tr><td><a target=_blank id="IF_TYPE_PPP" style="color: rgb(51, 102, 153);"></a><dl><dt>IF_TYPE_PPP </dt><dt>23 </dt></dl></td><td><p>A PPP network interface.</p></td></tr><tr><td><a target=_blank id="IF_TYPE_SOFTWARE_LOOPBACK" style="color: rgb(51, 102, 153);"></a><dl><dt>IF_TYPE_SOFTWARE_LOOPBACK </dt><dt>24 </dt></dl></td><td><p>A software loopback network interface.</p></td></tr><tr><td><a target=_blank id="IF_TYPE_ATM" style="color: rgb(51, 102, 153);"></a><dl><dt>IF_TYPE_ATM </dt><dt>37 </dt></dl></td><td><p>An ATM network interface.</p></td></tr><tr><td><a target=_blank id="IF_TYPE_IEEE80211" style="color: rgb(51, 102, 153);"></a><dl><dt>IF_TYPE_IEEE80211 </dt><dt>71 </dt></dl></td><td><p>An IEEE 802.11 wireless network interface.</p></td></tr><tr><td><a target=_blank id="IF_TYPE_TUNNEL" style="color: rgb(51, 102, 153);"></a><dl><dt>IF_TYPE_TUNNEL </dt><dt>131 </dt></dl></td><td><p>A tunnel type encapsulation network interface.</p></td></tr><tr><td><a target=_blank id="IF_TYPE_IEEE1394" style="color: rgb(51, 102, 153);"></a><dl><dt>IF_TYPE_IEEE1394 </dt><dt>144 </dt></dl></td><td><p>An IEEE 1394 (Firewire) high performance serial bus network interface.</p></td></tr></tbody></table><p></p>

其中24是网络回路的网卡(我自己是这样叫的),就是127.0.0.1那个,应该是每个机子都有的吧

一般我们用的是6.

dwMtu:百度一下MTU就知道了,就是该接口的最大传输单元,理解为该通信协议的某一层上面能通过的最大的数据包的大小(以字节为单位)

dwSpeed:该接口最大的传输速率,可是看成是这个接口每秒最多传多大的数据的一个规格,我刚开始的时候以为这是该接口的即时传输速度呢,郁闷

dwPhysAddrLen: bPhysAddr指向的地址的长度

bPhysAddr:指向该接口地址的指针

dwAdminStatus:该接口的管理状态,按我的理解就是人为设定的那个状态:启用/禁用

dwOperStatus:该接口的操作状态,它可以取以下的值,看了下面的值就知道是什么意思了

0   MIB_IF_OPER_STATUS_NON_OPERATIONAL    网络适配器被禁止的状态;

1   MIB_IF_OPER_STATUS_UNREACHABLE    没有连接的状态;

2   MIB_IF_OPER_STATUS_DISCONNECTED  电缆未连接的状态;

3   MIB_IF_OPER_STATUS_CONNECTING   广域网适配器连接中的状态;

4   MIB_IF_OPER_STATUS_CONNECTED   广域网适配器连接上远程对等点的状态;

5   MIB_IF_OPER_STATUS_OPERATIONAL  局域网适配器默认的连接状态;

dwLastChange: 适配器状态最后一次改变的时间;

dwInOctets: 该接口总的收到的数据大小;

dwInUcastPkts As Long     '总共收到(unicast包)
dwInNUcastPkts As Long    '总共收到(non-unicast包),包括广播包和多点传送包dwInDiscards As Long     '收到后丢弃包总数(即使没有错误)
dwInErrors As Long        '收到出错包总数
dwInUnknownProtos As Long   '收到后因协议不明而丢弃的包总数
dwOutOctets As Long       '总共发送(字节)
dwOutUcastPkts As Long    '总共发送(unicast包)
dwOutNUcastPkts As Long '总共发送(non-unicast包),包括广播包和多点传送包
dwOutDiscards As Long     '发送丢弃包总数(即使没有错误)
dwOutErrors As Long       '发送出错包总数
dwOutQLen As Long      '发送队列长度
dwDescrLen As Long       ' bDescr部分有效长度
bDescr(0 To 255) As Byte  '接口描述  也就是在设备管理器上看的到名字

用一秒钟前后得到的dwInOctets数据相减,就是这一秒中该接口的流量,用这个方法就可以计算机子的即时流量了

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

此间的年少

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值