Linux C下获取下载和上传的网速V2

/*
* 作者:杨志永
* 日期:2012-04-17 10:10 
* Email:ljy520zhiyong@163.com
* QQ:929168233
* 
* 文件名: watch_network_speed.c
* 编译环境:Debian 6.0.4 Testing, GCC 4.6.3 X86_64
* 
* 功能:获取Linux系统下的下载和上传的网速
*
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>

#define BUFFER 1024
#define SECOND 1
int get_net_work_download_speed(long int * download_speed, long int * upload_speed, char * download_type, char * upload_type);

int main(int argc, char * argv[])
{
    long int start_download_speed;
    long int end_download_speed;
    long int start_upload_speed;
    long int end_upload_speed;

    while (1)
    {
        get_net_work_download_speed(&start_download_speed, &start_upload_speed, "RX bytes:", "TX bytes:");
        sleep(SECOND);
        get_net_work_download_speed(&end_download_speed, &end_upload_speed, "RX bytes:", "TX bytes:");
        printf("下载速度: %.2lf KB/s\t", (float)(end_download_speed-start_download_speed)/(SECOND*1000));
        printf("上传速度: %.2lf KB/s\n", (float)(end_upload_speed-start_upload_speed)/(SECOND*1000));
    }
    exit(EXIT_SUCCESS);
}

int get_net_work_download_speed(long int * download_speed, long int * upload_speed, char * download_type, char * upload_type)
{
    FILE * pipo_stream;
    size_t bytes_read;

    char buffer[BUFFER];
    char * match;

    if ( (pipo_stream=popen("sudo ifconfig", "r")) == NULL )
    {
        printf("pipo error!\n");
        return -1;
    }

    bytes_read = fread(buffer, 1, sizeof(buffer), pipo_stream);

    if ( (fclose(pipo_stream)) != 0 )
    {
        printf("fclose error!\n");
        return -1;
    }

    if ( bytes_read == 0 )
    {
        printf("bytes_read == 0\n");
        return -1;
    }

    match = strstr(buffer, download_type);

    if (match == NULL)
    {
        printf("NO Keyword %s To Find!\n", download_type);
        return -1;
    }

    sscanf(match, "RX bytes:%ld", download_speed);

    match = strstr(buffer, upload_type);
    if (match == NULL)
    {
        printf("No Keyword %s To Find!\n", upload_type);
        return -1;
    }
    sscanf(match, "TX bytes:%ld", upload_speed);
    return 0;

}


Linux环境下,获取eth0网卡的实时流量速度(通常称为带宽使用情况或吞吐量)并不直接提供一个命令行工具,但是你可以使用一些第三方工具或者通过内核提供的接口来获取。这里介绍几种常见的方法: **使用ifstat或iftop** 1. **iftop**: 这是一个交互式的带宽监控工具,可以在终端中显示网络接口的实时流量数据。安装后,运行`iftop -i eth0`可以看到eth0网卡的速度信息。 ```sh sudo apt-get install iftop # 对于基于Debian的系统 sudo yum install iftop # 对于基于RPM的系统 iftop -i eth0 ``` 2. **ifstat** (仅适用于某些发行版):这是一个简洁的带宽监视工具,可以用`ifstat -a | grep eth0`查看eth0接口的统计信息。 ```sh ifstat -a | grep eth0 ``` **使用ethtool** ethtool是一个用于查询、设置监控网卡性能的工具,虽然它本身不直接显示速率,但你可以借助它获得统计数据然后计算平均速率。 ```sh ethtool -S eth0 ``` 然后分析输出中的RX/TX bytes等信息。 **内核命令行参数** 还可以使用`/proc/net/dev`目录下的文件,这是内核提供的接口,需要稍微解析一下: ```sh cat /proc/net/dev | grep eth0 | awk '{print $9+$10 " MBit/s"}' ``` 请注意,这些方法提供的都是估算值,并非严格的实时流速,实际带宽可能会因为网络拥塞、应用负载等因素有所不同。如果你需要非常精确的数据,可能需要部署专门的网络监控工具或者服务。
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值