本文提供两种获取实时网速的方法:
一、Nload命令
Nload是Linux的一款网络流量实时监控软件,可以通过 sudo apt-get install nload 来安装。
输入命令nload:
显示说明:
默认第一行是网卡的名称及IP信息,使用键盘上的左右键可以切换网卡。
默认上边Incoming是进入网卡的流量;
默认下边Outgoing是网卡出去的流量;
默认右边(Curr当前流量)、(Avg平均流量)、(Min最小流量)、(Max最大流量)、(Ttl流量统计);
默认情况,统计数据的左边会使用显示流量图,用#号拼出来的,根据实时流量变化显示。
参数及其帮助:
nload –help
-a:这个好像是全部数据的刷新时间周期,单位是秒,默认是300.
-i:进入网卡的流量图的显示比例最大值设置,默认10240 kBit/s.
-m:不显示流量图,只显示统计数据。
-o:出去网卡的流量图的显示比例最大值设置,默认10240 kBit/s.
-t:显示数据的刷新时间间隔,单位是毫秒,默认500。
-u:设置右边Curr、Avg、Min、Max的数据单位,默认是自动变的.注意大小写单位不同!
h|b|k|m|g h: auto, b: Bit/s, k: kBit/s, m: MBit/s etc.
H|B|K|M|G H: auto, B: Byte/s, K: kByte/s, M: MByte/s etc.
-U:设置右边Ttl的数据单位,默认是自动变的.注意大小写单位不同(与-u相同)!
Devices:自定义监控的网卡,默认是全部监控的,使用左右键切换。
如只监控eth0命令: nload eth0
二、网卡操作
net_speed.c
#include "net_speed.h"
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 == 0)
continue;
ret = strcmp(ifa->ifa_name,ndev->ifi_name);
if(ret == 0){
ifd = (struct if_data *)ifa->ifa_data;
ndev->ifi_ibytes = ifd->ifi_ibytes;
ndev->ifi_obytes = ifd->ifi_obytes;
break;
}
}
freeifaddrs(ifa_list);
end:
return (ret ? -1 : 0);
}
int get_if_speed(struct if_speed *ndev)
{
assert(ndev);
struct if_info *p1=NULL,*p2=NULL;
p1 = (struct if_info *)malloc(sizeof(struct if_info));
p2 = (struct if_info *)malloc(sizeof(struct if_info));
bzero(p1,sizeof(struct if_info));
bzero(p2,sizeof(struct if_info));
strncpy(p1->ifi_name,ndev->ifs_name,strlen(ndev->ifs_name));
strncpy(p2->ifi_name,ndev->ifs_name,strlen(ndev->ifs_name));
int ret = 0;
ret = get_if_dbytes(p1);
if(ret < 0) goto end;
usleep(ndev->ifs_us);
ret = get_if_dbytes(p2);
if(ret < 0) goto end;
ndev->ifs_ispeed = p2->ifi_ibytes - p1->ifi_ibytes;
ndev->ifs_ospeed = p2->ifi_obytes - p1->ifi_obytes;
end:
free(p1);
free(p2);
return 0;
}
net_speed.h
#ifndef __NET_SPEED_H__
#define __NET_SPEED_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <error.h>
#include <ifaddrs.h>
#include <net/if.h> /* for ifconf */
#include <linux/sockios.h> /* for net status mask */
#include <netinet/in.h> /* for sockaddr_in */
#include <sys/socket.h>
#include <sys/ioctl.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;
};
int get_if_dbytes(struct if_info *ndev);
int get_if_speed(struct if_speed *ndev);
#endif
main.c
#include "net_peed.h"
int main ()
{
struct if_speed ndev;
int ret = 0;
bzero(&ndev,sizeof(ndev));
sprintf(ndev.ifs_name,"eth0");
ndev.ifs_us = 100000;
printf("Get %s Speed",ndev.ifs_name);
#if 1
ret = get_if_speed(&ndev);
if(ret < 0)
printf("[Fail]\n");
else
printf("[OK]\n");
float ispeed ,ospeed;
while(1){
time_t *timep = malloc(sizeof(*timep));
time(timep);
char *s = ctime(timep);
ispeed = ndev.ifs_ispeed * 1.0/(ndev.ifs_us/1000 * 0.001);
ospeed = ndev.ifs_ospeed * 1.0/(ndev.ifs_us/1000 * 0.001);
#if 0
printf("%s: Up Speed: %fMB/s || Down Speed: %fMB/s\r",
ndev.ifs_name,ispeed/(1024.0*1024.0),ospeed/(1024.0*1024.0));
#endif
// sleep(3);
#if 1
printf("*****************************************************\n");
printf("*****************************************\n");
printf("***************************\n");
printf("*************\n");
printf("Time is %s || Down Speed: %fKB/s\n",s,ospeed/(1024.0));
#endif
get_if_speed(&ndev);
}
#endif
return 0;
} /* ----- End of main() ----- */
Makefile
#cc=arm-none-linux-gnueabi-gcc
cc=gcc
target=main
obj=main.o net_speed.o
$(target):$(obj)
$(cc) $(obj) -Wall -o $(target)
main.o:main.c
$(cc) -c main.c
net_speed.o:net_speed.c
$(cc) -c net_speed.c
clean:
rm -f $(target) *.elf *.gdb *.o
编译执行:
make
./main
结果:
总结:上述两个方法都可获取实时网速,在移动端的开发比如安卓应用下开发网速应用程序也是类似。