使用 libperf API 并将比特率值存储在变量中时如何抑制终端输出

问题描述

我想使用 libperf 库通过 C++ 应用程序在 Ubuntu Linux 上测量网络速度和质量。
我在互联网上找到了以下示例并且工作正常:

    #include "iperf_config.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #include <sysexits.h>
    #include <stdint.h>
    
    
    #include <iperf_api.h>
    
    int
    main( int argc,char** argv )
    {
        char* argv0;
        char* host;
        int port;
        struct iperf_test *test;
    
        argv0 = strrchr( argv[0],'/' );
        if ( argv0 != (char*) 0 )
        ++argv0;
        else
        argv0 = argv[0];
    
        if ( argc != 3 ) {
        fprintf( stderr,"usage: %s [host] [port]\n",argv0 );
        exit( EXIT_FAILURE );
        }
        host = argv[1];
        port = atoi( argv[2] );
    
        test = iperf_new_test();
        if ( test == NULL ) {
        fprintf( stderr,"%s: Failed to create test\n",argv0 );
        exit( EXIT_FAILURE );
        }
        iperf_defaults( test );
        iperf_set_verbose( test,1 );
    
        iperf_set_test_role( test,'c' );
        iperf_set_test_server_hostname( test,host );
        iperf_set_test_server_port( test,port );
        iperf_set_test_omit( test,3 );
        iperf_set_test_duration( test,5 );
        iperf_set_test_reporter_interval( test,1 );
        iperf_set_test_stats_interval( test,1 );
    
        if ( iperf_run_client( test ) < 0 ) {
        fprintf( stderr,"%s: error - %s\n",argv0,iperf_strerror( i_errno ) );
        exit( EXIT_FAILURE );
        }
    
        iperf_free_test( test );
        exit( EXIT_SUCCESS );
    }

但是我在我的终端上得到了以下所有输出

iperf 3.10.1
Linux user-VirtualBox 4.15.0-142-generic #146~16.04.1-Ubuntu SMP Tue Apr 13 09:27:15 UTC 2021 x86_64
Control connection MSS 1460
Time: Fri,11 Jun 2021 10:23:07 GMT
Connecting to host ping-ams1.online.net,port 5209
      Cookie: xkd7bwss42k6r25zah5h4snec5af3sthyfnw
      TCP MSS: 1460 (default)
[  5] local 10.0.2.15 port 36632 connected to 163.172.208.7 port 5209
Starting Test: protocol: TCP,1 streams,131072 byte blocks,omitting 3 seconds,5 second test,tos 0
[ ID] Interval           Transfer     Bitrate         Retr  Cwnd
[  5]   0.00-1.00   sec   419 KBytes  3.43 Mbits/sec    0   35.6 KBytes       (omitted)
[  5]   1.00-2.00   sec  0.00 Bytes  0.00 bits/sec    0   34.2 KBytes       (omitted)
[  5]   2.00-3.00   sec  94.1 KBytes   771 Kbits/sec    0   32.8 KBytes       (omitted)
[  5]   0.00-1.00   sec  0.00 Bytes  0.00 bits/sec    0   32.8 KBytes       
[  5]   1.00-2.00   sec  0.00 Bytes  0.00 bits/sec    0   32.8 KBytes       
[  5]   2.00-3.00   sec  94.1 KBytes   771 Kbits/sec    0   34.2 KBytes       
[  5]   3.00-4.01   sec   125 KBytes  1.02 Mbits/sec    0   38.5 KBytes       
[  5]   4.01-5.00   sec  94.1 KBytes   780 Kbits/sec    0   41.3 KBytes     

我知道使用 iPerf3 二进制文件可以将输出重定向一个文件,然后解析该文件
由于我对 libperf 完全陌生并且没有找到任何详细的 API 文档,我想问一下如何以编程方式抑制所有终端输出并将比特率值和其他值存储在我的程序中。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)