栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

以编程方式获取链接速度?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

以编程方式获取链接速度?

您需要使用

SIOCETHTOOL
ioctl()调用。在LinuxJournal上对ioctl
/
SIOCETHTOOL

调用进行了很好的介绍,下面的代码(不旨在成为C良好实践的例子!)应向您展示如何使用它来提高速度。

#include <stdio.h>#include <sys/socket.h>#include <sys/ioctl.h>#include <netinet/in.h>#include <linux/sockios.h>#include <linux/if.h>#include <linux/ethtool.h>#include <string.h>#include <stdlib.h>int main (int argc, char **argv){    int sock;    struct ifreq ifr;    struct ethtool_cmd edata;    int rc;    sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);    if (sock < 0) {        perror("socket");        exit(1);    }    strncpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name));    ifr.ifr_data = &edata;    edata.cmd = ETHTOOL_GSET;    rc = ioctl(sock, SIOCETHTOOL, &ifr);    if (rc < 0) {        perror("ioctl");        exit(1);    }    switch (ethtool_cmd_speed(&edata)) {        case SPEED_10: printf("10Mbpsn"); break;        case SPEED_100: printf("100Mbpsn"); break;        case SPEED_1000: printf("1Gbpsn"); break;        case SPEED_2500: printf("2.5Gbpsn"); break;        case SPEED_10000: printf("10Gbpsn"); break;        default: printf("Speed returned is %dn", edata.speed);    }    return (0);}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/388071.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号