linux 下如何查看本机的网络速率

linux 下如何查看本机的网络速率,第1张

使用以下2个命令可以查看:

1)查看网卡型号

lspci

|

grep

Ethernet

这个命令可以查看你的网卡设备型号,根据型号就知道是什么性能了。

2)查看网卡实际通讯速率 dmesg

|

grep

eth0

这个命令可以列出网卡工作速率。看到

Up

1000Mps

full

duplex

...

#include <time.h>

#include "tspeed.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() ----- */

----------------------------------------------------------------------------

#include "tspeed.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_listifaifa=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

}

------------------------------------------

#ifndef __TSPEED_H__

#define __TSPEED_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

------------------------

纯手打 代码都给你了


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7433712.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-05
下一篇 2023-04-05

发表评论

登录后才能评论

评论列表(0条)

保存