首页 > 代码库 > 【Linux环境编程】获取网卡的实时网速
【Linux环境编程】获取网卡的实时网速
在windows下面,我们可以看到360或者是qq安全卫士的“安全球”,上面显示实时的网速情况。那么在linux里面如何获取网卡的实时网速?其实原理很简单,读取需要获取网速的网卡在某段时间dT内流量的变化dL,那么实时网速就出来了,Speed = dL / dt。
linux在ifaddrs.h中提供了函数:
/* Create a linked list of `struct ifaddrs' structures, one for each network interface on the host machine. If successful, store the list in *IFAP and return 0. On errors, return -1 and set `errno'. The storage returned in *IFAP is allocated dynamically and can only be properly freed by passing it to `freeifaddrs'. */ extern int getifaddrs (struct ifaddrs **__ifap) __THROW; /* Reclaim the storage allocated by a previous `getifaddrs' call. */ extern void freeifaddrs (struct ifaddrs *__ifa) __THROW;
系统会创建一个包含本机所有网卡信息链表,然后我们就可以在这个链表里面获取我们想要的信息。
/* The `getifaddrs' function generates a linked list of these structures. Each element of the list describes one network interface. */ struct ifaddrs { struct ifaddrs *ifa_next; /* Pointer to the next structure. */ char *ifa_name; /* Name of this network interface. */ unsigned int ifa_flags; /* Flags as from SIOCGIFFLAGS ioctl. */ struct sockaddr *ifa_addr; /* Network address of this interface. */ struct sockaddr *ifa_netmask; /* Netmask of this interface. */ union { /* At most one of the following two is valid. If the IFF_BROADCAST bit is set in `ifa_flags', then `ifa_broadaddr' is valid. If the IFF_POINTOPOINT bit is set, then `ifa_dstaddr' is valid. It is never the case that both these bits are set at once. */ struct sockaddr *ifu_broadaddr; /* Broadcast address of this interface. */ struct sockaddr *ifu_dstaddr; /* Point-to-point destination address. */ } ifa_ifu; /* These very same macros are defined by <net/if.h> for `struct ifaddr'. So if they are defined already, the existing definitions will be fine. */ # ifndef ifa_broadaddr # define ifa_broadaddr ifa_ifu.ifu_broadaddr # endif # ifndef ifa_dstaddr # define ifa_dstaddr ifa_ifu.ifu_dstaddr # endif void *ifa_data; /* Address-specific data (may be unused). */ };另外这个链表我们是可以提前用ioctl来筛选的,可以通过ifa_name和ifa_flags来确定ifa_ifu里面到底选用那个union。不过这次我们是来测量实时网速的,不必要关心这个。
我们需要关心的是ifa_data这个项,关于这个项我百度了很多,一直没有发现他到底应该属于哪个结构体的。
后来无意在http://blog.chinaunix.net/uid-22832715-id-292763.html发现有类似的,但是我找不到头文件在那,所以后来干脆我直接把他放到我的头文件里面;
struct if_data{ /* generic interface information */ u_char ifi_type; /* ethernet, tokenring, etc */ u_char ifi_addrlen; /* media address length */ u_char ifi_hdrlen; /* media header length */ u_long ifi_mtu; /* maximum transmission unit */ u_long ifi_metric; /* routing metric (external only) */ u_long ifi_baudrate; /* linespeed */ /* volatile statistics */ u_long ifi_ipackets; /* packets received on interface */ u_long ifi_ierrors; /* input errors on interface */ u_long ifi_opackets; /* packets sent on interface */ u_long ifi_oerrors; /* output errors on interface */ u_long ifi_collisions; /* collisions on csma interfaces */ u_long ifi_ibytes; /* total number of octets received */ u_long ifi_obytes; /* total number of octets sent */ u_long ifi_imcasts; /* packets received via multicast */ u_long ifi_omcasts; /* packets sent via multicast */ u_long ifi_iqdrops; /* dropped on input, this interface */ u_long ifi_noproto; /* destined for unsupported protocol */ struct timeval ifi_lastchange;/* last updated */ };刚刚开始我就打印了ifi_iobytes,ifi_obytes这两个项,不管我怎么下载和上次文件,这两个量都是0。纠结了我半天,我就直接把所有变量都打印出来,发现ifi_mtu,ifi_metric,ifi_baudrate跟ifconfig eth0输出的数据很像。
[15:12 @ ~/program/netspeed]$ ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:22:15:67:F8:16 inet addr:210.42.158.204 Bcast:210.42.158.255 Mask:255.255.255.0 inet6 addr: fe80::222:15ff:fe67:f816/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:917978 errors:0 dropped:0 overruns:0 frame:0 TX packets:1132894 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:132866544 (126.7 MiB) TX bytes:1250785627 (1.1 GiB) Interrupt:29 Base address:0x4000慢慢的我知道了规律,struct ifaddrs里面的ifa_data前四个字(32位)以此是发送数据包数,接收数据包数,发送字节数,接收字节数。
我就重新调整了struct if_data的结构体,由于后面的数据全为0,我就保留了4项:
struct if_data { /* generic interface information */ u_long ifi_opackets; /* packets sent on interface */ u_long ifi_ipackets; /* packets received on interface */ u_long ifi_obytes; /* total number of octets sent */ u_long ifi_ibytes; /* total number of octets received */ };测试OK。
[15:17 @ ~/program/netspeed]$ ./netspeed Get eth0 Speed [OK] eth0: Up Speed: 1.671066 MB/s || Down Speed: 0.036335 MB/s附上我已经封装好的代码:
int get_if_dbytes(struct if_info* ndev) { assert(ndev); struct ifaddrs *ifa_list = NULL; struct ifaddrs *ifa = NULL; struct if_data *ifd = NULL; int ret = 0; ret = getifaddrs(&ifa_list); if(ret < 0) { perror("Get Interface Address Fail:"); goto end; } for(ifa=ifa_list; ifa; ifa=ifa->ifa_next){ if(!(ifa->ifa_flags & IFF_UP) && !(ifa->ifa_flags & IFF_RUNNING)) continue; if(ifa->ifa_data =http://www.mamicode.com/= 0)>
头文件:#ifndef __TSPEED_H__ #define __TSPEED_H__ #ifdef __cplusplus extern "C" { #endif #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #include <error.h> /* For "open" function */ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> struct if_data { /* generic interface information */ u_long ifi_opackets; /* packets sent on interface */ u_long ifi_ipackets; /* packets received on interface */ u_long ifi_obytes; /* total number of octets sent */ u_long ifi_ibytes; /* total number of octets received */ }; struct if_info { char ifi_name[16]; unsigned long ifi_ibytes; unsigned long ifi_obytes; }; struct if_speed { char ifs_name[16]; unsigned long ifs_ispeed; unsigned long ifs_ospeed; unsigned long ifs_us; }; extern int get_if_dbytes(struct if_info *ndev); extern int get_if_speed(struct if_speed *ndev); #ifdef __cplusplus } #endif #endif测试代码:int main (int argc, char **argv) { struct if_speed ndev; int ret = 0; bzero(&ndev,sizeof(ndev)); sprintf(ndev.ifs_name,"eth0"); ndev.ifs_us = 100000; printf("Get %s Speed"); ret = get_if_speed(&ndev); if(ret < 0) printf("\t\t\t[Fail]\n"); else printf("\t\t\t[OK]\n"); float ispeed ,ospeed; while(1){ ispeed = ndev.ifs_ispeed * 1.0/(ndev.ifs_us/1000 * 0.001); ospeed = ndev.ifs_ospeed * 1.0/(ndev.ifs_us/1000 * 0.001); printf("%s: Up Speed: %f MB/s || Down Speed: %f MB/s \r", ndev.ifs_name,ispeed/(1024.0*1024.0),ospeed/(1024.0*1024.0)); get_if_speed(&ndev); } return 0; } /* ----- End of main() ----- */
可能你有更好的获取网速的办法,求留言指点!【Linux环境编程】获取网卡的实时网速
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。