Monitoring Linux Network Interface Usage in C / C ++

I am in a situation where I am limited by limited bandwidth and must use most of the bandwidth to transmit one type of measurement data. Sometimes I will send a lot of measurement data, and in other cases I will just wait for events (all this is via a TCP socket).

I would like to be able to transfer a complete data capture file (different from measurements) in the background at a speed that is inversely proportional to the number of measurements that I send back.

I am looking for a way to track how many bytes are sent through a network interface, much like the Ubuntu system monitor. The source code for the system monitor depends on the gnome libraries, and since my program is on an embedded device, I would like to reduce the number of external libraries that I use. Does anyone know a way to do this in C / C ++ without many additional libraries in the standard Linux distribution?

+3
source share
3 answers

One of the easiest ways is to parse the file: / proc / net / dev

The mine contains:

Inter-|   Receive                                                |  Transmit
 face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
    lo:   44865    1431    0    0    0     0          0         0    44865    1431    0    0    0     0       0          0
  eth0:150117850  313734    0    0    0     0          0         0 34347178  271210    0    0    0     0       0          0
  pan0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0

Then you can write a parser that uses nothing but C / C ++ libraries.

+3
source

NetLink RTNetLink, struct net_device_stats

+1

Bytes transmitted and received through /sys/class/net/eth0/statistics/tx_bytesand /sys/class/net/eth0/statistics/rx_bytes.

$ cat /sys/class/net/net1/statistics/rx_bytes 
1055448
+1
source

Source: https://habr.com/ru/post/1716063/


All Articles