-1

I am developing Test Equipment which has couple of Ethernet ports. As part of the testing i want to check the current speed of the ethernet ports (10/100/1000) when a tested unit is connected.

How can i get this information? is there a C library or CMD command which i can use that can supply this information?

Raz
  • 489
  • 3
  • 8
  • 17

2 Answers2

2

C# is probably quicker to get up and running than API access with c/c++.

Try using the System.Net.NetworkInformation classes. In particular, System.Net.NetworkInformation.IPv4InterfaceStatistics ought to have some information along the lines of what you're looking for.

Specifically, you can check the bytesReceived property, wait a given interval, and then check the bytesReceived property again to get an idea of how many bytes/second your connection is processing. To get a good number, though, you should try to download a large block of information from a given source, and check then; that way you should be 'maxing' the connection when you do the test, which should give more helpful numbers.

You need a linux running machine for the code to work You need to use the SIOCETHTOOL ioctl() call in Linux.

#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);
}

I'm not sure about the Windows .May be you can refer here: Microsoft Developer Network

PSN
  • 2,326
  • 3
  • 27
  • 52
  • I cant use .net , i am restricted to C – Raz Dec 25 '15 at 07:47
  • 1
    After changing the signature for main() to `int main( void )` so as to get a clean compile, I ran the code on my linux computer. It printed `100Mps` which was the correct value. I did this without trying to 'stuff' the I/O via upload or download of a large file – user3629249 Dec 25 '15 at 23:54
1

use wmic command like this

wmic PATH Win32_NetworkAdapter WHERE "PhysicalAdapter = TRUE AND NetEnabled = TRUE" GET Name, Speed

Win32_NetworkAdapter classof WMI

BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70