目录

Collecting Network Statistics

Introduction

Traffic management provides the capability of collecting traffic data based on the physical network, and supports traffic statistics based on NIC/UID.

Its functions include:

  • To maximize the app running efficiency, most API calls are asynchronous. For asynchronous APIs, both callback and promise modes are provided. The following examples use the promise mode. For details about other modes, see @ohos.net.statistics (traffic management).
  • Uplink traffic refers to the amount of data sent from the terminal device to the network side, and downlink traffic refers to the amount of data transmitted from the network side to the terminal device.

The following describes the development procedure specific to each application scenario.

How to Develop

  1. Import the statistics, socket, and error code modules.

    import { socket, statistics } from '@kit.NetworkKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
  2. Get the traffic data of the specified NIC.

    Call getIfaceRxBytes and getIfaceTxBytes separately, and pass the NIC name to get the downlink and uplink traffic data of the specified NIC since the last startup.

      // Obtain the real-time downlink data traffic of the primary Wi-Fi NIC. wlan0 is the name of the primary Wi-Fi NIC.
      statistics.getIfaceRxBytes('wlan0').then((stats: number) => {
        hilog.info(0x0000, 'testTag', JSON.stringify(stats));
        // ...
      })
      .catch((err: BusinessError) => {
        hilog.error(0x0000, 'testTag', JSON.stringify(err));
        // ...
      });
      // ...
      // Obtain the real-time uplink data traffic of the primary Wi-Fi NIC. wlan0 is the name of the primary Wi-Fi NIC.
      statistics.getIfaceTxBytes('wlan0').then((stats: number) => {
        hilog.info(0x0000, 'testTag', JSON.stringify(stats));
        // ...
      })
      .catch((err: BusinessError) => {
        hilog.error(0x0000, 'testTag', JSON.stringify(err));
        // ...
      });
    // ...
  3. Obtain the cellular traffic data.

    Call getCellularRxBytes and getCellularTxBytes separately to obtain the cellular downlink and uplink traffic data since the last startup.

    // Obtain the real-time downlink data traffic of the cellular network.
    statistics.getCellularRxBytes().then((stats: number) => {
      hilog.info(0x0000, 'testTag', JSON.stringify(stats));
      // ...
    })
    // ...
    // Obtain the real-time uplink data traffic of the cellular network.
    statistics.getCellularTxBytes().then((stats: number) => {
      hilog.info(0x0000, 'testTag', JSON.stringify(stats));
      // ...
    })
    // ...
  4. Obtain the traffic data of all NICs.

    Call getAllRxBytes and getAllTxBytes separately to obtain the downlink and uplink traffic data of all NICs since the last startup.

    // Obtain the real-time downlink data traffic of all NICs.
    statistics.getAllRxBytes().then((stats: number) => {
      hilog.info(0x0000, 'testTag', JSON.stringify(stats));
      // ...
    })
    // ...
    // Obtain the real-time uplink data traffic of all NICs.
    statistics.getAllTxBytes().then((stats: number) => {
      hilog.info(0x0000, 'testTag', JSON.stringify(stats));
      // ...
    })
    // ...
  5. Get the traffic data of the specified app.

    Call getUidRxBytes and getUidTxBytes separately, and pass the UID to get the downlink and uplink traffic data of the specified app since the last startup.

    Here, the app UID 20010038 is used as an example. Replace it with the actual UID when calling.

     let UID = 20010038;
    // Obtain the real-time downlink data traffic of the specified application.
    // ...
    statistics.getUidRxBytes(UID).then((stats: number) => {
      hilog.info(0x0000, 'testTag', JSON.stringify(stats));
      // ...
    })
    // ...
    // Obtain the real-time uplink data traffic of the specified application.
    // ...
    statistics.getUidTxBytes(UID).then((stats: number) => {
      hilog.info(0x0000, 'testTag', JSON.stringify(stats));
      // ...
    })
    // ...
  6. Obtain the traffic data of the specified socket.

    Call getSockfdRxBytes and getSockfdTxBytes separately, and pass the socket FD to get the downlink and uplink traffic data of the specified socket.

    // Obtain the real-time downlink data traffic of the specified socket.
    let tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
    // ...
    tcp.getSocketFd().then((sockfd: number) => {
      statistics.getSockfdRxBytes(sockfd).then((stats: number) => {
        hilog.info(0x0000, 'testTag', JSON.stringify(stats));
        // ...
      }).catch((err: BusinessError) => {
        hilog.error(0x0000, 'testTag', JSON.stringify(err));
        // ...
      });
    })
    // ...
    // Obtain the real-time uplink data traffic of the specified socket.
    tcp.getSocketFd().then((sockfd: number) => {
      statistics.getSockfdTxBytes(sockfd).then((stats: number) => {
        hilog.info(0x0000, 'testTag', JSON.stringify(stats));
        // ...
      }).catch((err: BusinessError) => {
        hilog.error(0x0000, 'testTag', JSON.stringify(err));
        // ...
      });
    })
    // ...