现在的位置: 首页 > 综合 > 正文

vc获得网卡流量信息 vc获得网卡流量信息       

2013年09月04日 ⁄ 综合 ⁄ 共 16894字 ⁄ 字号 评论关闭

vc获得网卡流量信息       

 

  1. //获得网卡流量信息         
  2. DWORD dwIfBufSize = 0; 
  3.     BOOL bRet; 
  4.  
  5.     if(m_pMIT == NULL) 
  6.         m_pMIT = new MIB_IFTABLE[sizeof(MIB_IFTABLE)]; 
  7.  
  8.     bRet = ::GetIfTable(m_pMIT,&dwIfBufSize,0); 
  9.     if(bRet == ERROR_INSUFFICIENT_BUFFER)//如果内存不足,则重新分配内存大小 
  10.     { 
  11.         if(dwIfBufSize != NULL) 
  12.             delete [] m_pMIT; 
  13.         m_pMIT = new MIB_IFTABLE[dwIfBufSize]; 
  14.     } 
  15.  
  16.     bRet = ::GetIfTable(m_pMIT,&dwIfBufSize,0); 
  17.     if (bRet == NO_ERROR) 
  18.     { 
  19.         if (m_pMIT->dwNumEntries <= 1) 
  20.         { 
  21.             //bResult = false; 
  22.         } 
  23.         else 
  24.         { 
  25.             __int64 i64TotalOutOctets = 0; 
  26.             __int64 i64TotalInOctets = 0; 
  27.             //多網卡 
  28.             for(int i=0; i<(m_pMIT->dwNumEntries); i++) 
  29.             { 
  30.                 if (m_pMIT->table[i].dwType <= 23) 
  31.                 { 
  32.                     //当前上传 
  33.                     i64TotalOutOctets += m_pMIT->table[i].dwOutOctets; 
  34.                     //当前下载 
  35.                     i64TotalInOctets += m_pMIT->table[i].dwInOctets; 
  36.                     //bResult = mit.table[i].dwOperStatus; 
  37.                     //if (bResult) 
  38.                     { 
  39.                         //return true; 
  40.                     } 
  41.                 } 
  42.             } 
  43.             memset(szVal,0,sizeof(szVal)); 
  44.             if(m_oldUploadOctets != 0 && (i64TotalOutOctets > m_oldUploadOctets)) 
  45.                 m_curUploadOctets = i64TotalOutOctets - m_oldUploadOctets; 
  46.             m_oldUploadOctets = i64TotalOutOctets; 
  47.             lengthToStr(m_curUploadOctets * 8,szVal); 
  48.             m_CapabilityDlgObj.GetDlgItem(IDC_STA_NETUPSPEED)->SetWindowText(szVal); 
  49.             //下载 
  50.             memset(szVal,0,sizeof(szVal)); 
  51.             if(m_oldDownloadOctets != 0 && (i64TotalInOctets > m_oldDownloadOctets)) 
  52.                 m_curDownloadOctets = i64TotalInOctets - m_oldDownloadOctets; 
  53.             m_oldDownloadOctets = i64TotalInOctets; 
  54.             lengthToStr(m_curDownloadOctets * 8,szVal); 
  55.             m_CapabilityDlgObj.GetDlgItem(IDC_STA_NETDNSPEED)->SetWindowText(szVal); 
  56.         } 
  57.     } 

还有个wincap 的例子

  1. #include "stdafx.h" 
  2. #include <stdlib.h> 
  3. #include <stdio.h> 
  4.  
  5. #include "include/pcap.h" 
  6.  
  7. #pragma comment(lib,"lib/Packet.lib") 
  8. #pragma comment(lib,"lib/wpcap.lib") 
  9.  
  10. void usage(); 
  11.  
  12. void dispatcher_handler(u_char *,
    const struct pcap_pkthdr *,
    const u_char *); 
  13.  
  14. void _tmain(int argc, _TCHAR* argv[]) 
  15. pcap_t *fp; 
  16. char errbuf[PCAP_ERRBUF_SIZE]; 
  17. struct timeval st_ts; 
  18. u_int netmask; 
  19. struct bpf_program fcode; 
  20.    
  21.     /* 检查命令行参数的合法性 */ 
  22.  
  23.          
  24.     /* 打开输出适配器 */ 
  25.     if((fp= pcap_open_live("\\Device\\NPF_{4C280CAF-B56F-467D-91E2-43F8C54B5BD2}"/*我的网卡名*/,100,1,1000,errbuf))==NULL) 
  26.     { 
  27.         fprintf(stderr,"\nUnable to open adapter %s.\n", errbuf); 
  28.         return
  29.     } 
  30.  
  31.     /* 不用关心掩码,在这个过滤器中,它不会被使用 */ 
  32.     netmask=0xffffff;  
  33.  
  34.     // 编译过滤器 
  35.     if (pcap_compile(fp, &fcode,
    "ether proto 0x8864"/*我用的是ADSL这里是设置只接收PPPOE的包*/, 1, netmask) <0 ) 
  36.     { 
  37.         fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax.\n"); 
  38.         /* 释放设备列表 */ 
  39.         return
  40.     } 
  41.      
  42.     //设置过滤器 
  43.     if (pcap_setfilter(fp, &fcode)<0) 
  44.     { 
  45.         fprintf(stderr,"\nError setting the filter.\n"); 
  46.         pcap_close(fp); 
  47.         /* 释放设备列表 */ 
  48.         return
  49.     } 
  50.  
  51.     /* 将接口设置为统计模式 */ 
  52.     if (pcap_setmode(fp, MODE_STAT)<0) 
  53.     { 
  54.         fprintf(stderr,"\nError setting the mode.\n"); 
  55.         pcap_close(fp); 
  56.         /* 释放设备列表 */ 
  57.         return
  58.     } 
  59.  
  60.  
  61.     printf("NetWork traffic summary:\n"); 
  62.  
  63.     /* 开始主循环 */ 
  64.     pcap_loop(fp, 0, dispatcher_handler, (PUCHAR)&st_ts); 
  65.  
  66.     pcap_close(fp); 
  67.     return
  68.  
  69. void dispatcher_handler(u_char *state,
    const struct pcap_pkthdr *header,
    const u_char *pkt_data) 
  70.     struct timeval *old_ts = (struct timeval *)state; 
  71.     u_int delay; 
  72.     LARGE_INTEGER Bps,Pps; 
  73.     struct tm *ltime; 
  74.     char timestr[16]; 
  75.     time_t local_tv_sec; 
  76.  
  77.     /* 以毫秒计算上一次采样的延迟时间 */ 
  78.     /* 这个值通过采样到的时间戳获得 */ 
  79.     delay=(header->ts.tv_sec - old_ts->tv_sec) * 1000000 - old_ts->tv_usec + header->ts.tv_usec; 
  80.     /* 获取每秒的比特数b/s */ 
  81.     Bps.QuadPart=(((*(LONGLONG*)(pkt_data + 8)) * 8 * 1000000) / (delay)); 
  82.     /*                                            ^      ^
  83.                                                   |      |
  84.                                                   |      |
  85.                                                   |      |
  86.                               将字节转换成比特 --   |
  87.                                                          |
  88.                                        延时是以毫秒表示的 --
  89.     */ 
  90.  
  91.     /* 得到每秒的数据包数量 */ 
  92.     Pps.QuadPart=(((*(LONGLONG*)(pkt_data)) * 1000000) / (delay)); 
  93.  
  94.     /* 将时间戳转化为可识别的格式 */ 
  95.     local_tv_sec = header->ts.tv_sec; 
  96.     ltime=localtime(&local_tv_sec); 
  97.     strftime( timestr, sizeof timestr,
    "%H:%M:%S", ltime); 
  98.  
  99.     /* 打印时间戳*/ 
  100.     printf("%s ", timestr); 
  101.  
  102.     /* 打印采样结果 */ 
  103.     printf("BPS=%I64u ", Bps.QuadPart); 
  104.     printf("PPS=%I64u\n", Pps.QuadPart); 
  105.  
  106.     //存储当前的时间戳 
  107.     old_ts->tv_sec=header->ts.tv_sec; 
  108.     old_ts->tv_usec=header->ts.tv_usec; 
  109.  
  110.  
  111. void usage() 
  112.      
  113.     printf("\nShows the TCP traffic load, in bits per second and packets per second.\nCopyright (C) 2002 Loris Degioanni.\n"); 
  114.     printf("\nUsage:\n"); 
  115.     printf("\t tcptop adapter\n"); 
  116.     printf("\t You can use \"WinDump -D\" if you don't know the name of your adapters.\n"); 
  117.  
  118.     exit(0); 

  1. //获得网卡流量信息         
  2. DWORD dwIfBufSize = 0; 
  3.     BOOL bRet; 
  4.  
  5.     if(m_pMIT == NULL) 
  6.         m_pMIT = new MIB_IFTABLE[sizeof(MIB_IFTABLE)]; 
  7.  
  8.     bRet = ::GetIfTable(m_pMIT,&dwIfBufSize,0); 
  9.     if(bRet == ERROR_INSUFFICIENT_BUFFER)//如果内存不足,则重新分配内存大小 
  10.     { 
  11.         if(dwIfBufSize != NULL) 
  12.             delete [] m_pMIT; 
  13.         m_pMIT = new MIB_IFTABLE[dwIfBufSize]; 
  14.     } 
  15.  
  16.     bRet = ::GetIfTable(m_pMIT,&dwIfBufSize,0); 
  17.     if (bRet == NO_ERROR) 
  18.     { 
  19.         if (m_pMIT->dwNumEntries <= 1) 
  20.         { 
  21.             //bResult = false; 
  22.         } 
  23.         else 
  24.         { 
  25.             __int64 i64TotalOutOctets = 0; 
  26.             __int64 i64TotalInOctets = 0; 
  27.             //多網卡 
  28.             for(int i=0; i<(m_pMIT->dwNumEntries); i++) 
  29.             { 
  30.                 if (m_pMIT->table[i].dwType <= 23) 
  31.                 { 
  32.                     //当前上传 
  33.                     i64TotalOutOctets += m_pMIT->table[i].dwOutOctets; 
  34.                     //当前下载 
  35.                     i64TotalInOctets += m_pMIT->table[i].dwInOctets; 
  36.                     //bResult = mit.table[i].dwOperStatus; 
  37.                     //if (bResult) 
  38.                     { 
  39.                         //return true; 
  40.                     } 
  41.                 } 
  42.             } 
  43.             memset(szVal,0,sizeof(szVal)); 
  44.             if(m_oldUploadOctets != 0 && (i64TotalOutOctets > m_oldUploadOctets)) 
  45.                 m_curUploadOctets = i64TotalOutOctets - m_oldUploadOctets; 
  46.             m_oldUploadOctets = i64TotalOutOctets; 
  47.             lengthToStr(m_curUploadOctets * 8,szVal); 
  48.             m_CapabilityDlgObj.GetDlgItem(IDC_STA_NETUPSPEED)->SetWindowText(szVal); 
  49.             //下载 
  50.             memset(szVal,0,sizeof(szVal)); 
  51.             if(m_oldDownloadOctets != 0 && (i64TotalInOctets > m_oldDownloadOctets)) 
  52.                 m_curDownloadOctets = i64TotalInOctets - m_oldDownloadOctets; 
  53.             m_oldDownloadOctets = i64TotalInOctets; 
  54.             lengthToStr(m_curDownloadOctets * 8,szVal); 
  55.             m_CapabilityDlgObj.GetDlgItem(IDC_STA_NETDNSPEED)->SetWindowText(szVal); 
  56.         } 
  57.     } 

还有个wincap 的例子

  1. #include "stdafx.h" 
  2. #include <stdlib.h> 
  3. #include <stdio.h> 
  4.  
  5. #include "include/pcap.h" 
  6.  
  7. #pragma comment(lib,"lib/Packet.lib") 
  8. #pragma comment(lib,"lib/wpcap.lib") 
  9.  
  10. void usage(); 
  11.  
  12. void dispatcher_handler(u_char *,
    const struct pcap_pkthdr *,
    const u_char *); 
  13.  
  14. void _tmain(int argc, _TCHAR* argv[]) 
  15. pcap_t *fp; 
  16. char errbuf[PCAP_ERRBUF_SIZE]; 
  17. struct timeval st_ts; 
  18. u_int netmask; 
  19. struct bpf_program fcode; 
  20.    
  21.     /* 检查命令行参数的合法性 */ 
  22.  
  23.          
  24.     /* 打开输出适配器 */ 
  25.     if((fp= pcap_open_live("\\Device\\NPF_{4C280CAF-B56F-467D-91E2-43F8C54B5BD2}"/*我的网卡名*/,100,1,1000,errbuf))==NULL) 
  26.     { 
  27.         fprintf(stderr,"\nUnable to open adapter %s.\n", errbuf); 
  28.         return
  29.     } 
  30.  
  31.     /* 不用关心掩码,在这个过滤器中,它不会被使用 */ 
  32.     netmask=0xffffff;  
  33.  
  34.     // 编译过滤器 
  35.     if (pcap_compile(fp, &fcode,
    "ether proto 0x8864"/*我用的是ADSL这里是设置只接收PPPOE的包*/, 1, netmask) <0 ) 
  36.     { 
  37.         fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax.\n"); 
  38.         /* 释放设备列表 */ 
  39.         return
  40.     } 
  41.      
  42.     //设置过滤器 
  43.     if (pcap_setfilter(fp, &fcode)<0) 
  44.     { 
  45.         fprintf(stderr,"\nError setting the filter.\n"); 
  46.         pcap_close(fp); 
  47.         /* 释放设备列表 */ 
  48.         return
  49.     } 
  50.  
  51.     /* 将接口设置为统计模式 */ 
  52.     if (pcap_setmode(fp, MODE_STAT)<0) 
  53.     { 
  54.         fprintf(stderr,"\nError setting the mode.\n"); 
  55.         pcap_close(fp); 
  56.         /* 释放设备列表 */ 
  57.         return
  58.     } 
  59.  
  60.  
  61.     printf("NetWork traffic summary:\n"); 
  62.  
  63.     /* 开始主循环 */ 
  64.     pcap_loop(fp, 0, dispatcher_handler, (PUCHAR)&st_ts); 
  65.  
  66.     pcap_close(fp); 
  67.     return
  68.  
  69. void dispatcher_handler(u_char *state,
    const struct pcap_pkthdr *header,
    const u_char *pkt_data) 
  70.     struct timeval *old_ts = (struct timeval *)state; 
  71.     u_int delay; 
  72.     LARGE_INTEGER Bps,Pps; 
  73.     struct tm *ltime; 
  74.     char timestr[16]; 
  75.     time_t local_tv_sec; 
  76.  
  77.     /* 以毫秒计算上一次采样的延迟时间 */ 
  78.     /* 这个值通过采样到的时间戳获得 */ 
  79.     delay=(header->ts.tv_sec - old_ts->tv_sec) * 1000000 - old_ts->tv_usec + header->ts.tv_usec; 
  80.     /* 获取每秒的比特数b/s */ 
  81.     Bps.QuadPart=(((*(LONGLONG*)(pkt_data + 8)) * 8 * 1000000) / (delay)); 
  82.     /*                                            ^      ^
  83.                                                   |      |
  84.                                                   |      |
  85.                                                   |      |
  86.                               将字节转换成比特 --   |
  87.                                                          |
  88.                                        延时是以毫秒表示的 --
  89.     */ 
  90.  
  91.     /* 得到每秒的数据包数量 */ 
  92.     Pps.QuadPart=(((*(LONGLONG*)(pkt_data)) * 1000000) / (delay)); 
  93.  
  94.     /* 将时间戳转化为可识别的格式 */ 
  95.     local_tv_sec = header->ts.tv_sec; 
  96.     ltime=localtime(&local_tv_sec); 
  97.     strftime( timestr, sizeof timestr,
    "%H:%M:%S", ltime); 
  98.  
  99.     /* 打印时间戳*/ 
  100.     printf("%s ", timestr); 
  101.  
  102.     /* 打印采样结果 */ 
  103.     printf("BPS=%I64u ", Bps.QuadPart); 
  104.     printf("PPS=%I64u\n", Pps.QuadPart); 
  105.  
  106.     //存储当前的时间戳 
  107.     old_ts->tv_sec=header->ts.tv_sec; 
  108.     old_ts->tv_usec=header->ts.tv_usec; 
  109.  
  110.  
  111. void usage() 
  112.      
  113.     printf("\nShows the TCP traffic load, in bits per second and packets per second.\nCopyright (C) 2002 Loris Degioanni.\n"); 
  114.     printf("\nUsage:\n"); 
  115.     printf("\t tcptop adapter\n"); 
  116.     printf("\t You can use \"WinDump -D\" if you don't know the name of your adapters.\n"); 
  117.  
  118.     exit(0); 

抱歉!评论已关闭.