c++ 获取进程当前占有的带宽

chao360559040 2012-08-09 09:06:22
c++ 中有提供什么API 直接获取某个进程占有的带宽,我需要实时获取当前进程的带宽占有情况
...全文
593 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
titer1 2012-08-10
  • 打赏
  • 举报
回复
每次看到 360,qq流量监测,其实这个还蛮有技术含量
统计
per process
per pc
复杂度是不一样的。顺便感叹
titer1 2012-08-10
  • 打赏
  • 举报
回复

设置过滤器,多线程,每个线程统计一种协议

winpcap有个统计网络流量的例子,不过它要接受网卡名的输入,我改了一下,直接把我的网卡名放进去,同时设置只统计ADSL的包,供参考


C/C++ code

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>

#include "include/pcap.h"

#pragma comment(lib,"lib/Packet.lib")
#pragma comment(lib,"lib/wpcap.lib")

void usage();

void dispatcher_handler(u_char *, const struct pcap_pkthdr *, const u_char *);

void _tmain(int argc, _TCHAR* argv[])
{
pcap_t *fp;
char errbuf[PCAP_ERRBUF_SIZE];
struct timeval st_ts;
u_int netmask;
struct bpf_program fcode;

/* 检查命令行参数的合法性 */


/* 打开输出适配器 */
if((fp= pcap_open_live("\\Device\\NPF_{4C280CAF-B56F-467D-91E2-43F8C54B5BD2}"/*我的网卡名*/,100,1,1000,errbuf))==NULL)
{
fprintf(stderr,"\nUnable to open adapter %s.\n", errbuf);
return;
}

/* 不用关心掩码,在这个过滤器中,它不会被使用 */
netmask=0xffffff;

// 编译过滤器
if (pcap_compile(fp, &fcode, "ether proto 0x8864"/*我用的是ADSL这里是设置只接收PPPOE的包*/, 1, netmask) <0 )
{
fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax.\n");
/* 释放设备列表 */
return;
}

//设置过滤器
if (pcap_setfilter(fp, &fcode)<0)
{
fprintf(stderr,"\nError setting the filter.\n");
pcap_close(fp);
/* 释放设备列表 */
return;
}

/* 将接口设置为统计模式 */
if (pcap_setmode(fp, MODE_STAT)<0)
{
fprintf(stderr,"\nError setting the mode.\n");
pcap_close(fp);
/* 释放设备列表 */
return;
}


printf("NetWork traffic summary:\n");

/* 开始主循环 */
pcap_loop(fp, 0, dispatcher_handler, (PUCHAR)&st_ts);

pcap_close(fp);
return;
}

void dispatcher_handler(u_char *state, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
struct timeval *old_ts = (struct timeval *)state;
u_int delay;
LARGE_INTEGER Bps,Pps;
struct tm *ltime;
char timestr[16];
time_t local_tv_sec;

/* 以毫秒计算上一次采样的延迟时间 */
/* 这个值通过采样到的时间戳获得 */
delay=(header->ts.tv_sec - old_ts->tv_sec) * 1000000 - old_ts->tv_usec + header->ts.tv_usec;
/* 获取每秒的比特数b/s */
Bps.QuadPart=(((*(LONGLONG*)(pkt_data + 8)) * 8 * 1000000) / (delay));
/* ^ ^
| |
| |
| |
将字节转换成比特 -- |
|
延时是以毫秒表示的 --
*/

/* 得到每秒的数据包数量 */
Pps.QuadPart=(((*(LONGLONG*)(pkt_data)) * 1000000) / (delay));

/* 将时间戳转化为可识别的格式 */
local_tv_sec = header->ts.tv_sec;
ltime=localtime(&local_tv_sec);
strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);

/* 打印时间戳*/
printf("%s ", timestr);

/* 打印采样结果 */
printf("BPS=%I64u ", Bps.QuadPart);
printf("PPS=%I64u\n", Pps.QuadPart);

//存储当前的时间戳
old_ts->tv_sec=header->ts.tv_sec;
old_ts->tv_usec=header->ts.tv_usec;
}


void usage()
{

printf("\nShows the TCP traffic load, in bits per second and packets per second.\nCopyright (C) 2002 Loris Degioanni.\n");
printf("\nUsage:\n");
printf("\t tcptop adapter\n");
printf("\t You can use \"WinDump -D\" if you don't know the name of your adapters.\n");

exit(0);
}
chao360559040 2012-08-10
  • 打赏
  • 举报
回复
喔!是有点小麻烦的 谢谢了哈
titer1 2012-08-09
  • 打赏
  • 举报
回复
Hi,

Based on my knowledge, there a API named Network Monitor which can help you to monitor bandwidth. Network Monitor can helps to monitor network traffic of your machine, each captured frame has corresponding process name, so that you may monitor specified application.

Much more than a GUI application, it also provides APIs for developers to monitor network traffic in their code. After installation, you can find a C/C++ header file , a cs file and a lib library.

Here are the detail about the NMAPI:
1. http://msdn.microsoft.com/en-us/library/ee831989(v=VS.85).aspx
2. http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=4865

Here is a tutorial about NMAPI:
http://channel9.msdn.com/Blogs/MichaelHawker/Network-Monitor-Experts-Day-Part-1-The-Experts-Story

Here are other information about network monitor:
1. http://blogs.technet.com/b/netmon/archive/2009/05/13/event-tracing-for-windows-and-network-monitor.aspx
2. http://msdn.microsoft.com/en-us/library/dd569137(VS.85).aspx

Best Regards,
Rob

---------------------------------来源msdn

---------------
两个函数
---------------
Examples
The following example retrieves the interface table and prints the number of entries in the table and some data on each entry.
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365943.aspx

The GetIpStatistics function returns the statistics for IPv4 on the current computer.
On Windows XP and later, the GetIpStatisticsEx can be used to obtain the IP statistics for either IPv4 or IPv6.
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365959.aspx
-----------------

You can get some information about network utilization with GetIpStatistics.

You can get the rated bandwidths of the installed network adapters with GetIfTable.

64,687

社区成员

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

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