1. 程式人生 > >proc/net/dev實時網速統計實例【轉】

proc/net/dev實時網速統計實例【轉】

特殊 總結 能夠 double 統計 pen ear 原創 關心

轉自:https://blog.csdn.net/dosthing/article/details/80384541

前言

網絡編程是程序連接網絡拓展的基礎,尤其是在物聯網、互聯網加等概念火熱的當下,網絡編程能力體現了一個程序員能否具有大型程序的開發能力。在實際應用中,往往需要顯示目前系統的實時網速等信息,當然獲取網速等信息的軟件方法很多,但是用小幾行代碼,並可移植性好的方法卻不多,這裏介紹如何通過Linux的proc文件系統進行實時獲取網卡收發速率。

原理簡介

Linux提供的LKM機制可以使我們通過proc偽文件系統來獲取Linux內核信息,而通過proc/net/dev我們可以實時獲取網絡適配器及統計信息。拋開復雜的概念,簡單說就是我們可以利用proc/net/dev來獲取網卡的網速及網絡包的收發情況。這裏我們主要關心Receive和Transmit項的bytes項。bytes定義:The total number of bytes of datatransmitted or received by the interface. 即網口的發送或接收的數據的總字節數。更多選項的定義,我們用不著,如有興趣了解戳這裏。

實例詳解

有了以上的背景知識,足夠我們理解實例的內容了,直接上源碼,看看如何實現實時統計網卡的網速信息。

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>

#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <dirent.h>
#include <time.h>
#include <fcntl.h>
#include <errno.h>

#ifdef debugprintf
#define debugpri(mesg, args...) fprintf(stderr, "[NetRate print:%s:%d:] " mesg "\n", __FILE__, __LINE__, ##args)
#else
#define debugpri(mesg, args...)
#endif

int GetNetRate(FILE* fd,char *interface,long *recv,long *send)
{
char buf[1024];
char *p;
char flow[32];
int i = 0;
char tempstr[16][16]={0};

memset(buf,0,sizeof(buf));
memset(tempstr,0,sizeof(tempstr));
fseek(fd, 0, SEEK_SET);
int nBytes = fread(buf,1, sizeof(buf)-1,fd);
if (-1 == nBytes)
{
debugpri("fread error");
fclose(fd);
return -1;
}
buf[nBytes] = ‘\0‘;
char* pDev = strstr(buf, interface);
if (NULL == pDev)
{
printf("don‘t find dev %s\n", interface);
fclose(fd);
return -1;
}
sscanf(pDev,"%[^‘ ‘]\t%[^‘ ‘]\t%[^‘ ‘]\t%[^‘ ‘]\t%[^‘ ‘]\t%[^‘ ‘]\t%[^‘ ‘]\t%[^‘ ‘]\t%[^‘ ‘]\t%[^‘ ‘]\t%[^‘ ‘]\t%[^‘ ‘]\t",\
tempstr[0],tempstr[1],tempstr[2],tempstr[3],tempstr[4],tempstr[5],tempstr[6],tempstr[7],tempstr[8],tempstr[9],tempstr[10],tempstr[11]);
*recv = atol(tempstr[1]);
*send = atol(tempstr[9]);
}
int main(int argc, char** argv)
{
struct timeval tv_now,tv_pre;
char netdevice[16]={0};
int nDevLen;
long recvpre = 0,recvcur = 0;
long sendpre = 0,sendcur = 0;
double sendrate;
double recvrate;

if(argc != 2)
{
printf("Usage: netrate <network device>\n");
exit(0);
}

nDevLen = strlen(argv[1]);
if (nDevLen < 1 || nDevLen > 10)
{
printf("unkown device\n");
exit(0);
}
sprintf(netdevice,"%s",argv[1]);
FILE* fd = fopen("/proc/net/dev","r+");
if (NULL == fd)
{
debugpri("/proc/net/dev not exists!\n");
return -1;
}
while(1)
{
gettimeofday(&tv_pre,NULL);
GetNetRate(fd,netdevice,&recvpre,&sendpre);
sleep(2);
gettimeofday(&tv_now,NULL);
GetNetRate(fd,netdevice,&recvcur,&sendcur);
recvrate= (recvcur - recvpre)/(1024*(tv_now.tv_sec+tv_now.tv_usec*0.000001-tv_pre.tv_sec+tv_pre.tv_usec*0.000001));
if(recvrate<0)
{
recvrate = 0;
}
sendrate= (sendcur - sendpre)/(1024*(tv_now.tv_sec+tv_now.tv_usec*0.000001-tv_pre.tv_sec+tv_pre.tv_usec*0.000001));
if(sendrate<0)
{
sendrate = 0;
}
system("clear");
printf("NetWorkRate Statistic Verson 0.0.1\n");
printf("Net Device receive rate send rate\n");
printf("%-10s\t%-6.2fKB/sec\t%-6.2fKB/sec\n",netdevice,recvrate,recvrate);
}
fclose(fd);
return 0;
}

總結:

物聯網、互聯網加的蓬勃發展,產品的功能實現越來越依賴網絡,掌握一些網絡編程的小技巧,可以使得我們在產品開發中能夠事半功倍,提升軟實力。今天是5月20日,在特殊的日子裏整理一下知識,以做備忘,原創不易,轉載說明文章出處。番外篇,我把上面的實例程序整理成了一個網絡小工具,很實用,在主流的Linux平臺編譯運行沒有任何的warning警告,如需要嵌入式移植開發或者其他用途請點此下載。


---------------------
作者:dosthing
來源:CSDN
原文:https://blog.csdn.net/dosthing/article/details/80384541
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

proc/net/dev實時網速統計實例【轉】