[英]How to get current running ethernet speed from LabWindows\CVI C code or CMD commands
我正在開發具有幾個以太網端口的測試設備。 作為測試的一部分,我想在連接被測設備時檢查以太網端口的當前速度(10/100/1000)。
我如何獲得此信息? 我可以使用可以提供此信息的C庫或CMD命令嗎?
與使用c / c ++進行API訪問相比,C#的建立和運行速度可能更快。
嘗試使用System.Net.NetworkInformation類。 特別是, System.Net.NetworkInformation.IPv4InterfaceStatistics
應該具有與您所尋找內容System.Net.NetworkInformation.IPv4InterfaceStatistics
信息。
具體來說,您可以檢查bytesReceived
屬性,等待給定的時間間隔,然后再次檢查bytesReceived
屬性以了解連接正在處理多少字節/秒。 但是,要獲得一個好的數字,您應該嘗試從給定的源下載大量信息,然后進行檢查; 這樣一來,您在進行測試時就應該“最大化”連接,這將提供更多有用的數字。
您需要一台Linux運行機器才能使代碼正常工作。您需要在Linux中使用SIOCETHTOOL ioctl()
調用。
#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("10Mbps\n"); break;
case SPEED_100: printf("100Mbps\n"); break;
case SPEED_1000: printf("1Gbps\n"); break;
default: printf("Speed returned is %d\n", edata.speed);
}
return (0);
}
我不確定Windows。可能您可以在這里參考: Microsoft Developer Network
使用這樣的wmic
命令
wmic PATH Win32_NetworkAdapter WHERE "PhysicalAdapter = TRUE AND NetEnabled = TRUE" GET Name, Speed
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.