js-apis-net-connection.md 70.6 KB
Newer Older
S
shawn_he 已提交
1
# @ohos.net.connection (Network Connection Management)
S
shawn_he 已提交
2

S
shawn_he 已提交
3
The network connection management module provides basic network management capabilities. You can obtain the default active data network or the list of all active data networks, enable or disable the airplane mode, and obtain network capability information.
S
shawn_he 已提交
4

S
shawn_he 已提交
5
> **NOTE**
S
shawn_he 已提交
6 7 8 9
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.

## Modules to Import

S
shawn_he 已提交
10
```js
S
shawn_he 已提交
11 12
import connection from '@ohos.net.connection'
```
S
shawn_he 已提交
13

S
shawn_he 已提交
14
## connection.createNetConnection<sup>8+</sup>
S
shawn_he 已提交
15 16 17

createNetConnection(netSpecifier?: NetSpecifier, timeout?: number): NetConnection

S
shawn_he 已提交
18
Creates a **NetConnection** object. **netSpecifier** specifies the network, and **timeout** specifies the timeout duration in ms. **timeout** is configurable only when **netSpecifier** is specified. If neither of them is present, the default network is used.
S
shawn_he 已提交
19 20 21 22 23 24 25

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name      | Type                         | Mandatory| Description                                                        |
| ------------ | ----------------------------- | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
26 27
| netSpecifier | [NetSpecifier](#netspecifier) | No  | Network specifier, which specifies the characteristics of a network. If this parameter is not set or is set to **undefined**, the default network is used.                  |
| timeout      | number                        | No  | Timeout duration for obtaining the network specified by **netSpecifier**. This parameter is valid only when **netSpecifier** is specified. The default value is **0** if **netSpecifier** is **undefined**.|
S
shawn_he 已提交
28 29 30 31 32 33 34 35 36 37

**Return value**

| Type                           | Description                |
| ------------------------------- | -------------------- |
| [NetConnection](#netconnection) | Handle of the network specified by **netSpecifier**.|

**Example**

```js
S
shawn_he 已提交
38
// For the default network, you do not need to pass in parameters.
S
shawn_he 已提交
39 40
let netConnection = connection.createNetConnection()

S
shawn_he 已提交
41
// For the cellular network, you need to pass in related network parameters. If the timeout parameter is not specified, the timeout value is 0 by default.
S
shawn_he 已提交
42
let netConnectionCellular = connection.createNetConnection({
S
shawn_he 已提交
43 44 45
  netCapabilities: {
    bearerTypes: [connection.NetBearType.BEARER_CELLULAR]
  }
S
shawn_he 已提交
46 47
})
```
S
shawn_he 已提交
48

S
shawn_he 已提交
49
## connection.getDefaultNet<sup>8+</sup>
S
shawn_he 已提交
50 51 52

getDefaultNet(callback: AsyncCallback\<NetHandle>): void

S
shawn_he 已提交
53
Obtains the default active data network. This API uses an asynchronous callback to return the result. You can use [getNetCapabilities](#connectiongetnetcapabilities) to obtain information such as the network type and capabilities.
S
shawn_he 已提交
54

S
shawn_he 已提交
55
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
56 57 58 59 60 61 62

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                                   | Mandatory| Description      |
| -------- | --------------------------------------- | ---- | ---------- |
S
shawn_he 已提交
63 64 65 66 67 68 69
| callback | AsyncCallback\<[NetHandle](#nethandle)> | Yes  | Callback used to return the result. If the default activated data network is obtained successfully, err is undefined and data is the default activated data network. Otherwise, err is an error object.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
S
shawn_he 已提交
70
| 401     | Parameter error.             |
S
shawn_he 已提交
71 72
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
73 74 75

**Example**

S
shawn_he 已提交
76
```js
S
shawn_he 已提交
77
connection.getDefaultNet(function (error, data) {
S
shawn_he 已提交
78 79
  console.log(JSON.stringify(error))
  console.log(JSON.stringify(data))
S
shawn_he 已提交
80 81 82
})
```

S
shawn_he 已提交
83
## connection.getDefaultNet<sup>8+</sup>
S
shawn_he 已提交
84 85 86

getDefaultNet(): Promise\<NetHandle>

S
shawn_he 已提交
87
Obtains the default active data network. This API uses a promise to return the result. You can use [getNetCapabilities](#connectiongetnetcapabilities) to obtain information such as the network type and capabilities.
S
shawn_he 已提交
88

S
shawn_he 已提交
89
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
90 91 92

**System capability**: SystemCapability.Communication.NetManager.Core

S
shawn_he 已提交
93
**Return value**
S
shawn_he 已提交
94 95 96 97 98

| Type                             | Description                                 |
| --------------------------------- | ------------------------------------- |
| Promise\<[NetHandle](#nethandle)> | Promise used to return the result.|

S
shawn_he 已提交
99 100 101 102 103
**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
S
shawn_he 已提交
104
| 401     | Parameter error.             |
S
shawn_he 已提交
105 106 107
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

S
shawn_he 已提交
108 109
**Example**

S
shawn_he 已提交
110
```js
S
shawn_he 已提交
111
connection.getDefaultNet().then(function (data) {
S
shawn_he 已提交
112
  console.log(JSON.stringify(data))
S
shawn_he 已提交
113 114 115
})
```

S
shawn_he 已提交
116
## connection.getDefaultNetSync<sup>9+</sup>
S
shawn_he 已提交
117

S
shawn_he 已提交
118
getDefaultNetSync(): NetHandle
S
shawn_he 已提交
119

S
shawn_he 已提交
120
Obtains the default active data network in synchronous mode. You can use [getNetCapabilities](#connectiongetnetcapabilities) to obtain information such as the network type and capabilities.
S
shawn_he 已提交
121 122 123 124 125 126 127 128 129 130 131

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Communication.NetManager.Core

**Return value**

| Type     | Description                              |
| --------- | ---------------------------------- |
| NetHandle | Handle of the default active data network.|

S
shawn_he 已提交
132 133 134 135 136
**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
S
shawn_he 已提交
137
| 401     | Parameter error.             |
S
shawn_he 已提交
138 139 140
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

S
shawn_he 已提交
141 142 143 144 145 146
**Example**

```js
let netHandle = connection.getDefaultNetSync();
```

S
shawn_he 已提交
147
## connection.getGlobalHttpProxy<sup>10+</sup>
S
shawn_he 已提交
148

S
shawn_he 已提交
149
getGlobalHttpProxy(callback: AsyncCallback\<HttpProxy>): void
S
shawn_he 已提交
150

S
shawn_he 已提交
151
Obtains the global HTTP proxy configuration of the network. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
152

S
shawn_he 已提交
153
**System API**: This is a system API.
S
shawn_he 已提交
154

S
shawn_he 已提交
155 156 157 158 159 160 161 162 163 164 165 166
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name   | Type                                                        | Mandatory| Description            |
| --------- | ------------------------------------------------------------ | ---- | ---------------- |
| callback  | AsyncCallback\<[HttpProxy](#httpproxy)> | Yes  | Callback used to return the result. If the global HTTP proxy configuration of the network is obtained successfully, **err** is **undefined** and **data** is the global HTTP proxy configuration. Otherwise, **err** is an error object.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
S
shawn_he 已提交
167 168
| 401     | Parameter error.             |
| 202     | Non-system applications use system APIs.             |
S
shawn_he 已提交
169 170 171 172 173 174
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
S
shawn_he 已提交
175 176 177
connection.getGlobalHttpProxy((error, data) => {
  console.info(JSON.stringify(error));
  console.info(JSON.stringify(data));
S
shawn_he 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
})
```

## connection.getGlobalHttpProxy<sup>10+</sup>

getGlobalHttpProxy(): Promise\<HttpProxy>;

Obtains the global HTTP proxy configuration of the network. This API uses a promise to return the result.

**System API**: This is a system API.

**System capability**: SystemCapability.Communication.NetManager.Core

**Return value**

| Type                             | Description                                 |
| --------------------------------- | ------------------------------------- |
| Promise\<[HttpProxy](#httpproxy)> | Promise used to return the result.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
S
shawn_he 已提交
201 202
| 401     | Parameter error.             |
| 202     | Non-system applications use system APIs.             |
S
shawn_he 已提交
203 204 205 206 207 208 209
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
connection.getGlobalHttpProxy().then((data) => {
S
shawn_he 已提交
210
  console.info(JSON.stringify(data));
S
shawn_he 已提交
211
}).catch(error => {
S
shawn_he 已提交
212
  console.info(JSON.stringify(error));
S
shawn_he 已提交
213 214 215 216 217 218 219 220 221 222 223 224
})
```

## connection.setGlobalHttpProxy<sup>10+</sup>

setGlobalHttpProxy(httpProxy: HttpProxy, callback: AsyncCallback\<void>): void

Sets the global HTTP proxy configuration of the network. This API uses an asynchronous callback to return the result.

**System API**: This is a system API.

**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
S
shawn_he 已提交
225 226 227 228 229

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

S
shawn_he 已提交
230 231 232 233 234 235 236 237 238 239 240
| Name   | Type                                                        | Mandatory| Description            |
| --------- | ------------------------------------------------------------ | ---- | ---------------- |
| httpProxy | [HttpProxy](#httpproxy)                                      | Yes  | Global HTTP proxy configuration of the network.|
| callback  | AsyncCallback\<void> | Yes  | Callback used to return the result. If the global HTTP proxy configuration of the network is set successfully, **err** is **undefined**. Otherwise, **err** is an error object.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
S
shawn_he 已提交
241
| 202     | Non-system applications use system APIs.               |
S
shawn_he 已提交
242 243 244
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
245 246 247

**Example**

S
shawn_he 已提交
248
```js
S
shawn_he 已提交
249
let exclusionStr = "192.168,baidu.com"
S
shawn_he 已提交
250 251
let exclusionArray = exclusionStr.split(',');
let httpProxy = {
S
shawn_he 已提交
252 253 254
  host: "192.168.xx.xxx",
  port: 8080,
  exclusionList: exclusionArray
S
shawn_he 已提交
255
}
S
shawn_he 已提交
256 257
connection.setGlobalHttpProxy(httpProxy, (error) => {
  console.info(JSON.stringify(error));
S
shawn_he 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
});
```

## connection.setGlobalHttpProxy<sup>10+</sup>

setGlobalHttpProxy(httpProxy: HttpProxy): Promise\<void>;

Sets the global HTTP proxy configuration of the network. This API uses a promise to return the result.

**System API**: This is a system API.

**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name   | Type                                                        | Mandatory| Description            |
| --------- | ------------------------------------------------------------ | ---- | ---------------- |
| httpProxy | [HttpProxy](#httpproxy)                                      | Yes  | Global HTTP proxy configuration of the network.|

**Return value**

| Type                                       | Description                         |
| ------------------------------------------- | ----------------------------- |
| Promise\<void> | Promise that returns no value.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
S
shawn_he 已提交
291
| 202     | Non-system applications use system APIs.               |
S
shawn_he 已提交
292 293 294 295 296 297 298
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
S
shawn_he 已提交
299
let exclusionStr = "192.168,baidu.com"
S
shawn_he 已提交
300 301
let exclusionArray = exclusionStr.split(',');
let httpProxy = {
S
shawn_he 已提交
302 303 304
  host: "192.168.xx.xxx",
  port: 8080,
  exclusionList: exclusionArray
S
shawn_he 已提交
305
}
S
shawn_he 已提交
306
connection.setGlobalHttpProxy(httpProxy).then(() => {
S
shawn_he 已提交
307 308 309
  console.info("success");
}).catch(error => {
  console.info(JSON.stringify(error));
S
shawn_he 已提交
310 311 312
})
```

S
shawn_he 已提交
313
## connection.getAppNet<sup>9+</sup>
S
shawn_he 已提交
314

S
shawn_he 已提交
315
getAppNet(callback: AsyncCallback\<NetHandle>): void
S
shawn_he 已提交
316

S
shawn_he 已提交
317
Obtains information about the network bound to an application. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
318

S
shawn_he 已提交
319 320 321 322 323 324 325 326 327 328 329 330
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name   | Type                                                        | Mandatory| Description            |
| --------- | ------------------------------------------------------------ | ---- | ---------------- |
| callback  | AsyncCallback\<[NetHandle](#nethandle)> | Yes  | Callback used to return the result. If information about the network bound to the application is successfully obtained, **err** is **undefined** and **data** is the obtained network information. Otherwise, **err** is an error object.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
S
shawn_he 已提交
331
| 401 | Parameter error.|
S
shawn_he 已提交
332 333 334 335 336 337
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
S
shawn_he 已提交
338 339 340
connection.getAppNet(function (error, data) {
  console.log(JSON.stringify(error))
  console.log(JSON.stringify(data))
S
shawn_he 已提交
341 342 343 344 345 346 347 348
})
```

## connection.getAppNet<sup>9+</sup>

getAppNet(): Promise\<NetHandle>;

Obtains information about the network bound to an application. This API uses a promise to return the result.
S
shawn_he 已提交
349 350 351

**System capability**: SystemCapability.Communication.NetManager.Core

S
shawn_he 已提交
352
**Return value**
S
shawn_he 已提交
353

S
shawn_he 已提交
354 355 356 357 358 359 360 361
| Type                             | Description                                 |
| --------------------------------- | ------------------------------------- |
| Promise\<[NetHandle](#nethandle)> | Promise used to return the result.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
S
shawn_he 已提交
362
| 401 | Parameter error.|
S
shawn_he 已提交
363 364 365 366 367 368 369
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
connection.getAppNet().then((data) => {
S
shawn_he 已提交
370
  console.info(JSON.stringify(data));
S
shawn_he 已提交
371
}).catch(error => {
S
shawn_he 已提交
372
  console.info(JSON.stringify(error));
S
shawn_he 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
})
```

## connection.SetAppNet<sup>9+</sup>

setAppNet(netHandle: NetHandle, callback: AsyncCallback\<void>): void

Binds an application to the specified network, so that the application can access the external network only through this network. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.INTERNET

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name   | Type                                                        | Mandatory| Description            |
| --------- | ------------------------------------------------------------ | ---- | ---------------- |
| netHandle | [NetHandle](#nethandle)                                      | Yes  | Handle of the data network.|
| callback  | AsyncCallback\<void> | Yes  | Callback used to return the result. If the application is successfully bound to the specified network, **err** is **undefined**. Otherwise, **err** is an error object.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
402 403 404

**Example**

S
shawn_he 已提交
405
```js
S
shawn_he 已提交
406
connection.getDefaultNet(function (error, netHandle) {
S
shawn_he 已提交
407 408 409 410
  connection.setAppNet(netHandle, (error, data) => {
    console.log(JSON.stringify(error))
    console.log(JSON.stringify(data))
  });
S
shawn_he 已提交
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
})
```

## connection.SetAppNet<sup>9+</sup>

setAppNet(netHandle: NetHandle): Promise\<void>;

Binds an application to the specified network, so that the application can access the external network only through this network. This API uses a promise to return the result.

**Required permissions**: ohos.permission.INTERNET

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name   | Type                                                        | Mandatory| Description            |
| --------- | ------------------------------------------------------------ | ---- | ---------------- |
| netHandle | [NetHandle](#nethandle)                                      | Yes  | Handle of the data network.|

**Return value**

| Type                                       | Description                         |
| ------------------------------------------- | ----------------------------- |
| Promise\<void> | Promise that returns no value.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
connection.getDefaultNet().then(function (netHandle) {
S
shawn_he 已提交
450 451 452 453 454
  connection.setAppNet(netHandle).then(() => {
    console.log("success")
  }).catch(error => {
    console.log(JSON.stringify(error))
  })
S
shawn_he 已提交
455 456 457
})
```

S
shawn_he 已提交
458
## connection.getAllNets<sup>8+</sup>
S
shawn_he 已提交
459 460 461

getAllNets(callback: AsyncCallback&lt;Array&lt;NetHandle&gt;&gt;): void

S
shawn_he 已提交
462
Obtains the list of all connected networks. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
463

S
shawn_he 已提交
464
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
465 466 467 468

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**
S
shawn_he 已提交
469

S
shawn_he 已提交
470 471
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
S
shawn_he 已提交
472 473 474 475 476 477 478
| callback | AsyncCallback&lt;Array&lt;[NetHandle](#nethandle)&gt;&gt; | Yes| Callback used to return the result. If the list of all connected networks is obtained successfully, **err** is **undefined** and **data** is the list of activated data networks. Otherwise, **err** is an error object.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
S
shawn_he 已提交
479
| 401     | Parameter error.             |
S
shawn_he 已提交
480 481
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
482 483 484

**Example**

S
shawn_he 已提交
485
```js
S
shawn_he 已提交
486
connection.getAllNets(function (error, data) {
S
shawn_he 已提交
487 488
  console.log(JSON.stringify(error))
  console.log(JSON.stringify(data))
S
shawn_he 已提交
489 490 491
});
```

S
shawn_he 已提交
492
## connection.getAllNets<sup>8+</sup>
S
shawn_he 已提交
493 494 495

getAllNets(): Promise&lt;Array&lt;NetHandle&gt;&gt;

S
shawn_he 已提交
496
Obtains the list of all connected networks. This API uses a promise to return the result.
S
shawn_he 已提交
497

S
shawn_he 已提交
498
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
499 500 501

**System capability**: SystemCapability.Communication.NetManager.Core

S
shawn_he 已提交
502 503
**Return value**

S
shawn_he 已提交
504 505 506 507
| Type| Description|
| -------- | -------- |
| Promise&lt;Array&lt;[NetHandle](#nethandle)&gt;&gt; | Promise used to return the result.|

S
shawn_he 已提交
508 509 510 511 512
**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
S
shawn_he 已提交
513
| 401     | Parameter error.             |
S
shawn_he 已提交
514 515 516
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

S
shawn_he 已提交
517 518
**Example**

S
shawn_he 已提交
519
```js
S
shawn_he 已提交
520
connection.getAllNets().then(function (data) {
S
shawn_he 已提交
521
  console.log(JSON.stringify(data))
S
shawn_he 已提交
522 523 524
});
```

S
shawn_he 已提交
525
## connection.getConnectionProperties<sup>8+</sup>
S
shawn_he 已提交
526 527 528

getConnectionProperties(netHandle: NetHandle, callback: AsyncCallback\<ConnectionProperties>): void

S
shawn_he 已提交
529
Obtains connection properties of the network corresponding to the **netHandle**. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
530

S
shawn_he 已提交
531
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
532 533 534 535 536 537 538

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name   | Type                                                        | Mandatory| Description            |
| --------- | ------------------------------------------------------------ | ---- | ---------------- |
S
shawn_he 已提交
539
| netHandle | [NetHandle](#nethandle)                                      | Yes  | Handle of the data network.|
S
shawn_he 已提交
540 541 542 543 544 545 546 547 548 549 550
| callback  | AsyncCallback\<[ConnectionProperties](#connectionproperties)> | Yes  | Callback used to return the result. If the connection properties of the network corresponding to the **netHandle** is obtained successfully, **err** is **undefined** and **data** is the obtained network connection information. Otherwise, **err** is an error object.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
551 552 553

**Example**

S
shawn_he 已提交
554
```js
S
shawn_he 已提交
555
connection.getDefaultNet().then(function (netHandle) {
S
shawn_he 已提交
556 557 558 559
  connection.getConnectionProperties(netHandle, function (error, data) {
    console.log(JSON.stringify(error))
    console.log(JSON.stringify(data))
  })
S
shawn_he 已提交
560 561 562
})
```

S
shawn_he 已提交
563
## connection.getConnectionProperties<sup>8+</sup>
S
shawn_he 已提交
564 565 566

getConnectionProperties(netHandle: NetHandle): Promise\<ConnectionProperties>

S
shawn_he 已提交
567
Obtains connection properties of the network corresponding to the **netHandle**. This API uses a promise to return the result.
S
shawn_he 已提交
568

S
shawn_he 已提交
569
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
570 571 572 573 574 575 576

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name   | Type                   | Mandatory| Description            |
| --------- | ----------------------- | ---- | ---------------- |
S
shawn_he 已提交
577
| netHandle | [NetHandle](#nethandle) | Yes  | Handle of the data network.|
S
shawn_he 已提交
578

S
shawn_he 已提交
579
**Return value**
S
shawn_he 已提交
580 581 582 583 584

| Type                                                   | Description                             |
| ------------------------------------------------------- | --------------------------------- |
| Promise\<[ConnectionProperties](#connectionproperties)> | Promise used to return the result.|

S
shawn_he 已提交
585 586 587 588 589 590 591 592 593 594
**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

S
shawn_he 已提交
595 596
**Example**

S
shawn_he 已提交
597
```js
S
shawn_he 已提交
598
connection.getDefaultNet().then(function (netHandle) {
S
shawn_he 已提交
599 600 601
  connection.getConnectionProperties(netHandle).then(function (data) {
    console.log(JSON.stringify(data))
  })
S
shawn_he 已提交
602 603 604
})
```

S
shawn_he 已提交
605
## connection.getNetCapabilities<sup>8+</sup>
S
shawn_he 已提交
606 607 608

getNetCapabilities(netHandle: NetHandle, callback: AsyncCallback\<NetCapabilities>): void

S
shawn_he 已提交
609
Obtains capability information of the network corresponding to the **netHandle**. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
610

S
shawn_he 已提交
611
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
612 613 614 615 616 617 618

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name   | Type                                               | Mandatory| Description            |
| --------- | --------------------------------------------------- | ---- | ---------------- |
S
shawn_he 已提交
619
| netHandle | [NetHandle](#nethandle)                             | Yes  | Handle of the data network.|
S
shawn_he 已提交
620 621 622 623 624 625 626 627 628 629 630
| callback  | AsyncCallback\<[NetCapabilities](#netcapabilities)> | Yes  | Callback used to return the result. If the capability information of the network corresponding to the **netHandle** is obtained successfully, **err** is **undefined** and **data** is the obtained network capability information. Otherwise, **err** is an error object.      |

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
631 632 633

**Example**

S
shawn_he 已提交
634
```js
S
shawn_he 已提交
635
connection.getDefaultNet().then(function (netHandle) {
S
shawn_he 已提交
636 637 638 639
  connection.getNetCapabilities(netHandle, function (error, data) {
    console.log(JSON.stringify(error))
    console.log(JSON.stringify(data))
  })
S
shawn_he 已提交
640 641 642
})
```

S
shawn_he 已提交
643
## connection.getNetCapabilities<sup>8+</sup>
S
shawn_he 已提交
644 645 646

getNetCapabilities(netHandle: NetHandle): Promise\<NetCapabilities>

S
shawn_he 已提交
647
Obtains capability information of the network corresponding to the **netHandle**. This API uses a promise to return the result.
S
shawn_he 已提交
648

S
shawn_he 已提交
649
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
650 651 652 653 654 655 656

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name   | Type                   | Mandatory| Description            |
| --------- | ----------------------- | ---- | ---------------- |
S
shawn_he 已提交
657
| netHandle | [NetHandle](#nethandle) | Yes  | Handle of the data network.|
S
shawn_he 已提交
658

S
shawn_he 已提交
659
**Return value**
S
shawn_he 已提交
660 661 662 663 664

| Type                                         | Description                             |
| --------------------------------------------- | --------------------------------- |
| Promise\<[NetCapabilities](#netcapabilities)> | Promise used to return the result.|

S
shawn_he 已提交
665 666 667 668 669 670 671 672 673 674
**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

S
shawn_he 已提交
675 676
**Example**

S
shawn_he 已提交
677
```js
S
shawn_he 已提交
678
connection.getDefaultNet().then(function (netHandle) {
S
shawn_he 已提交
679 680 681
  connection.getNetCapabilities(netHandle).then(function (data) {
    console.log(JSON.stringify(data))
  })
S
shawn_he 已提交
682 683 684
})
```

S
shawn_he 已提交
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
## connection.isDefaultNetMetered<sup>9+</sup>

isDefaultNetMetered(callback: AsyncCallback\<boolean>): void

Checks whether the data traffic usage on the current network is metered. This API uses an asynchronous callback to return the result.

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
| callback | AsyncCallback\<boolean> | Yes  | Callback used to return the result. The value **true** indicates the data traffic usage is metered.|

S
shawn_he 已提交
701 702 703 704 705
**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
S
shawn_he 已提交
706
| 401     | Parameter error.               |
S
shawn_he 已提交
707 708 709 710
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**
S
shawn_he 已提交
711 712

```js
S
shawn_he 已提交
713
connection.isDefaultNetMetered(function (error, data) {
S
shawn_he 已提交
714 715
  console.log(JSON.stringify(error))
  console.log('data: ' + data)
S
shawn_he 已提交
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
})
```

## connection.isDefaultNetMetered<sup>9+</sup>

isDefaultNetMetered(): Promise\<boolean>

Checks whether the data traffic usage on the current network is metered. This API uses a promise to return the result.

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Communication.NetManager.Core

**Return value**

| Type             | Description                                           |
| ----------------- | ----------------------------------------------- |
| Promise\<boolean> | Promise used to return the result. The value **true** indicates the data traffic usage is metered.|

S
shawn_he 已提交
735 736 737 738 739
**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
S
shawn_he 已提交
740
| 401     | Parameter error.               |
S
shawn_he 已提交
741 742 743 744 745 746 747
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
connection.isDefaultNetMetered().then(function (data) {
S
shawn_he 已提交
748
  console.log('data: ' + data)
S
shawn_he 已提交
749 750 751
})
```

S
shawn_he 已提交
752
## connection.hasDefaultNet<sup>8+</sup>
S
shawn_he 已提交
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772

hasDefaultNet(callback: AsyncCallback\<boolean>): void

Checks whether the default data network is activated. This API uses an asynchronous callback to return the result. You can use [getDefaultNet](#connectiongetdefaultnet) to obtain the default data network, if any.

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
| callback | AsyncCallback\<boolean> | Yes  | Callback used to return the result. The value **true** indicates the default data network is activated.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
S
shawn_he 已提交
773
| 401     | Parameter error.               |
S
shawn_he 已提交
774 775 776 777 778 779 780
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
connection.hasDefaultNet(function (error, data) {
S
shawn_he 已提交
781 782
  console.log(JSON.stringify(error))
  console.log('data: ' + data)
S
shawn_he 已提交
783 784 785
})
```

S
shawn_he 已提交
786
## connection.hasDefaultNet<sup>8+</sup>
S
shawn_he 已提交
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806

hasDefaultNet(): Promise\<boolean>

Checks whether the default data network is activated. This API uses a promise to return the result. You can use [getDefaultNet](#connectiongetdefaultnet) to obtain the default data network, if any.

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Communication.NetManager.Core

**Return value**

| Type             | Description                                           |
| ----------------- | ----------------------------------------------- |
| Promise\<boolean> | Promise used to return the result. The value **true** indicates that the default data network is activated.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
S
shawn_he 已提交
807
| 401     | Parameter error.               |
S
shawn_he 已提交
808 809 810 811
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**
S
shawn_he 已提交
812 813

```js
S
shawn_he 已提交
814
connection.hasDefaultNet().then(function (data) {
S
shawn_he 已提交
815
  console.log('data: ' + data)
S
shawn_he 已提交
816 817 818
})
```

S
shawn_he 已提交
819
## connection.enableAirplaneMode<sup>8+</sup>
S
shawn_he 已提交
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840

enableAirplaneMode(callback: AsyncCallback\<void>): void

Enables the airplane mode. This API uses an asynchronous callback to return the result.

**System API**: This is a system API.

**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                                             | Mandatory| Description              |
| -------- | ------------------------------------------------- | ---- | ------------------ |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.        |

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
S
shawn_he 已提交
841 842 843
| 201     | Permission denied.             |
| 202     | Non-system applications use system APIs.              |
| 401     | Parameter error.               |
S
shawn_he 已提交
844 845 846 847 848 849 850
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
connection.enableAirplaneMode(function (error) {
S
shawn_he 已提交
851
  console.log(JSON.stringify(error))
S
shawn_he 已提交
852 853 854
})
```

S
shawn_he 已提交
855
## connection.enableAirplaneMode<sup>8+</sup>
S
shawn_he 已提交
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876

enableAirplaneMode(): Promise\<void>

Enables the airplane mode. This API uses a promise to return the result.

**System API**: This is a system API.

**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

**System capability**: SystemCapability.Communication.NetManager.Core

**Return value**

| Type                                       | Description                         |
| ------------------------------------------- | ----------------------------- |
| Promise\<void> | Promise that returns no value.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
S
shawn_he 已提交
877 878 879
| 201     | Permission denied.             |
| 202     | Non-system applications use system APIs.              |
| 401     | Parameter error.               |
S
shawn_he 已提交
880 881 882 883 884 885 886
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
connection.enableAirplaneMode().then(function (error) {
S
shawn_he 已提交
887
  console.log(JSON.stringify(error))
S
shawn_he 已提交
888 889 890
})
```

S
shawn_he 已提交
891
## connection.disableAirplaneMode<sup>8+</sup>
S
shawn_he 已提交
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912

disableAirplaneMode(callback: AsyncCallback\<void>): void

Disables the airplane mode. This API uses an asynchronous callback to return the result.

**System API**: This is a system API.

**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                                             | Mandatory| Description              |
| -------- | ------------------------------------------------- | ---- | ------------------ |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result. If the airplane mode is disabled successfully, **err** is **undefined**. Otherwise, **err** is an error object.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
S
shawn_he 已提交
913 914 915
| 201     | Permission denied.             |
| 202     | Non-system applications use system APIs.              |
| 401     | Parameter error.               |
S
shawn_he 已提交
916 917 918 919 920 921 922
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
connection.disableAirplaneMode(function (error) {
S
shawn_he 已提交
923
  console.log(JSON.stringify(error))
S
shawn_he 已提交
924 925 926
})
```

S
shawn_he 已提交
927
## connection.disableAirplaneMode<sup>8+</sup>
S
shawn_he 已提交
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948

disableAirplaneMode(): Promise\<void>

Disables the airplane mode. This API uses a promise to return the result.

**System API**: This is a system API.

**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

**System capability**: SystemCapability.Communication.NetManager.Core

**Return value**

| Type                                       | Description                         |
| ------------------------------------------- | ----------------------------- |
| Promise\<void> | Promise that returns no value.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
S
shawn_he 已提交
949 950 951
| 201     | Permission denied.             |
| 202     | Non-system applications use system APIs.              |
| 401     | Parameter error.               |
S
shawn_he 已提交
952 953 954 955 956 957 958
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
connection.disableAirplaneMode().then(function (error) {
S
shawn_he 已提交
959
  console.log(JSON.stringify(error))
S
shawn_he 已提交
960 961 962
})
```

S
shawn_he 已提交
963
## connection.reportNetConnected<sup>8+</sup>
S
shawn_he 已提交
964 965 966

reportNetConnected(netHandle: NetHandle, callback: AsyncCallback&lt;void&gt;): void

S
shawn_he 已提交
967
Reports connection of the data network to the network management module. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
968

S
shawn_he 已提交
969
**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET
S
shawn_he 已提交
970 971 972 973

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**
S
shawn_he 已提交
974

S
shawn_he 已提交
975 976 977
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).|
S
shawn_he 已提交
978 979 980 981 982 983 984 985 986 987 988
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result. If the network status is reported successfully, **err** is **undefined**. Otherwise, **err** is an error object.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
989 990 991

**Example**

S
shawn_he 已提交
992
```js
S
shawn_he 已提交
993
connection.getDefaultNet().then(function (netHandle) {
S
shawn_he 已提交
994 995 996
  connection.reportNetConnected(netHandle, function (error) {
    console.log(JSON.stringify(error))
  });
S
shawn_he 已提交
997 998 999
});
```

S
shawn_he 已提交
1000
## connection.reportNetConnected<sup>8+</sup>
S
shawn_he 已提交
1001 1002 1003

reportNetConnected(netHandle: NetHandle): Promise&lt;void&gt;

S
shawn_he 已提交
1004
Reports connection of the data network to the network management module. This API uses a promise to return the result.
S
shawn_he 已提交
1005

S
shawn_he 已提交
1006
**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET
S
shawn_he 已提交
1007 1008 1009 1010

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**
S
shawn_he 已提交
1011

S
shawn_he 已提交
1012 1013 1014 1015
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).|

S
shawn_he 已提交
1016
**Return value**
S
shawn_he 已提交
1017 1018
| Type| Description|
| -------- | -------- |
S
shawn_he 已提交
1019
| Promise&lt;void&gt; | Promise that returns no value.|
S
shawn_he 已提交
1020

S
shawn_he 已提交
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

S
shawn_he 已提交
1031 1032
**Example**

S
shawn_he 已提交
1033
```js
S
shawn_he 已提交
1034
connection.getDefaultNet().then(function (netHandle) {
S
shawn_he 已提交
1035 1036 1037
  connection.reportNetConnected(netHandle).then(function () {
    console.log(`report success`)
  });
S
shawn_he 已提交
1038 1039 1040
});
```

S
shawn_he 已提交
1041
## connection.reportNetDisconnected<sup>8+</sup>
S
shawn_he 已提交
1042 1043 1044

reportNetDisconnected(netHandle: NetHandle, callback: AsyncCallback&lt;void&gt;): void

S
shawn_he 已提交
1045
Reports disconnection of the data network to the network management module. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
1046

S
shawn_he 已提交
1047
**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET
S
shawn_he 已提交
1048 1049 1050 1051

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**
S
shawn_he 已提交
1052

S
shawn_he 已提交
1053 1054 1055
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).|
S
shawn_he 已提交
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result. If the network status is reported successfully, **err** is **undefined**. Otherwise, **err** is an error object.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
1067 1068 1069

**Example**

S
shawn_he 已提交
1070
```js
S
shawn_he 已提交
1071
connection.getDefaultNet().then(function (netHandle) {
S
shawn_he 已提交
1072 1073 1074
  connection.reportNetDisconnected(netHandle, function (error) {
    console.log(JSON.stringify(error))
  });
S
shawn_he 已提交
1075 1076 1077
});
```

S
shawn_he 已提交
1078
## connection.reportNetDisconnected<sup>8+</sup>
S
shawn_he 已提交
1079 1080 1081

reportNetDisconnected(netHandle: NetHandle): Promise&lt;void&gt;

S
shawn_he 已提交
1082
Reports disconnection of the data network to the network management module. This API uses a promise to return the result.
S
shawn_he 已提交
1083

S
shawn_he 已提交
1084
**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET
S
shawn_he 已提交
1085 1086 1087 1088

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**
S
shawn_he 已提交
1089

S
shawn_he 已提交
1090 1091 1092 1093
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).|

S
shawn_he 已提交
1094
**Return value**
S
shawn_he 已提交
1095 1096
| Type| Description|
| -------- | -------- |
S
shawn_he 已提交
1097
| Promise&lt;void&gt; | Promise that returns no value.|
S
shawn_he 已提交
1098

S
shawn_he 已提交
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

S
shawn_he 已提交
1109 1110
**Example**

S
shawn_he 已提交
1111
```js
S
shawn_he 已提交
1112
connection.getDefaultNet().then(function (netHandle) {
S
shawn_he 已提交
1113 1114 1115
  connection.reportNetDisconnected(netHandle).then(function () {
    console.log(`report success`)
  });
S
shawn_he 已提交
1116 1117 1118
});
```

S
shawn_he 已提交
1119
## connection.getAddressesByName<sup>8+</sup>
S
shawn_he 已提交
1120 1121 1122 1123 1124

getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>>): void

Resolves the host name by using the default network to obtain all IP addresses. This API uses an asynchronous callback to return the result.

S
shawn_he 已提交
1125
**Required permissions**: ohos.permission.INTERNET
S
shawn_he 已提交
1126 1127 1128 1129 1130 1131 1132

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                                             | Mandatory| Description              |
| -------- | ------------------------------------------------- | ---- | ------------------ |
S
shawn_he 已提交
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
| host     | string                                            | Yes  | Host name to resolve.|
| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | Yes  | Callback used to return the result. If all IP addresses are successfully obtained, **err** is **undefined**, and **data** is the list of all obtained IP addresses. Otherwise, **err** is an error object.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
1145 1146 1147 1148

**Example**

```js
S
shawn_he 已提交
1149 1150
let host = "xxxx";
connection.getAddressesByName(host, function (error, data) {
S
shawn_he 已提交
1151 1152
  console.log(JSON.stringify(error))
  console.log(JSON.stringify(data))
S
shawn_he 已提交
1153 1154 1155
})
```

S
shawn_he 已提交
1156
## connection.getAddressesByName<sup>8+</sup>
S
shawn_he 已提交
1157

S
shawn_he 已提交
1158
getAddressesByName(host: string): Promise\<Array\<NetAddress>>
S
shawn_he 已提交
1159

S
shawn_he 已提交
1160
Resolves the host name by using the default network to obtain all IP addresses. This API uses a promise to return the result.
S
shawn_he 已提交
1161

S
shawn_he 已提交
1162
**Required permissions**: ohos.permission.INTERNET
S
shawn_he 已提交
1163 1164

**System capability**: SystemCapability.Communication.NetManager.Core
S
shawn_he 已提交
1165

S
shawn_he 已提交
1166 1167 1168 1169 1170
**Parameters**

| Name| Type  | Mandatory| Description              |
| ------ | ------ | ---- | ------------------ |
| host   | string | Yes  | Host name to resolve.|
S
shawn_he 已提交
1171 1172 1173 1174 1175

**Return value**

| Type                                       | Description                         |
| ------------------------------------------- | ----------------------------- |
S
shawn_he 已提交
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
| Promise\<Array\<[NetAddress](#netaddress)>> | Promise used to return the result.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
1187 1188 1189 1190

**Example**

```js
S
shawn_he 已提交
1191 1192
let host = "xxxx";
connection.getAddressesByName(host).then(function (data) {
S
shawn_he 已提交
1193
  console.log(JSON.stringify(data))
S
shawn_he 已提交
1194 1195 1196
})
```

S
shawn_he 已提交
1197
## NetConnection
S
shawn_he 已提交
1198

S
shawn_he 已提交
1199
Represents the network connection handle.
S
shawn_he 已提交
1200

S
shawn_he 已提交
1201 1202 1203 1204 1205
> **NOTE**
> When a device changes to the network connected state, the **netAvailable**, **netCapabilitiesChange**, and **netConnectionPropertiesChange** events will be triggered.
> When a device changes to the network disconnected state, the **netLost** event will be triggered.
> When a device switches from a Wi-Fi network to a cellular network, the **netLost** event will be first triggered to indicate that the Wi-Fi network is lost and then the **netAvaliable** event will be triggered to indicate that the cellular network is available.

S
shawn_he 已提交
1206
### register<sup>8+</sup>
S
shawn_he 已提交
1207

S
shawn_he 已提交
1208
register(callback: AsyncCallback\<void>): void
S
shawn_he 已提交
1209

S
shawn_he 已提交
1210
Registers a listener for network status changes.
S
shawn_he 已提交
1211

S
shawn_he 已提交
1212
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
1213

S
shawn_he 已提交
1214
**System capability**: SystemCapability.Communication.NetManager.Core
S
shawn_he 已提交
1215

S
shawn_he 已提交
1216
**Parameters**
S
shawn_he 已提交
1217

S
shawn_he 已提交
1218 1219 1220
| Name  | Type                | Mandatory| Description      |
| -------- | -------------------- | ---- | ---------- |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result. If a listener for network status changes is registered successfully, **err** is **undefined**. Otherwise, **err** is an error object.|
S
shawn_he 已提交
1221

S
shawn_he 已提交
1222
**Error codes**
S
shawn_he 已提交
1223

S
shawn_he 已提交
1224 1225 1226
| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
S
shawn_he 已提交
1227
| 401     | Parameter error.             |
S
shawn_he 已提交
1228 1229
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
1230
| 2101008 | The same callback exists.     |
S
shawn_he 已提交
1231
| 2101022 | The number of requests exceeded the maximum. |
S
shawn_he 已提交
1232 1233 1234 1235

**Example**

```js
S
shawn_he 已提交
1236
netConnection.register(function (error) {
S
shawn_he 已提交
1237
  console.log(JSON.stringify(error))
S
shawn_he 已提交
1238 1239 1240
})
```

S
shawn_he 已提交
1241
### unregister<sup>8+</sup>
S
shawn_he 已提交
1242

S
shawn_he 已提交
1243
unregister(callback: AsyncCallback\<void>): void
S
shawn_he 已提交
1244

S
shawn_he 已提交
1245
Unregisters the listener for network status changes.
S
shawn_he 已提交
1246 1247 1248 1249 1250

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

S
shawn_he 已提交
1251 1252 1253
| Name  | Type                | Mandatory| Description      |
| -------- | -------------------- | ---- | ---------- |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result. If a listener for network status changes is unregistered successfully, **err** is **undefined**. Otherwise, **err** is an error object.|
S
shawn_he 已提交
1254

S
shawn_he 已提交
1255
**Error codes**
S
shawn_he 已提交
1256

S
shawn_he 已提交
1257 1258
| ID| Error Message                       |
| ------- | -----------------------------  |
S
shawn_he 已提交
1259 1260
| 201 | Permission denied.|
| 401 | Parameter error.         |
S
shawn_he 已提交
1261 1262
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
1263
| 2101007 | The callback is not exists.      |
S
shawn_he 已提交
1264 1265 1266

**Example**

S
shawn_he 已提交
1267
```js
S
shawn_he 已提交
1268
netConnection.unregister(function (error) {
S
shawn_he 已提交
1269
  console.log(JSON.stringify(error))
S
shawn_he 已提交
1270 1271 1272
})
```

S
shawn_he 已提交
1273
### on('netAvailable')<sup>8+</sup>
S
shawn_he 已提交
1274 1275 1276 1277 1278

on(type: 'netAvailable', callback: Callback\<NetHandle>): void

Registers a listener for **netAvailable** events.

S
shawn_he 已提交
1279
**Model restriction**: Before you call this API, make sure tat you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network.
S
shawn_he 已提交
1280

S
shawn_he 已提交
1281 1282 1283 1284 1285 1286
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                              | Mandatory| Description                                                        |
| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
1287 1288
| type     | string                             | Yes  | Event type. The value is fixed to **netAvailable**.<br>**netAvailable**: event indicating that the data network is available.|
| callback | Callback\<[NetHandle](#nethandle)> | Yes  | Callback used to return the network handle.|
S
shawn_he 已提交
1289 1290 1291

**Example**

S
shawn_he 已提交
1292
```js
S
shawn_he 已提交
1293 1294 1295 1296 1297
// Create a NetConnection object.
let netCon = connection.createNetConnection()

// Call register to register a listener.
netCon.register(function (error) {
S
shawn_he 已提交
1298
  console.log(JSON.stringify(error))
S
shawn_he 已提交
1299 1300 1301 1302
})

// Subscribe to netAvailable events. Event notifications can be received only after register is called.
netCon.on('netAvailable', function (data) {
S
shawn_he 已提交
1303
  console.log(JSON.stringify(data))
S
shawn_he 已提交
1304
})
S
shawn_he 已提交
1305 1306 1307

// Call unregister to unregister the listener.
netCon.unregister(function (error) {
S
shawn_he 已提交
1308
  console.log(JSON.stringify(error))
S
shawn_he 已提交
1309
})
S
shawn_he 已提交
1310 1311
```

S
shawn_he 已提交
1312
### on('netBlockStatusChange')<sup>8+</sup>
S
shawn_he 已提交
1313

S
shawn_he 已提交
1314
on(type: 'netBlockStatusChange', callback: Callback&lt;{ netHandle: NetHandle, blocked: boolean }&gt;): void
S
shawn_he 已提交
1315

S
shawn_he 已提交
1316
Registers a listener for **netBlockStatusChange** events. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
1317

S
shawn_he 已提交
1318
**Model restriction**: Before you call this API, make sure that you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network.
S
shawn_he 已提交
1319 1320 1321 1322 1323 1324 1325

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
1326 1327
| type     | string                                                       | Yes  | Event type. The value is fixed to **netBlockStatusChange**.<br>**netBlockStatusChange**: event indicating a change in the network blocking status.|
| callback | Callback&lt;{&nbsp;netHandle:&nbsp;[NetHandle](#nethandle),&nbsp;blocked:&nbsp;boolean&nbsp;}&gt; | Yes  | Callback used to return the network handle (**netHandle**) and network status (**blocked**).|
S
shawn_he 已提交
1328 1329 1330

**Example**

S
shawn_he 已提交
1331
```js
S
shawn_he 已提交
1332 1333 1334 1335 1336
// Create a NetConnection object.
let netCon = connection.createNetConnection()

// Call register to register a listener.
netCon.register(function (error) {
S
shawn_he 已提交
1337
  console.log(JSON.stringify(error))
S
shawn_he 已提交
1338 1339 1340 1341
})

// Subscribe to netBlockStatusChange events. Event notifications can be received only after register is called.
netCon.on('netBlockStatusChange', function (data) {
S
shawn_he 已提交
1342
  console.log(JSON.stringify(data))
S
shawn_he 已提交
1343
})
S
shawn_he 已提交
1344 1345 1346

// Call unregister to unregister the listener.
netCon.unregister(function (error) {
S
shawn_he 已提交
1347
  console.log(JSON.stringify(error))
S
shawn_he 已提交
1348
})
S
shawn_he 已提交
1349 1350
```

S
shawn_he 已提交
1351
### on('netCapabilitiesChange')<sup>8+</sup>
S
shawn_he 已提交
1352

S
shawn_he 已提交
1353
on(type: 'netCapabilitiesChange', callback: Callback<{ netHandle: NetHandle, netCap: NetCapabilities }>): void
S
shawn_he 已提交
1354

S
shawn_he 已提交
1355 1356 1357
Registers a listener for **netCapabilitiesChange** events.

**Model restriction**: Before you call this API, make sure tat you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network.
S
shawn_he 已提交
1358 1359 1360 1361 1362 1363 1364

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
1365 1366
| type     | string                                                       | Yes  | Event type. The value is fixed to **netCapabilitiesChange**.<br>**netCapabilitiesChange**: event indicating that the network capabilities have changed.|
| callback | Callback<{ netHandle: [NetHandle](#nethandle), netCap: [NetCapabilities](#netcapabilities) }> | Yes  | Callback used to return the network handle (**netHandle**) and capability information (**netCap**).|
S
shawn_he 已提交
1367 1368 1369

**Example**

S
shawn_he 已提交
1370
```js
S
shawn_he 已提交
1371 1372 1373 1374 1375
// Create a NetConnection object.
let netCon = connection.createNetConnection()

// Call register to register a listener.
netCon.register(function (error) {
S
shawn_he 已提交
1376
  console.log(JSON.stringify(error))
S
shawn_he 已提交
1377 1378 1379 1380
})

// Subscribe to netCapabilitiesChange events. Event notifications can be received only after register is called.
netCon.on('netCapabilitiesChange', function (data) {
S
shawn_he 已提交
1381
  console.log(JSON.stringify(data))
S
shawn_he 已提交
1382
})
S
shawn_he 已提交
1383 1384 1385

// Call unregister to unregister the listener.
netCon.unregister(function (error) {
S
shawn_he 已提交
1386
  console.log(JSON.stringify(error))
S
shawn_he 已提交
1387
})
S
shawn_he 已提交
1388 1389
```

S
shawn_he 已提交
1390
### on('netConnectionPropertiesChange')<sup>8+</sup>
S
shawn_he 已提交
1391

S
shawn_he 已提交
1392 1393
on(type: 'netConnectionPropertiesChange', callback: Callback<{ netHandle: NetHandle, connectionProperties:
ConnectionProperties }>): void
S
shawn_he 已提交
1394

S
shawn_he 已提交
1395 1396 1397
Registers a listener for **netConnectionPropertiesChange** events.

**Model restriction**: Before you call this API, make sure tat you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network.
S
shawn_he 已提交
1398 1399 1400 1401 1402 1403 1404

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
1405
| type     | string                                                       | Yes  | Event type. The value is fixed to **netConnectionPropertiesChange**.<br>**netConnectionPropertiesChange**: event indicating that network connection properties have changed.|
S
shawn_he 已提交
1406
| callback | Callback<{ netHandle: [NetHandle](#nethandle), connectionProperties: [ConnectionProperties](#connectionproperties) }> | Yes  | Callback used to return the network handle (**netHandle**) and connection information (**connectionProperties**).|
S
shawn_he 已提交
1407 1408 1409

**Example**

S
shawn_he 已提交
1410
```js
S
shawn_he 已提交
1411 1412 1413 1414 1415
// Create a NetConnection object.
let netCon = connection.createNetConnection()

// Call register to register a listener.
netCon.register(function (error) {
S
shawn_he 已提交
1416
  console.log(JSON.stringify(error))
S
shawn_he 已提交
1417 1418 1419 1420
})

// Subscribe to netConnectionPropertiesChange events. Event notifications can be received only after register is called.
netCon.on('netConnectionPropertiesChange', function (data) {
S
shawn_he 已提交
1421
  console.log(JSON.stringify(data))
S
shawn_he 已提交
1422
})
S
shawn_he 已提交
1423 1424 1425

// Call unregister to unregister the listener.
netCon.unregister(function (error) {
S
shawn_he 已提交
1426
  console.log(JSON.stringify(error))
S
shawn_he 已提交
1427
})
S
shawn_he 已提交
1428 1429
```

S
shawn_he 已提交
1430
### on('netLost')<sup>8+</sup>
S
shawn_he 已提交
1431 1432 1433 1434 1435

on(type: 'netLost', callback: Callback\<NetHandle>): void

Registers a listener for **netLost** events.

S
shawn_he 已提交
1436 1437
**Model restriction**: Before you call this API, make sure tat you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network.

S
shawn_he 已提交
1438 1439 1440 1441 1442 1443
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                              | Mandatory| Description                                                        |
| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
1444 1445
| type     | string                             | Yes  | Event type. The value is fixed to **netLost**.<br>netLost: event indicating that the network is interrupted or normally disconnected.|
| callback | Callback\<[NetHandle](#nethandle)> | Yes  | Callback used to return the network handle (**netHandle**).|
S
shawn_he 已提交
1446 1447 1448

**Example**

S
shawn_he 已提交
1449
```js
S
shawn_he 已提交
1450 1451 1452 1453 1454
// Create a NetConnection object.
let netCon = connection.createNetConnection()

// Call register to register a listener.
netCon.register(function (error) {
S
shawn_he 已提交
1455
  console.log(JSON.stringify(error))
S
shawn_he 已提交
1456 1457 1458 1459
})

// Subscribe to netLost events. Event notifications can be received only after register is called.
netCon.on('netLost', function (data) {
S
shawn_he 已提交
1460
  console.log(JSON.stringify(data))
S
shawn_he 已提交
1461
})
S
shawn_he 已提交
1462 1463 1464

// Call unregister to unregister the listener.
netCon.unregister(function (error) {
S
shawn_he 已提交
1465
  console.log(JSON.stringify(error))
S
shawn_he 已提交
1466
})
S
shawn_he 已提交
1467 1468
```

S
shawn_he 已提交
1469
### on('netUnavailable')<sup>8+</sup>
S
shawn_he 已提交
1470 1471 1472 1473 1474

on(type: 'netUnavailable', callback: Callback\<void>): void

Registers a listener for **netUnavailable** events.

S
shawn_he 已提交
1475 1476
**Model restriction**: Before you call this API, make sure tat you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network.

S
shawn_he 已提交
1477 1478 1479 1480 1481 1482
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type           | Mandatory| Description                                                        |
| -------- | --------------- | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
1483 1484
| type     | string          | Yes  | Event type. The value is fixed to **netUnavailable**.<br>**netUnavailable**: event indicating that the network is unavailable.|
| callback | Callback\<void> | Yes  | Callback used to return the result, which is empty.|
S
shawn_he 已提交
1485 1486 1487

**Example**

S
shawn_he 已提交
1488
```js
S
shawn_he 已提交
1489 1490
// Create a NetConnection object.
let netCon = connection.createNetConnection()
S
shawn_he 已提交
1491

S
shawn_he 已提交
1492 1493
// Call register to register a listener.
netCon.register(function (error) {
S
shawn_he 已提交
1494
  console.log(JSON.stringify(error))
S
shawn_he 已提交
1495 1496
})

S
shawn_he 已提交
1497 1498
// Subscribe to netUnavailable events. Event notifications can be received only after register is called.
netCon.on('netUnavailable', function (data) {
S
shawn_he 已提交
1499
  console.log(JSON.stringify(data))
S
shawn_he 已提交
1500
})
S
shawn_he 已提交
1501

S
shawn_he 已提交
1502 1503
// Call unregister to unregister the listener.
netCon.unregister(function (error) {
S
shawn_he 已提交
1504
  console.log(JSON.stringify(error))
S
shawn_he 已提交
1505 1506 1507
})
```

S
shawn_he 已提交
1508
## NetHandle<sup>8+</sup>
S
shawn_he 已提交
1509 1510 1511

Defines the handle of the data network.

S
shawn_he 已提交
1512
Before invoking **NetHandle** APIs, call **getNetHandle** to obtain a **NetHandle** object.
S
shawn_he 已提交
1513 1514 1515

**System capability**: SystemCapability.Communication.NetManager.Core

S
shawn_he 已提交
1516
### Attributes
S
shawn_he 已提交
1517

S
shawn_he 已提交
1518 1519 1520
| Name   | Type  | Mandatory| Description                     |
| ------ | ------ | --- |------------------------- |
| netId  | number | Yes |  Network ID. The value **0** indicates no default network. Any other value must be greater than or equal to 100.|
S
shawn_he 已提交
1521

S
shawn_he 已提交
1522
### bindSocket<sup>9+</sup>
S
shawn_he 已提交
1523

S
shawn_he 已提交
1524
bindSocket(socketParam: TCPSocket \| UDPSocket, callback: AsyncCallback\<void>): void
S
shawn_he 已提交
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534

Binds a **TCPSocket** or **UDPSocket** object to the data network. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name     | Type                    | Mandatory| Description                           |
| ----------- | ------------------------ | ---- | -------------------------------|
| socketParam | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | Yes| **TCPSocket** or **UDPSocket** object.|
S
shawn_he 已提交
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
| callback    | AsyncCallback\<void>      | Yes  | Callback used to return the result. If the **TCPSocket** or **UDPSocket** object is successfully bound to the current network, **err** is **undefined**. Otherwise, **err** is an error object.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
1545 1546 1547 1548

**Example**

```js
S
shawn_he 已提交
1549
import socket from "@ohos.net.socket";
S
shawn_he 已提交
1550

S
shawn_he 已提交
1551
connection.getDefaultNet().then((netHandle) => {
S
shawn_he 已提交
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
  var tcp = socket.constructTCPSocketInstance();
  var udp = socket.constructUDPSocketInstance();
  let socketType = "TCPSocket";
  if (socketType == "TCPSocket") {
    tcp.bind({
      address: '192.168.xx.xxx', port: 8080, family: 1
    }, error => {
      if (error) {
        console.log('bind fail');
        return;
      }
      netHandle.bindSocket(tcp, (error, data) => {
        if (error) {
          console.log(JSON.stringify(error));
        } else {
          console.log(JSON.stringify(data));
S
shawn_he 已提交
1568
        }
S
shawn_he 已提交
1569 1570 1571 1572 1573
      })
    })
  } else {
    let callback = value => {
      console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo);
S
shawn_he 已提交
1574
    }
S
shawn_he 已提交
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
    udp.on('message', callback);
    udp.bind({
      address: '192.168.xx.xxx', port: 8080, family: 1
    }, error => {
      if (error) {
        console.log('bind fail');
        return;
      }
      udp.on('message', (data) => {
        console.log(JSON.stringify(data))
      });
      netHandle.bindSocket(udp, (error, data) => {
        if (error) {
          console.log(JSON.stringify(error));
        } else {
          console.log(JSON.stringify(data));
        }
      })
    })
  }
S
shawn_he 已提交
1595
})
S
shawn_he 已提交
1596 1597
```

S
shawn_he 已提交
1598
### bindSocket<sup>9+</sup>
S
shawn_he 已提交
1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615

bindSocket(socketParam: TCPSocket \| UDPSocket): Promise\<void>;

Binds a **TCPSocket** or **UDPSocket** object to the data network. This API uses a promise to return the result.

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name         | Type                 | Mandatory | Description                          |
| --------------- | --------------------- | ---- | ------------------------------ |
| socketParam     | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | Yes  | **TCPSocket** or **UDPSocket** object.|

**Return value**

| Type          | Description                  |
| -------------- | ---------------------- |
S
shawn_he 已提交
1616
| Promise\<void> | Promise that returns no value.|
S
shawn_he 已提交
1617

S
shawn_he 已提交
1618 1619 1620 1621 1622 1623 1624 1625 1626
**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

S
shawn_he 已提交
1627 1628 1629
**Example**

```js
S
shawn_he 已提交
1630
import socket from "@ohos.net.socket";
S
shawn_he 已提交
1631

S
shawn_he 已提交
1632
connection.getDefaultNet().then((netHandle) => {
S
shawn_he 已提交
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652
  var tcp = socket.constructTCPSocketInstance();
  var udp = socket.constructUDPSocketInstance();
  let socketType = "TCPSocket";
  if (socketType == "TCPSocket") {
    tcp.bind({
      address: '192.168.xx.xxx', port: 8080, family: 1
    }, error => {
      if (error) {
        console.log('bind fail');
        return;
      }
      netHandle.bindSocket(tcp).then((data) => {
        console.log(JSON.stringify(data));
      }).catch(error => {
        console.log(JSON.stringify(error));
      })
    })
  } else {
    let callback = value => {
      console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo);
S
shawn_he 已提交
1653
    }
S
shawn_he 已提交
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
    udp.on('message', callback);
    udp.bind({
      address: '192.168.xx.xxx', port: 8080, family: 1
    }, error => {
      if (error) {
        console.log('bind fail');
        return;
      }
      udp.on('message', (data) => {
        console.log(JSON.stringify(data));
      })
      netHandle.bindSocket(udp).then((data) => {
        console.log(JSON.stringify(data));
      }).catch(error => {
        console.log(JSON.stringify(error));
      })
    })
  }
S
shawn_he 已提交
1672
})
S
shawn_he 已提交
1673 1674
```

S
shawn_he 已提交
1675
### getAddressesByName<sup>8+</sup>
S
shawn_he 已提交
1676 1677 1678 1679 1680

getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>>): void

Resolves the host name by using the corresponding network to obtain all IP addresses. This API uses an asynchronous callback to return the result.

S
shawn_he 已提交
1681
**Required permissions**: ohos.permission.INTERNET
S
shawn_he 已提交
1682

S
shawn_he 已提交
1683 1684 1685 1686 1687 1688
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                                             | Mandatory| Description              |
| -------- | ------------------------------------------------- | ---- | ------------------ |
S
shawn_he 已提交
1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700
| host     | string                                            | Yes  | Host name to resolve.|
| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | Yes  | Callback used to return the result. If all IP addresses are successfully obtained, **err** is **undefined**, and **data** is the list of all obtained IP addresses. Otherwise, **err** is an error object.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
1701 1702 1703

**Example**

S
shawn_he 已提交
1704
```js
S
shawn_he 已提交
1705
connection.getDefaultNet().then(function (netHandle) {
S
shawn_he 已提交
1706 1707 1708 1709 1710
  let host = "xxxx";
  netHandle.getAddressesByName(host, function (error, data) {
    console.log(JSON.stringify(error))
    console.log(JSON.stringify(data))
  })
S
shawn_he 已提交
1711 1712 1713
})
```

S
shawn_he 已提交
1714
### getAddressesByName<sup>8+</sup>
S
shawn_he 已提交
1715 1716 1717 1718 1719

getAddressesByName(host: string): Promise\<Array\<NetAddress>>

Resolves the host name by using the corresponding network to obtain all IP addresses. This API uses a promise to return the result.

S
shawn_he 已提交
1720
**Required permissions**: ohos.permission.INTERNET
S
shawn_he 已提交
1721

S
shawn_he 已提交
1722 1723 1724 1725 1726 1727
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name| Type  | Mandatory| Description              |
| ------ | ------ | ---- | ------------------ |
S
shawn_he 已提交
1728
| host   | string | Yes  | Host name to resolve.|
S
shawn_he 已提交
1729

S
shawn_he 已提交
1730
**Return value**
S
shawn_he 已提交
1731 1732 1733 1734 1735

| Type                                       | Description                         |
| ------------------------------------------- | ----------------------------- |
| Promise\<Array\<[NetAddress](#netaddress)>> | Promise used to return the result.|

S
shawn_he 已提交
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745
**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

S
shawn_he 已提交
1746 1747
**Example**

S
shawn_he 已提交
1748
```js
S
shawn_he 已提交
1749
connection.getDefaultNet().then(function (netHandle) {
S
shawn_he 已提交
1750 1751 1752 1753
  let host = "xxxx";
  netHandle.getAddressesByName(host).then(function (data) {
    console.log(JSON.stringify(data))
  })
S
shawn_he 已提交
1754 1755 1756
})
```

S
shawn_he 已提交
1757
### getAddressByName<sup>8+</sup>
S
shawn_he 已提交
1758 1759 1760 1761 1762

getAddressByName(host: string, callback: AsyncCallback\<NetAddress>): void

Resolves the host name by using the corresponding network to obtain the first IP address. This API uses an asynchronous callback to return the result.

S
shawn_he 已提交
1763
**Required permissions**: ohos.permission.INTERNET
S
shawn_he 已提交
1764

S
shawn_he 已提交
1765 1766 1767 1768 1769 1770
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                                     | Mandatory| Description              |
| -------- | ----------------------------------------- | ---- | ------------------ |
S
shawn_he 已提交
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782
| host     | string                                    | Yes  | Host name to resolve.|
| callback | AsyncCallback\<[NetAddress](#netaddress)> | Yes  | Callback used to return the result. If the first IP address is obtained successfully, **err** is **undefined**, and **data** is the first obtained IP address. Otherwise, **err** is an error object.        |

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
1783 1784 1785

**Example**

S
shawn_he 已提交
1786
```js
S
shawn_he 已提交
1787
connection.getDefaultNet().then(function (netHandle) {
S
shawn_he 已提交
1788 1789 1790 1791 1792
  let host = "xxxx";
  netHandle.getAddressByName(host, function (error, data) {
    console.log(JSON.stringify(error))
    console.log(JSON.stringify(data))
  })
S
shawn_he 已提交
1793 1794 1795
})
```

S
shawn_he 已提交
1796
### getAddressByName<sup>8+</sup>
S
shawn_he 已提交
1797 1798 1799 1800 1801

getAddressByName(host: string): Promise\<NetAddress>

Resolves the host name by using the corresponding network to obtain the first IP address. This API uses a promise to return the result.

S
shawn_he 已提交
1802
**Required permissions**: ohos.permission.INTERNET
S
shawn_he 已提交
1803

S
shawn_he 已提交
1804 1805 1806 1807 1808 1809
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name| Type  | Mandatory| Description              |
| ------ | ------ | ---- | ------------------ |
S
shawn_he 已提交
1810
| host   | string | Yes  | Host name to resolve.|
S
shawn_he 已提交
1811

S
shawn_he 已提交
1812
**Return value**
S
shawn_he 已提交
1813 1814 1815 1816 1817

| Type                               | Description                           |
| ----------------------------------- | ------------------------------- |
| Promise\<[NetAddress](#netaddress)> | Promise used to return the result.|

S
shawn_he 已提交
1818 1819 1820 1821 1822 1823 1824 1825 1826 1827
**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

S
shawn_he 已提交
1828 1829
**Example**

S
shawn_he 已提交
1830
```js
S
shawn_he 已提交
1831
connection.getDefaultNet().then(function (netHandle) {
S
shawn_he 已提交
1832 1833 1834 1835
  let host = "xxxx";
  netHandle.getAddressByName(host).then(function (data) {
    console.log(JSON.stringify(data))
  })
S
shawn_he 已提交
1836 1837 1838
})
```

S
shawn_he 已提交
1839
## NetCap<sup>8+</sup>
S
shawn_he 已提交
1840

S
shawn_he 已提交
1841
Defines the network capability.
S
shawn_he 已提交
1842 1843 1844

**System capability**: SystemCapability.Communication.NetManager.Core

S
shawn_he 已提交
1845 1846 1847 1848 1849 1850 1851
| Name                 | Value  | Description                  |
| ------------------------ | ---- | ---------------------- |
| NET_CAPABILITY_MMS | 0 | The network can connect to the carrier's Multimedia Messaging Service Center (MMSC) to send and receive multimedia messages.|
| NET_CAPABILITY_NOT_METERED | 11 | The network traffic is not metered.|
| NET_CAPABILITY_INTERNET  | 12   | The network has the Internet access capability, which is set by the network provider.|
| NET_CAPABILITY_NOT_VPN | 15 | The network does not use a virtual private network (VPN).|
| NET_CAPABILITY_VALIDATED | 16   | The Internet access capability of the network is successfully verified by the connection management module.|
S
shawn_he 已提交
1852

S
shawn_he 已提交
1853
## NetBearType<sup>8+</sup>
S
shawn_he 已提交
1854

S
shawn_he 已提交
1855
Enumerates network types.
S
shawn_he 已提交
1856 1857 1858

**System capability**: SystemCapability.Communication.NetManager.Core

S
shawn_he 已提交
1859 1860 1861 1862 1863
| Name        | Value  | Description       |
| --------------- | ---- | ----------- |
| BEARER_CELLULAR | 0    | Cellular network. |
| BEARER_WIFI     | 1    | Wi-Fi network.|
| BEARER_ETHERNET | 3 | Ethernet network.|
S
shawn_he 已提交
1864

S
shawn_he 已提交
1865
## HttpProxy<sup>10+</sup>
S
shawn_he 已提交
1866

S
shawn_he 已提交
1867
Defines the global HTTP proxy configuration of the network.
S
shawn_he 已提交
1868 1869 1870

**System capability**: SystemCapability.Communication.NetManager.Core

S
shawn_he 已提交
1871 1872 1873 1874
| Name   | Type  | Mandatory| Description                     |
| ------ | ------ | --- |------------------------- |
| host  | string | No |  Host name of the proxy server.|
| port  | number | No |  Host port.|
S
shawn_he 已提交
1875
| exclusionList  | Array<string> | No |  Exclusion list of hosts that do not use the proxy server. The length of the combined elements in the list cannot exceed 96 bytes.<br>For example, the length of **baidu.com,zhihu.com** is 20 bytes.|
S
shawn_he 已提交
1876

S
shawn_he 已提交
1877
## NetSpecifier<sup>8+</sup>
S
shawn_he 已提交
1878

S
shawn_he 已提交
1879
Provides an instance that bears data network capabilities.
S
shawn_he 已提交
1880 1881 1882

**System capability**: SystemCapability.Communication.NetManager.Core

S
shawn_he 已提交
1883 1884 1885 1886 1887
| Name                    | Type                               | Mandatory | Description                                                        |
| ----------------------- | ----------------------------------- | ---- | ------------------------------------------------------------ |
| netCapabilities         | [NetCapabilities](#netcapabilities) |  Yes | Network transmission capabilities and bearer types of the data network.                               |
| bearerPrivateIdentifier | string                              |  No |  Network identifier. The identifier of a Wi-Fi network is **wifi**, and that of a cellular network is **slot0** (corresponding to SIM card 1).|

S
shawn_he 已提交
1888
## NetCapabilities<sup>8+</sup>
S
shawn_he 已提交
1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899

Defines the network capability set.

**System capability**: SystemCapability.Communication.NetManager.Core

| Name                 | Type                               | Mandatory| Description                    |
| --------------------- | ---------------------------------- | --- | ------------------------ |
| linkUpBandwidthKbps   | number                             |  No|  Uplink (from the device to the network) bandwidth. |
| linkDownBandwidthKbps | number                             |  No|  Downlink (from the network to the device) bandwidth.  |
| networkCap            | Array\<[NetCap](#netcap)>           |  No|  Network capability.          |
| bearerTypes           | Array\<[NetBearType](#netbeartype)> |  Yes|  Network type.              |
S
shawn_he 已提交
1900

S
shawn_he 已提交
1901
## ConnectionProperties<sup>8+</sup>
S
shawn_he 已提交
1902 1903 1904 1905 1906

Defines the network connection properties.

**System capability**: SystemCapability.Communication.NetManager.Core

S
shawn_he 已提交
1907 1908 1909 1910 1911 1912 1913 1914
| Name          | Type                              | Mandatory|  Description            |
| ------------- | ---------------------------------- | ----|---------------- |
| interfaceName | string                             | Yes|Network interface card (NIC) name.      |
| domains       | string                             | Yes|Domain. The default value is **""**.|
| linkAddresses | Array\<[LinkAddress](#linkaddress)> | Yes|Link information.      |
| routes        | Array\<[RouteInfo](#routeinfo)>     | Yes|Route information.      |
| dnses     | Array\<[NetAddress](#netaddress)> | Yes|Network address. For details, see [NetAddress](#netaddress).|
| mtu           | number                             | Yes|Maximum transmission unit (MTU).  |
S
shawn_he 已提交
1915

S
shawn_he 已提交
1916
## RouteInfo<sup>8+</sup>
S
shawn_he 已提交
1917

S
shawn_he 已提交
1918
Defines network route information.
S
shawn_he 已提交
1919 1920 1921

**System capability**: SystemCapability.Communication.NetManager.Core

S
shawn_he 已提交
1922 1923 1924 1925 1926 1927 1928
| Name          | Type                       | Mandatory|Description            |
| -------------- | --------------------------- | --- |---------------- |
| interface      | string                      | Yes|NIC name.      |
| destination    | [LinkAddress](#linkaddress) | Yes|Destination address.      |
| gateway        | [NetAddress](#netaddress)   | Yes|Gateway address.      |
| hasGateway     | boolean                     | Yes|Whether a gateway is present.    |
| isDefaultRoute | boolean                     | Yes|Whether the route is the default route.|
S
shawn_he 已提交
1929

S
shawn_he 已提交
1930
## LinkAddress<sup>8+</sup>
S
shawn_he 已提交
1931

S
shawn_he 已提交
1932
Defines network link information.
S
shawn_he 已提交
1933 1934 1935

**System capability**: SystemCapability.Communication.NetManager.Core

S
shawn_he 已提交
1936 1937 1938 1939
| Name       | Type                     | Mandatory|Description                |
| ------------ | ----------------------- |---- |-------------------- |
| address      | [NetAddress](#netaddress) | Yes| Link address.          |
| prefixLength | number                    | Yes|Length of the link address prefix.|
S
shawn_he 已提交
1940

S
shawn_he 已提交
1941
## NetAddress<sup>8+</sup>
S
shawn_he 已提交
1942

S
shawn_he 已提交
1943
Defines a network address.
S
shawn_he 已提交
1944 1945 1946

**System capability**: SystemCapability.Communication.NetManager.Core

S
shawn_he 已提交
1947
| Name| Type| Mandatory| Description|
S
shawn_he 已提交
1948
| ------- | ------ | -- |------------------------------ |
S
shawn_he 已提交
1949
| address | string | Yes|Network address.|
S
shawn_he 已提交
1950
| family  | number | No|Address family identifier. The value is **1** for IPv4 and **2** for IPv6. The default value is **1**.|
S
shawn_he 已提交
1951
| port    | number | No|Port number. The value ranges from **0** to **65535**.|