/*
* =====================================================================================
*
* Filename: net_speed.c
*
* Description:
*
* Version: 1.0
* Created: 08/16/2010 09:27:03 PM
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Company:
*
* =====================================================================================
*/
#include <stdio.h>
int main()
{
FILE *fp;
char buf[1024];
unsigned int old = 0, new = 0;
fp = fopen("/sys/class/net/eth0/statistics/rx_bytes", "rb");
if (fp == NULL)
return -1;
fread(buf, 1, sizeof(buf), fp);
sscanf(buf, "%u", &old);
fclose(fp);
while(1)
{
fp = fopen("/sys/class/net/eth0/statistics/rx_bytes", "rb");
fread(buf, 1, sizeof(buf), fp);
fclose(fp);
sscanf(buf, "%u", &new);
printf("net speed: %-u KB/s/n", (new - old)/1000);
old = new;
sleep(1);
}
return 0;
}
要靠打开文件来计算,有点慢阿,不知道有没有别的办法呢。
计算网络速度
最新推荐文章于 2024-04-21 17:43:41 发布