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