• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.net.statistics (Traffic Management)
2
3The **statistics** module provides APIs to obtain the real-time uplink and downlink data traffic of the specified NIC.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9## Modules to Import
10
11```js
12import { statistics } from '@kit.NetworkKit';
13```
14
15## statistics.getIfaceRxBytes<sup>10+</sup>
16
17getIfaceRxBytes(nic: string, callback: AsyncCallback\<number>): void;
18
19Obtains the real-time downlink data traffic of the specified NIC. This API uses an asynchronous callback to return the result.
20
21**System capability**: SystemCapability.Communication.NetManager.Core
22
23**Parameters**
24
25| Name  | Type                  | Mandatory| Description                                                                                                                   |
26| -------- | ---------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------- |
27| nic      | string                 | Yes  | NIC name.                                                                                                     |
28| callback | AsyncCallback\<number> | Yes  | Callback used to return the result. If the operation is successful, **error** is **undefined** and **stats** is the real-time downlink data traffic of the NIC in bytes. Otherwise, **error** is an error object.   |
29
30**Error codes**
31
32For details about the error codes, see [Traffic Management Error Codes](errorcode-net-statistics.md).
33
34| ID| Error Message                                    |
35| --------- | -------------------------------------------- |
36| 401       | Parameter error.                             |
37| 2100002   | Failed to connect to the service.            |
38| 2100003   | System internal error.                       |
39| 2103005   | Failed to read the system map.               |
40| 2103011   | Failed to create a system map.               |
41| 2103012   | Failed to obtain the NIC name.               |
42
43**Example**
44
45```js
46import { BusinessError } from '@kit.BasicServicesKit';
47import { statistics } from '@kit.NetworkKit';
48
49statistics.getIfaceRxBytes("wlan0", (error: BusinessError, stats: number) => {
50  console.log(JSON.stringify(error));
51  console.log(JSON.stringify(stats));
52});
53```
54
55## statistics.getIfaceRxBytes<sup>10+</sup>
56
57getIfaceRxBytes(nic: string): Promise\<number>;
58
59Obtains the real-time downlink data traffic of the specified NIC. This API uses a promise to return the result.
60
61**System capability**: SystemCapability.Communication.NetManager.Core
62
63**Parameters**
64
65| Name| Type  | Mandatory| Description              |
66| ------ | ------ | ---- | ------------------ |
67| nic    | string | Yes  | NIC name.|
68
69**Return value**
70| Type| Description|
71| -------- | -------- |
72| Promise\<number> | Promise used to return the result, which is the real-time downlink data traffic of the NIC in bytes.|
73
74**Error codes**
75
76For details about the error codes, see [Traffic Management Error Codes](errorcode-net-statistics.md).
77
78| ID| Error Message                                    |
79| --------- | -------------------------------------------- |
80| 401       | Parameter error.                             |
81| 2100002   | Failed to connect to the service.            |
82| 2100003   | System internal error.                       |
83| 2103005   | Failed to read the system map.               |
84| 2103011   | Failed to create a system map.               |
85| 2103012   | Failed to obtain the NIC name.               |
86
87**Example**
88
89```js
90import { statistics } from '@kit.NetworkKit';
91
92statistics.getIfaceRxBytes("wlan0").then((stats: number) => {
93  console.log(JSON.stringify(stats));
94});
95```
96
97## statistics.getIfaceTxBytes<sup>10+</sup>
98
99getIfaceTxBytes(nic: string, callback: AsyncCallback\<number>): void;
100
101Obtains the real-time uplink data traffic of the specified NIC. This API uses an asynchronous callback to return the result.
102
103**System capability**: SystemCapability.Communication.NetManager.Core
104
105**Parameters**
106
107| Name  | Type                  | Mandatory| Description                                                                                                                   |
108| -------- | ---------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------- |
109| nic      | string                 | Yes  | NIC name.                                                                                                     |
110| callback | AsyncCallback\<number> | Yes  | Callback used to return the result. If the operation is successful, **error** is **undefined** and **stats** is the real-time uplink data traffic of the NIC in bytes. Otherwise, **error** is an error object.   |
111
112**Error codes**
113
114For details about the error codes, see [Traffic Management Error Codes](errorcode-net-statistics.md).
115
116| ID| Error Message                                    |
117| --------- | -------------------------------------------- |
118| 401       | Parameter error.                             |
119| 2100002   | Failed to connect to the service.            |
120| 2100003   | System internal error.                       |
121| 2103005   | Failed to read the system map.               |
122| 2103011   | Failed to create a system map.               |
123| 2103012   | Failed to obtain the NIC name.               |
124
125**Example**
126
127```js
128import { BusinessError } from '@kit.BasicServicesKit';
129import { statistics } from '@kit.NetworkKit';
130
131statistics.getIfaceTxBytes("wlan0", (error: BusinessError, stats: number) => {
132  console.log(JSON.stringify(error));
133  console.log(JSON.stringify(stats));
134});
135```
136
137## statistics.getIfaceTxBytes<sup>10+</sup>
138
139getIfaceTxBytes(nic: string): Promise\<number>;
140
141Obtains the real-time uplink data traffic of the specified NIC. This API uses a promise to return the result.
142
143**System capability**: SystemCapability.Communication.NetManager.Core
144
145**Parameters**
146
147| Name| Type  | Mandatory| Description              |
148| ------ | ------ | ---- | ------------------ |
149| nic    | string | Yes  | NIC name.|
150
151**Return value**
152| Type| Description|
153| -------- | -------- |
154| Promise\<number> | Promise used to return the result, which is the real-time uplink data traffic of the NIC in bytes.|
155
156**Error codes**
157
158For details about the error codes, see [Traffic Management Error Codes](errorcode-net-statistics.md).
159
160| ID| Error Message                                    |
161| --------- | -------------------------------------------- |
162| 401       | Parameter error.                             |
163| 2100002   | Failed to connect to the service.            |
164| 2100003   | System internal error.                       |
165| 2103005   | Failed to read the system map.               |
166| 2103011   | Failed to create a system map.               |
167| 2103012   | Failed to obtain the NIC name.               |
168
169**Example**
170
171```js
172import { statistics } from '@kit.NetworkKit';
173
174statistics.getIfaceTxBytes("wlan0").then((stats: number) => {
175  console.log(JSON.stringify(stats));
176});
177```
178
179## statistics.getCellularRxBytes<sup>10+</sup>
180
181getCellularRxBytes(callback: AsyncCallback\<number>): void;
182
183Obtains the real-time downlink data traffic of a cellular network. This API uses an asynchronous callback to return the result.
184
185**System capability**: SystemCapability.Communication.NetManager.Core
186
187**Parameters**
188
189| Name  | Type                  | Mandatory| Description                                                                                                                   |
190| -------- | ---------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------- |
191| callback | AsyncCallback\<number> | Yes  | Callback used to return the result. If the operation is successful, **error** is **undefined** and **stats** is the real-time downlink data traffic of the cellular network in bytes. Otherwise, **error** is an error object.   |
192
193**Error codes**
194
195For details about the error codes, see [Traffic Management Error Codes](errorcode-net-statistics.md).
196
197| ID| Error Message                                    |
198| --------- | -------------------------------------------- |
199| 2100002   | Failed to connect to the service.            |
200| 2100003   | System internal error.                       |
201| 2103005   | Failed to read the system map.               |
202| 2103011   | Failed to create a system map.               |
203| 2103012   | Failed to obtain the NIC name.               |
204
205**Example**
206
207```js
208import { BusinessError } from '@kit.BasicServicesKit';
209import { statistics } from '@kit.NetworkKit';
210
211statistics.getCellularRxBytes((error: BusinessError, stats: number) => {
212  console.log(JSON.stringify(error));
213  console.log(JSON.stringify(stats));
214});
215```
216
217## statistics.getCellularRxBytes<sup>10+</sup>
218
219getCellularRxBytes(): Promise\<number>;
220
221Obtains the real-time downlink data traffic of a cellular network. This API uses a promise to return the result.
222
223**System capability**: SystemCapability.Communication.NetManager.Core
224
225**Return value**
226| Type| Description|
227| -------- | -------- |
228| Promise\<number> | Promise used to return the result, which is the real-time downlink data traffic of the cellular network in bytes.|
229
230**Error codes**
231
232For details about the error codes, see [Traffic Management Error Codes](errorcode-net-statistics.md).
233
234| ID| Error Message                                    |
235| --------- | -------------------------------------------- |
236| 2100002   | Failed to connect to the service.            |
237| 2100003   | System internal error.                       |
238| 2103005   | Failed to read the system map.               |
239| 2103011   | Failed to create a system map.               |
240| 2103012   | Failed to obtain the NIC name.               |
241
242**Example**
243
244```js
245import { statistics } from '@kit.NetworkKit';
246
247statistics.getCellularRxBytes().then((stats: number) => {
248  console.log(JSON.stringify(stats));
249});
250```
251
252## statistics.getCellularTxBytes<sup>10+</sup>
253
254getCellularTxBytes(callback: AsyncCallback\<number>): void;
255
256Obtains the real-time uplink data traffic of a cellular network. This API uses an asynchronous callback to return the result.
257
258**System capability**: SystemCapability.Communication.NetManager.Core
259
260**Parameters**
261
262| Name  | Type                  | Mandatory| Description                                                                                                                   |
263| -------- | ---------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------- |
264| callback | AsyncCallback\<number> | Yes  | Callback used to return the result. If the operation is successful, **error** is **undefined** and **stats** is the real-time uplink data traffic of the cellular network in bytes. Otherwise, **error** is an error object.   |
265
266**Error codes**
267
268For details about the error codes, see [Traffic Management Error Codes](errorcode-net-statistics.md).
269
270| ID| Error Message                                    |
271| --------- | -------------------------------------------- |
272| 2100002   | Failed to connect to the service.            |
273| 2100003   | System internal error.                       |
274| 2103005   | Failed to read the system map.               |
275| 2103011   | Failed to create a system map.               |
276| 2103012   | Failed to obtain the NIC name.               |
277
278**Example**
279
280```js
281import { BusinessError } from '@kit.BasicServicesKit';
282import { statistics } from '@kit.NetworkKit';
283
284statistics.getCellularTxBytes((error: BusinessError, stats: number) => {
285  console.log(JSON.stringify(error));
286  console.log(JSON.stringify(stats));
287});
288```
289
290## statistics.getCellularTxBytes<sup>10+</sup>
291
292getCellularTxBytes(): Promise\<number>;
293
294Obtains the real-time uplink data traffic of a cellular network. This API uses a promise to return the result.
295
296**System capability**: SystemCapability.Communication.NetManager.Core
297
298**Return value**
299| Type| Description|
300| -------- | -------- |
301| Promise\<number> | Promise used to return the result, which is the real-time uplink data traffic of the cellular network in bytes.|
302
303**Error codes**
304
305For details about the error codes, see [Traffic Management Error Codes](errorcode-net-statistics.md).
306
307| ID| Error Message                                    |
308| --------- | -------------------------------------------- |
309| 2100002   | Failed to connect to the service.            |
310| 2100003   | System internal error.                       |
311| 2103005   | Failed to read the system map.               |
312| 2103011   | Failed to create a system map.               |
313| 2103012   | Failed to obtain the NIC name.               |
314
315**Example**
316
317```js
318import { statistics } from '@kit.NetworkKit';
319
320statistics.getCellularTxBytes().then((stats: number) => {
321  console.log(JSON.stringify(stats));
322});
323```
324
325## statistics.getAllRxBytes<sup>10+</sup>
326
327getAllRxBytes(callback: AsyncCallback\<number>): void;
328
329Obtains the real-time downlink data traffic of all NICs. This API uses an asynchronous callback to return the result.
330
331**System capability**: SystemCapability.Communication.NetManager.Core
332
333**Parameters**
334
335| Name  | Type                  | Mandatory| Description                                                                                                                         |
336| -------- | ---------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- |
337| callback | AsyncCallback\<number> | Yes  | Callback used to return the result. If the operation is successful, **error** is **undefined** and **stats** is the real-time downlink data traffic of all NICs in bytes. Otherwise, **error** is an error object.   |
338
339**Error codes**
340
341For details about the error codes, see [Traffic Management Error Codes](errorcode-net-statistics.md).
342
343| ID| Error Message                                    |
344| --------- | -------------------------------------------- |
345| 2100002   | Failed to connect to the service. |
346| 2100003   | System internal error.                       |
347| 2103005   | Failed to read the system map.               |
348| 2103011   | Failed to create a system map.               |
349
350**Example**
351
352```js
353import { statistics } from '@kit.NetworkKit';
354import { BusinessError } from '@kit.BasicServicesKit';
355
356statistics.getAllRxBytes((error: BusinessError, stats: number) => {
357  console.log(JSON.stringify(error));
358  console.log(JSON.stringify(stats));
359});
360```
361
362## statistics.getAllRxBytes<sup>10+</sup>
363
364getAllRxBytes(): Promise\<number>;
365
366Obtains the real-time downlink data traffic of all NICs. This API uses a promise to return the result.
367
368**System capability**: SystemCapability.Communication.NetManager.Core
369
370**Return value**
371| Type| Description|
372| -------- | -------- |
373| Promise\<number> | Promise used to return the result, which is the real-time downlink data traffic of all NICs in bytes.|
374
375**Error codes**
376
377For details about the error codes, see [Traffic Management Error Codes](errorcode-net-statistics.md).
378
379| ID| Error Message                                    |
380| --------- | -------------------------------------------- |
381| 2100002   | Failed to connect to the service.            |
382| 2100003   | System internal error.                       |
383| 2103005   | Failed to read the system map.               |
384| 2103011   | Failed to create a system map.               |
385
386**Example**
387
388```js
389import { statistics } from '@kit.NetworkKit';
390
391statistics.getAllRxBytes().then((stats: number) => {
392  console.log(JSON.stringify(stats));
393});
394```
395
396## statistics.getAllTxBytes<sup>10+</sup>
397
398getAllTxBytes(callback: AsyncCallback\<number>): void;
399
400Obtains the real-time uplink data traffic of all NICs. This API uses an asynchronous callback to return the result.
401
402**System capability**: SystemCapability.Communication.NetManager.Core
403
404**Parameters**
405
406| Name  | Type                  | Mandatory| Description                                                                                                                         |
407| -------- | ---------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- |
408| callback | AsyncCallback\<number> | Yes  | Callback used to return the result. If the operation is successful, **error** is **undefined** and **stats** is the real-time uplink data traffic of all NICs in bytes. Otherwise, **error** is an error object.   |
409
410**Error codes**
411
412For details about the error codes, see [Traffic Management Error Codes](errorcode-net-statistics.md).
413
414| ID| Error Message                                    |
415| --------- | -------------------------------------------- |
416| 2100002   | Failed to connect to the service.            |
417| 2100003   | System internal error.                       |
418| 2103005   | Failed to read the system map.               |
419| 2103011   | Failed to create a system map.               |
420
421**Example**
422
423```js
424import { BusinessError } from '@kit.BasicServicesKit';
425import { statistics } from '@kit.NetworkKit';
426
427statistics.getAllTxBytes((error: BusinessError, stats: number) => {
428  console.log(JSON.stringify(error));
429  console.log(JSON.stringify(stats));
430});
431```
432
433## statistics.getAllTxBytes<sup>10+</sup>
434
435getAllTxBytes(): Promise\<number>;
436
437Obtains the real-time uplink data traffic of all NICs. This API uses a promise to return the result.
438
439**System capability**: SystemCapability.Communication.NetManager.Core
440
441**Return value**
442| Type| Description|
443| -------- | -------- |
444| Promise\<number> | Promise used to return the result, which is the real-time uplink data traffic of all NICs in bytes.|
445
446**Error codes**
447
448For details about the error codes, see [Traffic Management Error Codes](errorcode-net-statistics.md).
449
450| ID| Error Message                                    |
451| --------- | -------------------------------------------- |
452| 2100002   | Failed to connect to the service.            |
453| 2100003   | System internal error.                       |
454| 2103005   | Failed to read the system map.               |
455| 2103011   | Failed to create a system map.               |
456
457**Example**
458
459```js
460import { statistics } from '@kit.NetworkKit';
461
462statistics.getAllTxBytes().then((stats: number) => {
463  console.log(JSON.stringify(stats));
464});
465```
466
467## statistics.getUidRxBytes<sup>10+</sup>
468
469getUidRxBytes(uid: number, callback: AsyncCallback\<number>): void;
470
471Obtains the real-time downlink data traffic of the specified application. This API uses an asynchronous callback to return the result.
472
473**System capability**: SystemCapability.Communication.NetManager.Core
474
475**Parameters**
476
477| Name  | Type                  | Mandatory| Description                                                                                                                   |
478| -------- | ---------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------- |
479| uid      | number                 | Yes  | Application UID.                                                                                                   |
480| callback | AsyncCallback\<number> | Yes  | Callback used to return the result. If the operation is successful, **error** is **undefined** and **stats** is the real-time downlink data traffic of the application in bytes. Otherwise, **error** is an error object.   |
481
482**Error codes**
483
484For details about the error codes, see [Traffic Management Error Codes](errorcode-net-statistics.md).
485
486| ID| Error Message                                    |
487| --------- | -------------------------------------------- |
488| 401       | Parameter error.                             |
489| 2100002   | Failed to connect to the service.            |
490| 2100003   | System internal error.                       |
491| 2103005   | Failed to read the system map.               |
492| 2103011   | Failed to create a system map.               |
493
494**Example**
495
496```js
497import { BusinessError } from '@kit.BasicServicesKit';
498import { statistics } from '@kit.NetworkKit';
499
500statistics.getUidRxBytes(20010038, (error: BusinessError, stats: number) => {
501  console.log(JSON.stringify(error));
502  console.log(JSON.stringify(stats));
503});
504```
505
506## statistics.getUidRxBytes<sup>10+</sup>
507
508getUidRxBytes(uid: number): Promise\<number>;
509
510Obtains the real-time downlink data traffic of the specified application. This API uses a promise to return the result.
511
512**System capability**: SystemCapability.Communication.NetManager.Core
513
514**Parameters**
515
516| Name| Type  | Mandatory| Description                |
517| ------ | ------ | ---- | -------------------- |
518| uid    | number | Yes  | Application UID.|
519
520**Return value**
521| Type| Description|
522| -------- | -------- |
523| Promise\<number> | Promise used to return the result, which is the real-time downlink data traffic of the application in bytes.|
524
525**Error codes**
526
527For details about the error codes, see [Traffic Management Error Codes](errorcode-net-statistics.md).
528
529| ID| Error Message                                    |
530| --------- | -------------------------------------------- |
531| 401       | Parameter error.                             |
532| 2100002   | Failed to connect to the service.            |
533| 2100003   | System internal error.                       |
534| 2103005   | Failed to read the system map.               |
535| 2103011   | Failed to create a system map.               |
536
537**Example**
538
539```js
540import { statistics } from '@kit.NetworkKit';
541
542statistics.getUidRxBytes(20010038).then((stats: number) => {
543  console.log(JSON.stringify(stats));
544});
545```
546
547## statistics.getUidTxBytes<sup>10+</sup>
548
549getUidTxBytes(uid: number, callback: AsyncCallback\<number>): void;
550
551Obtains the real-time uplink data traffic of the specified application. This API uses an asynchronous callback to return the result.
552
553**System capability**: SystemCapability.Communication.NetManager.Core
554
555**Parameters**
556
557| Name  | Type                  | Mandatory| Description                                                                                                                   |
558| -------- | ---------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------- |
559| uid      | number                 | Yes  | Application UID.                                                                                                   |
560| callback | AsyncCallback\<number> | Yes  | Callback used to return the result. If the operation is successful, **error** is **undefined** and **stats** is the real-time uplink data traffic of the application in bytes. Otherwise, **error** is an error object.   |
561
562**Error codes**
563
564For details about the error codes, see [Traffic Management Error Codes](errorcode-net-statistics.md).
565
566| ID| Error Message                                    |
567| --------- | -------------------------------------------- |
568| 401       | Parameter error.                             |
569| 2100002   | Failed to connect to the service.            |
570| 2100003   | System internal error.                       |
571| 2103005   | Failed to read the system map.               |
572| 2103011   | Failed to create a system map.               |
573
574**Example**
575
576```js
577import { BusinessError } from '@kit.BasicServicesKit';
578import { statistics } from '@kit.NetworkKit';
579
580statistics.getUidTxBytes(20010038, (error: BusinessError, stats: number) => {
581  console.log(JSON.stringify(error));
582  console.log(JSON.stringify(stats));
583});
584```
585
586## statistics.getUidTxBytes<sup>10+</sup>
587
588getUidTxBytes(uid: number): Promise\<number>;
589
590Obtains the real-time uplink data traffic of the specified application. This API uses a promise to return the result.
591
592**System capability**: SystemCapability.Communication.NetManager.Core
593
594**Parameters**
595
596| Name| Type  | Mandatory| Description                |
597| ------ | ------ | ---- | -------------------- |
598| uid    | number | Yes  | Application UID.|
599
600**Return value**
601| Type| Description|
602| -------- | -------- |
603| Promise\<number> | Promise used to return the result, which is the real-time uplink data traffic of the application in bytes.|
604
605**Error codes**
606
607For details about the error codes, see [Traffic Management Error Codes](errorcode-net-statistics.md).
608
609| ID| Error Message                                    |
610| --------- | -------------------------------------------- |
611| 401       | Parameter error.                             |
612| 2100002   | Failed to connect to the service.            |
613| 2100003   | System internal error.                       |
614| 2103005   | Failed to read the system map.               |
615| 2103011   | Failed to create a system map.               |
616
617**Example**
618
619```js
620import { statistics } from '@kit.NetworkKit';
621
622statistics.getUidTxBytes(20010038).then((stats: number) => {
623  console.log(JSON.stringify(stats));
624});
625```
626
627
628## statistics.getSockfdRxBytes<sup>11+</sup>
629
630getSockfdRxBytes(sockfd: number, callback: AsyncCallback\<number\>): void;
631
632Obtains the downlink data traffic (in bytes) of the specified socket. This API uses an asynchronous callback to return the result.
633
634**System capability**: SystemCapability.Communication.NetManager.Core
635
636**Parameters**
637
638| Name  | Type                  | Mandatory| Description                                                        |
639| -------- | ---------------------- | ---- | ------------------------------------------------------------ |
640| sockfd   | number                 | Yes  | FD of the socket.                    |
641| callback | AsyncCallback\<number> | Yes  | Callback used to return the result. Callback used to return the result. If the operation is successful, **error** is **undefined** and **stats** is the downlink data traffic of the socket. Otherwise, **error** is an error object.|
642
643**Error codes**
644
645For details about the error codes, see [Traffic Management Error Codes](errorcode-net-statistics.md).
646
647| ID| Error Message                                    |
648| --------- | -------------------------------------------- |
649| 401       | Parameter error.                             |
650| 2100001   | Invalid parameter value.                     |
651| 2100002   | Failed to connect to the service.            |
652| 2100003   | System internal error.                       |
653
654**Example**
655
656```js
657import { BusinessError } from '@kit.BasicServicesKit';
658import { statistics } from '@kit.NetworkKit';
659
660let sockfd = 50; // FD of the socket you created.
661statistics.getSockfdRxBytes(sockfd, (error: BusinessError, stats: number) => {
662  console.log(JSON.stringify(error));
663  console.log(JSON.stringify(stats));
664});
665```
666
667## statistics.getSockfdRxBytes<sup>11+</sup>
668
669getSockfdRxBytes(sockfd: number): Promise\<number\>;
670
671Obtains the downlink data traffic (in bytes) of the specified socket. This API uses a promise to return the result.
672
673**System capability**: SystemCapability.Communication.NetManager.Core
674
675**Parameters**
676
677| Name| Type  | Mandatory| Description                                    |
678| ------ | ------ | ---- | ---------------------------------------- |
679| sockfd | number | Yes  | FD of the socket.|
680
681**Return value**
682
683| Type            | Description                                                        |
684| ---------------- | ------------------------------------------------------------ |
685| Promise\<number> | Promise used to return the result.|
686
687**Error codes**
688
689For details about the error codes, see [Traffic Management Error Codes](errorcode-net-statistics.md).
690
691| ID| Error Message                                    |
692| --------- | -------------------------------------------- |
693| 401       | Parameter error.                             |
694| 2100001   | Invalid parameter value.                     |
695| 2100002   | Failed to connect to the service.            |
696| 2100003   | System internal error.                       |
697
698**Example**
699
700```js
701import { BusinessError } from '@kit.BasicServicesKit';
702import { statistics } from '@kit.NetworkKit';
703
704let sockfd = 50; // FD of the socket you created.
705statistics.getSockfdRxBytes(sockfd).then((stats: number) => {
706  console.log(JSON.stringify(stats));
707}).catch((err: BusinessError) => {
708  console.error(JSON.stringify(err));
709});
710```
711
712## statistics.getSockfdTxBytes<sup>11+</sup>
713
714getSockfdTxBytes(sockfd: number, callback: AsyncCallback\<number\>): void;
715
716Obtains the uplink data traffic (in bytes) of the specified socket. This API uses an asynchronous callback to return the result.
717
718**System capability**: SystemCapability.Communication.NetManager.Core
719
720**Parameters**
721
722| Name  | Type                  | Mandatory| Description                                                        |
723| -------- | ---------------------- | ---- | ------------------------------------------------------------ |
724| sockfd   | number                 | Yes  | FD of the socket.                    |
725| callback | AsyncCallback\<number> | Yes  | Callback used to return the result. Callback used to return the result. If the operation is successful, **error** is **undefined** and **stats** is the uplink data traffic of the socket. Otherwise, **error** is an error object.|
726
727**Error codes**
728
729For details about the error codes, see [Traffic Management Error Codes](errorcode-net-statistics.md).
730
731| ID| Error Message                                    |
732| --------- | -------------------------------------------- |
733| 401       | Parameter error.                             |
734| 2100001   | Invalid parameter value.                     |
735| 2100002   | Failed to connect to the service.            |
736| 2100003   | System internal error.                       |
737
738**Example**
739
740```js
741import { BusinessError } from '@kit.BasicServicesKit';
742import { statistics } from '@kit.NetworkKit';
743
744let sockfd = 50; // FD of the socket you created.
745statistics.getSockfdTxBytes(sockfd, (error: BusinessError, stats: number) => {
746  console.log(JSON.stringify(error));
747  console.log(JSON.stringify(stats));
748});
749```
750
751## statistics.getSockfdTxBytes<sup>11+</sup>
752
753getSockfdTxBytes(sockfd: number): Promise\<number\>;
754
755Obtains the uplink data traffic (in bytes) of the specified socket. This API uses a promise to return the result.
756
757**System capability**: SystemCapability.Communication.NetManager.Core
758
759**Parameters**
760
761| Name| Type  | Mandatory| Description                                    |
762| ------ | ------ | ---- | ---------------------------------------- |
763| sockfd | number | Yes  | FD of the socket.|
764
765**Return value**
766
767| Type            | Description                                                        |
768| ---------------- | ------------------------------------------------------------ |
769| Promise\<number> | Promise used to return the result.|
770
771**Error codes**
772
773For details about the error codes, see [Traffic Management Error Codes](errorcode-net-statistics.md).
774
775| ID| Error Message                                    |
776| --------- | -------------------------------------------- |
777| 401       | Parameter error.                             |
778| 2100001   | Invalid parameter value.                     |
779| 2100002   | Failed to connect to the service.            |
780| 2100003   | System internal error.                       |
781
782**Example**
783
784```js
785import { BusinessError } from '@kit.BasicServicesKit';
786import { statistics } from '@kit.NetworkKit';
787
788let sockfd = 50; // FD of the socket you created.
789statistics.getSockfdTxBytes(sockfd).then((stats: number) => {
790  console.log(JSON.stringify(stats));
791}).catch((err: BusinessError) => {
792  console.error(JSON.stringify(err));
793});
794```
795
796## NetBearType<sup>12+</sup>
797
798type NetBearType = connection.NetBearType
799
800Enumerates network types.
801
802**System capability**: SystemCapability.Communication.NetManager
803
804|       Type      |            Description            |
805| ---------------- | --------------------------- |
806| [connection.NetBearType](js-apis-net-connection.md#netbeartype) | Network type.   |
807