1e41f4b71Sopenharmony_ci# @ohos.net.socket (Socket Connection)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciThe **socket** module implements data transfer over TCP, UDP, Web, and TLS socket connections.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **NOTE**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci## Modules to Import
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci```ts
12e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
13e41f4b71Sopenharmony_ci```
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci## socket.constructUDPSocketInstance
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ciconstructUDPSocketInstance(): UDPSocket
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ciCreates a **UDPSocket** object.
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci**Return value**
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci| Type                              | Description                   |
26e41f4b71Sopenharmony_ci|  --------------------------------- |  ---------------------- |
27e41f4b71Sopenharmony_ci| [UDPSocket](#udpsocket) | **UDPSocket** object.|
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci**Example**
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci```ts
32e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
33e41f4b71Sopenharmony_cilet udp: socket.UDPSocket = socket.constructUDPSocketInstance();
34e41f4b71Sopenharmony_ci```
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ci## UDPSocket
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_ciDefines a UDP socket connection. Before calling UDPSocket APIs, you need to call [socket.constructUDPSocketInstance](#socketconstructudpsocketinstance) to create a **UDPSocket** object.
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci### bind
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_cibind(address: NetAddress, callback: AsyncCallback\<void\>): void
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ciBinds the IP address and port number. The port number can be specified or randomly allocated by the system. This API uses an asynchronous callback to return the result.
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci**Parameters**
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ci| Name  | Type                              | Mandatory| Description                                                  |
53e41f4b71Sopenharmony_ci| -------- | ---------------------------------- | ---- | ------------------------------------------------------ |
54e41f4b71Sopenharmony_ci| address  | [NetAddress](#netaddress) | Yes  | Destination address. For details, see [NetAddress](#netaddress).|
55e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void\>              | Yes  | Callback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.       |
56e41f4b71Sopenharmony_ci
57e41f4b71Sopenharmony_ci**Error codes**
58e41f4b71Sopenharmony_ci
59e41f4b71Sopenharmony_ci| ID| Error Message                |
60e41f4b71Sopenharmony_ci| ------- | ----------------------- |
61e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
62e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci**Example**
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci```ts
67e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
68e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_cilet udp: socket.UDPSocket = socket.constructUDPSocketInstance();
71e41f4b71Sopenharmony_cilet bindAddr: socket.NetAddress = {
72e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
73e41f4b71Sopenharmony_ci  port: 1234
74e41f4b71Sopenharmony_ci}
75e41f4b71Sopenharmony_ciudp.bind(bindAddr, (err: BusinessError) => {
76e41f4b71Sopenharmony_ci  if (err) {
77e41f4b71Sopenharmony_ci    console.log('bind fail');
78e41f4b71Sopenharmony_ci    return;
79e41f4b71Sopenharmony_ci  }
80e41f4b71Sopenharmony_ci  console.log('bind success');
81e41f4b71Sopenharmony_ci});
82e41f4b71Sopenharmony_ci```
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci### bind
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_cibind(address: NetAddress): Promise\<void\>
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ciBinds the IP address and port number. The port number can be specified or randomly allocated by the system. This API uses a promise to return the result.
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_ci**Parameters**
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ci| Name | Type                              | Mandatory| Description                                                  |
97e41f4b71Sopenharmony_ci| ------- | ---------------------------------- | ---- | ------------------------------------------------------ |
98e41f4b71Sopenharmony_ci| address | [NetAddress](#netaddress) | Yes  | Destination address. For details, see [NetAddress](#netaddress).|
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci**Error codes**
101e41f4b71Sopenharmony_ci
102e41f4b71Sopenharmony_ci| ID| Error Message                |
103e41f4b71Sopenharmony_ci| ------- | ----------------------- |
104e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
105e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
106e41f4b71Sopenharmony_ci
107e41f4b71Sopenharmony_ci**Return value**
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci| Type           | Description                                      |
110e41f4b71Sopenharmony_ci|  -------------- |  ----------------------------------------- |
111e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result.|
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_ci**Example**
114e41f4b71Sopenharmony_ci
115e41f4b71Sopenharmony_ci```ts
116e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
117e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_cilet udp: socket.UDPSocket = socket.constructUDPSocketInstance();
120e41f4b71Sopenharmony_cilet bindAddr: socket.NetAddress = {
121e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
122e41f4b71Sopenharmony_ci  port: 8080
123e41f4b71Sopenharmony_ci}
124e41f4b71Sopenharmony_ciudp.bind(bindAddr).then(() => {
125e41f4b71Sopenharmony_ci  console.log('bind success');
126e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
127e41f4b71Sopenharmony_ci  console.log('bind fail');
128e41f4b71Sopenharmony_ci});
129e41f4b71Sopenharmony_ci```
130e41f4b71Sopenharmony_ci
131e41f4b71Sopenharmony_ci### send
132e41f4b71Sopenharmony_ci
133e41f4b71Sopenharmony_cisend(options: UDPSendOptions, callback: AsyncCallback\<void\>): void
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_ciSends data over a UDP socket connection. This API uses an asynchronous callback to return the result.
136e41f4b71Sopenharmony_ci
137e41f4b71Sopenharmony_ciBefore sending data, call [UDPSocket.bind()](#bind) to bind the IP address and port. Call the API in the worker thread or taskpool thread as this operation is time-consuming.
138e41f4b71Sopenharmony_ci
139e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
140e41f4b71Sopenharmony_ci
141e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
142e41f4b71Sopenharmony_ci
143e41f4b71Sopenharmony_ci**Parameters**
144e41f4b71Sopenharmony_ci
145e41f4b71Sopenharmony_ci| Name  | Type                                    | Mandatory| Description                                                        |
146e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ------------------------------------------------------------ |
147e41f4b71Sopenharmony_ci| options  | [UDPSendOptions](#udpsendoptions) | Yes  | Parameters for sending data over a UDP socket connection. For details, see [UDPSendOptions](#udpsendoptions).|
148e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void\>                    | Yes  | Callback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.                                                 |
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ci**Error codes**
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ci| ID| Error Message                |
153e41f4b71Sopenharmony_ci| ------- | ----------------------- |
154e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
155e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
156e41f4b71Sopenharmony_ci
157e41f4b71Sopenharmony_ci**Example**
158e41f4b71Sopenharmony_ci
159e41f4b71Sopenharmony_ci```ts
160e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
161e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
162e41f4b71Sopenharmony_ci
163e41f4b71Sopenharmony_cilet udp: socket.UDPSocket = socket.constructUDPSocketInstance();
164e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
165e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
166e41f4b71Sopenharmony_ci  port: 8080
167e41f4b71Sopenharmony_ci}
168e41f4b71Sopenharmony_cilet sendOptions: socket.UDPSendOptions = {
169e41f4b71Sopenharmony_ci  data: 'Hello, server!',
170e41f4b71Sopenharmony_ci  address: netAddress
171e41f4b71Sopenharmony_ci}
172e41f4b71Sopenharmony_ciudp.send(sendOptions, (err: BusinessError) => {
173e41f4b71Sopenharmony_ci  if (err) {
174e41f4b71Sopenharmony_ci    console.log('send fail');
175e41f4b71Sopenharmony_ci    return;
176e41f4b71Sopenharmony_ci  }
177e41f4b71Sopenharmony_ci  console.log('send success');
178e41f4b71Sopenharmony_ci});
179e41f4b71Sopenharmony_ci```
180e41f4b71Sopenharmony_ci
181e41f4b71Sopenharmony_ci### send
182e41f4b71Sopenharmony_ci
183e41f4b71Sopenharmony_cisend(options: UDPSendOptions): Promise\<void\>
184e41f4b71Sopenharmony_ci
185e41f4b71Sopenharmony_ciSends data over a UDP socket connection. This API uses a promise to return the result.
186e41f4b71Sopenharmony_ci
187e41f4b71Sopenharmony_ciBefore sending data, call [UDPSocket.bind()](#bind) to bind the IP address and port. Call the API in the worker thread or taskpool thread as this operation is time-consuming.
188e41f4b71Sopenharmony_ci
189e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
190e41f4b71Sopenharmony_ci
191e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
192e41f4b71Sopenharmony_ci
193e41f4b71Sopenharmony_ci**Parameters**
194e41f4b71Sopenharmony_ci
195e41f4b71Sopenharmony_ci| Name | Type                                    | Mandatory| Description                                                        |
196e41f4b71Sopenharmony_ci| ------- | ---------------------------------------- | ---- | ------------------------------------------------------------ |
197e41f4b71Sopenharmony_ci| options | [UDPSendOptions](#udpsendoptions) | Yes  | Parameters for sending data over a UDP socket connection. For details, see [UDPSendOptions](#udpsendoptions).|
198e41f4b71Sopenharmony_ci
199e41f4b71Sopenharmony_ci**Error codes**
200e41f4b71Sopenharmony_ci
201e41f4b71Sopenharmony_ci| ID| Error Message                |
202e41f4b71Sopenharmony_ci| ------- | ----------------------- |
203e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
204e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
205e41f4b71Sopenharmony_ci
206e41f4b71Sopenharmony_ci**Return value**
207e41f4b71Sopenharmony_ci
208e41f4b71Sopenharmony_ci| Type           | Description                                          |
209e41f4b71Sopenharmony_ci|  -------------- |  --------------------------------------------- |
210e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result.|
211e41f4b71Sopenharmony_ci
212e41f4b71Sopenharmony_ci**Example**
213e41f4b71Sopenharmony_ci
214e41f4b71Sopenharmony_ci```ts
215e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
216e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
217e41f4b71Sopenharmony_ci
218e41f4b71Sopenharmony_cilet udp: socket.UDPSocket = socket.constructUDPSocketInstance();
219e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
220e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
221e41f4b71Sopenharmony_ci  port: 8080
222e41f4b71Sopenharmony_ci}
223e41f4b71Sopenharmony_cilet sendOptions: socket.UDPSendOptions = {
224e41f4b71Sopenharmony_ci  data: 'Hello, server!',
225e41f4b71Sopenharmony_ci  address: netAddress
226e41f4b71Sopenharmony_ci}
227e41f4b71Sopenharmony_ciudp.send(sendOptions).then(() => {
228e41f4b71Sopenharmony_ci  console.log('send success');
229e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
230e41f4b71Sopenharmony_ci  console.log('send fail');
231e41f4b71Sopenharmony_ci});
232e41f4b71Sopenharmony_ci```
233e41f4b71Sopenharmony_ci
234e41f4b71Sopenharmony_ci### close
235e41f4b71Sopenharmony_ci
236e41f4b71Sopenharmony_ciclose(callback: AsyncCallback\<void\>): void
237e41f4b71Sopenharmony_ci
238e41f4b71Sopenharmony_ciCloses a UDP socket connection. This API uses an asynchronous callback to return the result.
239e41f4b71Sopenharmony_ci
240e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
241e41f4b71Sopenharmony_ci
242e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
243e41f4b71Sopenharmony_ci
244e41f4b71Sopenharmony_ci**Parameters**
245e41f4b71Sopenharmony_ci
246e41f4b71Sopenharmony_ci| Name  | Type                 | Mandatory| Description      |
247e41f4b71Sopenharmony_ci| -------- | --------------------- | ---- | ---------- |
248e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void\> | Yes  | Callback used to return the result.  |
249e41f4b71Sopenharmony_ci
250e41f4b71Sopenharmony_ci**Error codes**
251e41f4b71Sopenharmony_ci
252e41f4b71Sopenharmony_ci| ID| Error Message                |
253e41f4b71Sopenharmony_ci| ------- | ----------------------- |
254e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
255e41f4b71Sopenharmony_ci
256e41f4b71Sopenharmony_ci**Example**
257e41f4b71Sopenharmony_ci
258e41f4b71Sopenharmony_ci```ts
259e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
260e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
261e41f4b71Sopenharmony_ci
262e41f4b71Sopenharmony_cilet udp: socket.UDPSocket = socket.constructUDPSocketInstance();
263e41f4b71Sopenharmony_ciudp.close((err: BusinessError) => {
264e41f4b71Sopenharmony_ci  if (err) {
265e41f4b71Sopenharmony_ci    console.log('close fail');
266e41f4b71Sopenharmony_ci    return;
267e41f4b71Sopenharmony_ci  }
268e41f4b71Sopenharmony_ci  console.log('close success');
269e41f4b71Sopenharmony_ci})
270e41f4b71Sopenharmony_ci```
271e41f4b71Sopenharmony_ci
272e41f4b71Sopenharmony_ci### close
273e41f4b71Sopenharmony_ci
274e41f4b71Sopenharmony_ciclose(): Promise\<void\>
275e41f4b71Sopenharmony_ci
276e41f4b71Sopenharmony_ciCloses a UDP socket connection. This API uses a promise to return the result.
277e41f4b71Sopenharmony_ci
278e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
279e41f4b71Sopenharmony_ci
280e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
281e41f4b71Sopenharmony_ci
282e41f4b71Sopenharmony_ci**Error codes**
283e41f4b71Sopenharmony_ci
284e41f4b71Sopenharmony_ci| ID| Error Message                |
285e41f4b71Sopenharmony_ci| ------- | ----------------------- |
286e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
287e41f4b71Sopenharmony_ci
288e41f4b71Sopenharmony_ci**Return value**
289e41f4b71Sopenharmony_ci
290e41f4b71Sopenharmony_ci| Type           | Description                                      |
291e41f4b71Sopenharmony_ci|  -------------- |  ----------------------------------------- |
292e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result.|
293e41f4b71Sopenharmony_ci
294e41f4b71Sopenharmony_ci**Example**
295e41f4b71Sopenharmony_ci
296e41f4b71Sopenharmony_ci```ts
297e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
298e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
299e41f4b71Sopenharmony_ci
300e41f4b71Sopenharmony_cilet udp: socket.UDPSocket = socket.constructUDPSocketInstance();
301e41f4b71Sopenharmony_ciudp.close().then(() => {
302e41f4b71Sopenharmony_ci  console.log('close success');
303e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
304e41f4b71Sopenharmony_ci  console.log('close fail');
305e41f4b71Sopenharmony_ci});
306e41f4b71Sopenharmony_ci```
307e41f4b71Sopenharmony_ci
308e41f4b71Sopenharmony_ci### getState
309e41f4b71Sopenharmony_ci
310e41f4b71Sopenharmony_cigetState(callback: AsyncCallback\<SocketStateBase\>): void
311e41f4b71Sopenharmony_ci
312e41f4b71Sopenharmony_ciObtains the status of the UDP socket connection. This API uses an asynchronous callback to return the result.
313e41f4b71Sopenharmony_ci
314e41f4b71Sopenharmony_ci> **NOTE**
315e41f4b71Sopenharmony_ci> This API can be called only after **bind** is successfully called.
316e41f4b71Sopenharmony_ci
317e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
318e41f4b71Sopenharmony_ci
319e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
320e41f4b71Sopenharmony_ci
321e41f4b71Sopenharmony_ci**Parameters**
322e41f4b71Sopenharmony_ci
323e41f4b71Sopenharmony_ci| Name  | Type                                                  | Mandatory| Description      |
324e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------ | ---- | ---------- |
325e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[SocketStateBase](#socketstatebase)\> | Yes  | Callback used to return the result. If the operation is successful, the status of the TLS socket server connection is returned. If the operation fails, an error message is returned.|
326e41f4b71Sopenharmony_ci
327e41f4b71Sopenharmony_ci**Error codes**
328e41f4b71Sopenharmony_ci
329e41f4b71Sopenharmony_ci| ID| Error Message                |
330e41f4b71Sopenharmony_ci| ------- | ----------------------- |
331e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
332e41f4b71Sopenharmony_ci
333e41f4b71Sopenharmony_ci**Example**
334e41f4b71Sopenharmony_ci
335e41f4b71Sopenharmony_ci```ts
336e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
337e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
338e41f4b71Sopenharmony_ci
339e41f4b71Sopenharmony_cilet udp: socket.UDPSocket = socket.constructUDPSocketInstance();
340e41f4b71Sopenharmony_cilet bindAddr: socket.NetAddress = {
341e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
342e41f4b71Sopenharmony_ci  port: 8080
343e41f4b71Sopenharmony_ci}
344e41f4b71Sopenharmony_ciudp.bind(bindAddr, (err: BusinessError) => {
345e41f4b71Sopenharmony_ci  if (err) {
346e41f4b71Sopenharmony_ci    console.log('bind fail');
347e41f4b71Sopenharmony_ci    return;
348e41f4b71Sopenharmony_ci  }
349e41f4b71Sopenharmony_ci  console.log('bind success');
350e41f4b71Sopenharmony_ci  udp.getState((err: BusinessError, data: socket.SocketStateBase) => {
351e41f4b71Sopenharmony_ci    if (err) {
352e41f4b71Sopenharmony_ci      console.log('getState fail');
353e41f4b71Sopenharmony_ci      return;
354e41f4b71Sopenharmony_ci    }
355e41f4b71Sopenharmony_ci    console.log('getState success:' + JSON.stringify(data));
356e41f4b71Sopenharmony_ci  })
357e41f4b71Sopenharmony_ci})
358e41f4b71Sopenharmony_ci```
359e41f4b71Sopenharmony_ci
360e41f4b71Sopenharmony_ci### getState
361e41f4b71Sopenharmony_ci
362e41f4b71Sopenharmony_cigetState(): Promise\<SocketStateBase\>
363e41f4b71Sopenharmony_ci
364e41f4b71Sopenharmony_ciObtains the status of the UDP socket connection. This API uses a promise to return the result.
365e41f4b71Sopenharmony_ci
366e41f4b71Sopenharmony_ci> **NOTE**
367e41f4b71Sopenharmony_ci> This API can be called only after **bind** is successfully called.
368e41f4b71Sopenharmony_ci
369e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
370e41f4b71Sopenharmony_ci
371e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
372e41f4b71Sopenharmony_ci
373e41f4b71Sopenharmony_ci**Error codes**
374e41f4b71Sopenharmony_ci
375e41f4b71Sopenharmony_ci| ID| Error Message                |
376e41f4b71Sopenharmony_ci| ------- | ----------------------- |
377e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
378e41f4b71Sopenharmony_ci
379e41f4b71Sopenharmony_ci**Return value**
380e41f4b71Sopenharmony_ci
381e41f4b71Sopenharmony_ci| Type                                            | Description                                      |
382e41f4b71Sopenharmony_ci|  ----------------------------------------------- |  ----------------------------------------- |
383e41f4b71Sopenharmony_ci| Promise\<[SocketStateBase](#socketstatebase)\> | Promise used to return the result.|
384e41f4b71Sopenharmony_ci
385e41f4b71Sopenharmony_ci**Example**
386e41f4b71Sopenharmony_ci
387e41f4b71Sopenharmony_ci```ts
388e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
389e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
390e41f4b71Sopenharmony_ci
391e41f4b71Sopenharmony_cilet udp: socket.UDPSocket = socket.constructUDPSocketInstance();
392e41f4b71Sopenharmony_cilet bindAddr: socket.NetAddress = {
393e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
394e41f4b71Sopenharmony_ci  port: 8080
395e41f4b71Sopenharmony_ci}
396e41f4b71Sopenharmony_ciudp.bind(bindAddr, (err: BusinessError) => {
397e41f4b71Sopenharmony_ci  if (err) {
398e41f4b71Sopenharmony_ci    console.log('bind fail');
399e41f4b71Sopenharmony_ci    return;
400e41f4b71Sopenharmony_ci  }
401e41f4b71Sopenharmony_ci  console.log('bind success');
402e41f4b71Sopenharmony_ci  udp.getState().then((data: socket.SocketStateBase) => {
403e41f4b71Sopenharmony_ci    console.log('getState success:' + JSON.stringify(data));
404e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
405e41f4b71Sopenharmony_ci    console.log('getState fail' + JSON.stringify(err));
406e41f4b71Sopenharmony_ci  });
407e41f4b71Sopenharmony_ci});
408e41f4b71Sopenharmony_ci```
409e41f4b71Sopenharmony_ci
410e41f4b71Sopenharmony_ci### setExtraOptions
411e41f4b71Sopenharmony_ci
412e41f4b71Sopenharmony_cisetExtraOptions(options: UDPExtraOptions, callback: AsyncCallback\<void\>): void
413e41f4b71Sopenharmony_ci
414e41f4b71Sopenharmony_ciSets other properties of the UDP socket connection. This API uses an asynchronous callback to return the result.
415e41f4b71Sopenharmony_ci
416e41f4b71Sopenharmony_ci> **NOTE**
417e41f4b71Sopenharmony_ci> This API can be called only after **bind** is successfully called.
418e41f4b71Sopenharmony_ci
419e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
420e41f4b71Sopenharmony_ci
421e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
422e41f4b71Sopenharmony_ci
423e41f4b71Sopenharmony_ci**Parameters**
424e41f4b71Sopenharmony_ci
425e41f4b71Sopenharmony_ci| Name  | Type                                    | Mandatory| Description                                                        |
426e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ------------------------------------------------------------ |
427e41f4b71Sopenharmony_ci| options  | [UDPExtraOptions](#udpextraoptions) | Yes  | Other properties of the UDP socket connection. For details, see [UDPExtraOptions](#udpextraoptions).|
428e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void\>                    | Yes  | Callback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.                   |
429e41f4b71Sopenharmony_ci
430e41f4b71Sopenharmony_ci**Error codes**
431e41f4b71Sopenharmony_ci
432e41f4b71Sopenharmony_ci| ID| Error Message                |
433e41f4b71Sopenharmony_ci| ------- | ----------------------- |
434e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
435e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
436e41f4b71Sopenharmony_ci
437e41f4b71Sopenharmony_ci**Example**
438e41f4b71Sopenharmony_ci
439e41f4b71Sopenharmony_ci```ts
440e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
441e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
442e41f4b71Sopenharmony_ci
443e41f4b71Sopenharmony_cilet udp: socket.UDPSocket = socket.constructUDPSocketInstance();
444e41f4b71Sopenharmony_ci
445e41f4b71Sopenharmony_cilet bindAddr: socket.NetAddress = {
446e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
447e41f4b71Sopenharmony_ci  port: 8080
448e41f4b71Sopenharmony_ci}
449e41f4b71Sopenharmony_ciudp.bind(bindAddr, (err: BusinessError) => {
450e41f4b71Sopenharmony_ci  if (err) {
451e41f4b71Sopenharmony_ci    console.log('bind fail');
452e41f4b71Sopenharmony_ci    return;
453e41f4b71Sopenharmony_ci  }
454e41f4b71Sopenharmony_ci  console.log('bind success');
455e41f4b71Sopenharmony_ci  let udpextraoptions: socket.UDPExtraOptions = {
456e41f4b71Sopenharmony_ci    receiveBufferSize: 1000,
457e41f4b71Sopenharmony_ci    sendBufferSize: 1000,
458e41f4b71Sopenharmony_ci    reuseAddress: false,
459e41f4b71Sopenharmony_ci    socketTimeout: 6000,
460e41f4b71Sopenharmony_ci    broadcast: true
461e41f4b71Sopenharmony_ci  }
462e41f4b71Sopenharmony_ci  udp.setExtraOptions(udpextraoptions, (err: BusinessError) => {
463e41f4b71Sopenharmony_ci    if (err) {
464e41f4b71Sopenharmony_ci      console.log('setExtraOptions fail');
465e41f4b71Sopenharmony_ci      return;
466e41f4b71Sopenharmony_ci    }
467e41f4b71Sopenharmony_ci    console.log('setExtraOptions success');
468e41f4b71Sopenharmony_ci  })
469e41f4b71Sopenharmony_ci})
470e41f4b71Sopenharmony_ci```
471e41f4b71Sopenharmony_ci
472e41f4b71Sopenharmony_ci### setExtraOptions
473e41f4b71Sopenharmony_ci
474e41f4b71Sopenharmony_cisetExtraOptions(options: UDPExtraOptions): Promise\<void\>
475e41f4b71Sopenharmony_ci
476e41f4b71Sopenharmony_ciSets other properties of the UDP socket connection. This API uses a promise to return the result.
477e41f4b71Sopenharmony_ci
478e41f4b71Sopenharmony_ci> **NOTE**
479e41f4b71Sopenharmony_ci> This API can be called only after **bind** is successfully called.
480e41f4b71Sopenharmony_ci
481e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
482e41f4b71Sopenharmony_ci
483e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
484e41f4b71Sopenharmony_ci
485e41f4b71Sopenharmony_ci**Parameters**
486e41f4b71Sopenharmony_ci
487e41f4b71Sopenharmony_ci| Name | Type                                    | Mandatory| Description                                                        |
488e41f4b71Sopenharmony_ci| ------- | ---------------------------------------- | ---- | ------------------------------------------------------------ |
489e41f4b71Sopenharmony_ci| options | [UDPExtraOptions](#udpextraoptions) | Yes  | Other properties of the UDP socket connection. For details, see [UDPExtraOptions](#udpextraoptions).|
490e41f4b71Sopenharmony_ci
491e41f4b71Sopenharmony_ci**Return value**
492e41f4b71Sopenharmony_ci
493e41f4b71Sopenharmony_ci| Type           | Description                                                |
494e41f4b71Sopenharmony_ci|  -------------- |  --------------------------------------------------- |
495e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result.|
496e41f4b71Sopenharmony_ci
497e41f4b71Sopenharmony_ci**Error codes**
498e41f4b71Sopenharmony_ci
499e41f4b71Sopenharmony_ci| ID| Error Message                |
500e41f4b71Sopenharmony_ci| ------- | ----------------------- |
501e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
502e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
503e41f4b71Sopenharmony_ci
504e41f4b71Sopenharmony_ci**Example**
505e41f4b71Sopenharmony_ci
506e41f4b71Sopenharmony_ci```ts
507e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
508e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
509e41f4b71Sopenharmony_ci
510e41f4b71Sopenharmony_cilet udp: socket.UDPSocket = socket.constructUDPSocketInstance();
511e41f4b71Sopenharmony_ci
512e41f4b71Sopenharmony_cilet bindAddr: socket.NetAddress = {
513e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
514e41f4b71Sopenharmony_ci  port: 8080
515e41f4b71Sopenharmony_ci}
516e41f4b71Sopenharmony_ciudp.bind(bindAddr, (err: BusinessError) => {
517e41f4b71Sopenharmony_ci  if (err) {
518e41f4b71Sopenharmony_ci    console.log('bind fail');
519e41f4b71Sopenharmony_ci    return;
520e41f4b71Sopenharmony_ci  }
521e41f4b71Sopenharmony_ci  console.log('bind success');
522e41f4b71Sopenharmony_ci  let udpextraoptions: socket.UDPExtraOptions = {
523e41f4b71Sopenharmony_ci    receiveBufferSize: 1000,
524e41f4b71Sopenharmony_ci    sendBufferSize: 1000,
525e41f4b71Sopenharmony_ci    reuseAddress: false,
526e41f4b71Sopenharmony_ci    socketTimeout: 6000,
527e41f4b71Sopenharmony_ci    broadcast: true
528e41f4b71Sopenharmony_ci  }
529e41f4b71Sopenharmony_ci  udp.setExtraOptions(udpextraoptions).then(() => {
530e41f4b71Sopenharmony_ci    console.log('setExtraOptions success');
531e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
532e41f4b71Sopenharmony_ci    console.log('setExtraOptions fail');
533e41f4b71Sopenharmony_ci  });
534e41f4b71Sopenharmony_ci})
535e41f4b71Sopenharmony_ci```
536e41f4b71Sopenharmony_ci
537e41f4b71Sopenharmony_ci### getLocalAddress<sup>12+</sup>
538e41f4b71Sopenharmony_ci
539e41f4b71Sopenharmony_cigetLocalAddress(): Promise\<NetAddress\>
540e41f4b71Sopenharmony_ci
541e41f4b71Sopenharmony_ciObtains the local socket address of a **UDPSocket** connection. This API uses a promise to return the result.
542e41f4b71Sopenharmony_ci
543e41f4b71Sopenharmony_ci> **NOTE**
544e41f4b71Sopenharmony_ci> This API can be called only after **bind** is successfully called.
545e41f4b71Sopenharmony_ci
546e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
547e41f4b71Sopenharmony_ci
548e41f4b71Sopenharmony_ci**Return value**
549e41f4b71Sopenharmony_ci
550e41f4b71Sopenharmony_ci| Type           | Description                                                |
551e41f4b71Sopenharmony_ci|  -------------- |  --------------------------------------------------- |
552e41f4b71Sopenharmony_ci| Promise\<[NetAddress](#netaddress)\> | Promise used to return the result.|
553e41f4b71Sopenharmony_ci
554e41f4b71Sopenharmony_ci**Error codes**
555e41f4b71Sopenharmony_ci
556e41f4b71Sopenharmony_ci| ID| Error Message                                   |
557e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- |
558e41f4b71Sopenharmony_ci| 2300002  | System internal error.                      |
559e41f4b71Sopenharmony_ci| 2301009  | Bad file descriptor.                            |
560e41f4b71Sopenharmony_ci| 2303188  | Socket operation on non-socket. |
561e41f4b71Sopenharmony_ci
562e41f4b71Sopenharmony_ci**Example**
563e41f4b71Sopenharmony_ci
564e41f4b71Sopenharmony_ci```ts
565e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
566e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
567e41f4b71Sopenharmony_ci
568e41f4b71Sopenharmony_cilet udp: socket.UDPSocket = socket.constructUDPSocketInstance();
569e41f4b71Sopenharmony_ci
570e41f4b71Sopenharmony_cilet bindAddr: socket.NetAddress = {
571e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
572e41f4b71Sopenharmony_ci  port: 8080
573e41f4b71Sopenharmony_ci}
574e41f4b71Sopenharmony_ciudp.bind(bindAddr).then(() => {
575e41f4b71Sopenharmony_ci  console.info('bind success');
576e41f4b71Sopenharmony_ci  udp.getLocalAddress().then((localAddress: socket.NetAddress) => {
577e41f4b71Sopenharmony_ci        console.info("UDP_Socket get SUCCESS! Address: " + JSON.stringify(localAddress));
578e41f4b71Sopenharmony_ci      }).catch((err: BusinessError) => {
579e41f4b71Sopenharmony_ci        console.error("UDP_Socket get FAILED! Error: " + JSON.stringify(err));
580e41f4b71Sopenharmony_ci      })
581e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
582e41f4b71Sopenharmony_ci  console.error('bind fail');
583e41f4b71Sopenharmony_ci});
584e41f4b71Sopenharmony_ci```
585e41f4b71Sopenharmony_ci
586e41f4b71Sopenharmony_ci### on('message')
587e41f4b71Sopenharmony_ci
588e41f4b71Sopenharmony_cion(type: 'message', callback: Callback\<SocketMessageInfo\>): void
589e41f4b71Sopenharmony_ci
590e41f4b71Sopenharmony_ciSubscribes to **message** events of the UDP socket connection. This API uses an asynchronous callback to return the result.
591e41f4b71Sopenharmony_ci
592e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
593e41f4b71Sopenharmony_ci
594e41f4b71Sopenharmony_ci**Parameters**
595e41f4b71Sopenharmony_ci
596e41f4b71Sopenharmony_ci| Name  | Type                                                        | Mandatory| Description                                     |
597e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------- |
598e41f4b71Sopenharmony_ci| type     | string                                                       | Yes  | Event type.<br/> **message**: message receiving event.|
599e41f4b71Sopenharmony_ci| callback | Callback\<[SocketMessageInfo](#socketmessageinfo11)\> | Yes  | Callback used to return the result.        |
600e41f4b71Sopenharmony_ci
601e41f4b71Sopenharmony_ci**Example**
602e41f4b71Sopenharmony_ci
603e41f4b71Sopenharmony_ci```ts
604e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
605e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
606e41f4b71Sopenharmony_ci
607e41f4b71Sopenharmony_cilet udp: socket.UDPSocket = socket.constructUDPSocketInstance();
608e41f4b71Sopenharmony_ci
609e41f4b71Sopenharmony_cilet messageView = '';
610e41f4b71Sopenharmony_ciudp.on('message', (value: socket.SocketMessageInfo) => {
611e41f4b71Sopenharmony_ci  for (let i: number = 0; i < value.message.byteLength; i++) {
612e41f4b71Sopenharmony_ci    let uint8Array = new Uint8Array(value.message) 
613e41f4b71Sopenharmony_ci    let messages = uint8Array[i]
614e41f4b71Sopenharmony_ci    let message = String.fromCharCode(messages);
615e41f4b71Sopenharmony_ci    messageView += message;
616e41f4b71Sopenharmony_ci  }
617e41f4b71Sopenharmony_ci  console.log('on message message: ' + JSON.stringify(messageView));
618e41f4b71Sopenharmony_ci  console.log('remoteInfo: ' + JSON.stringify(value.remoteInfo));
619e41f4b71Sopenharmony_ci});
620e41f4b71Sopenharmony_ci```
621e41f4b71Sopenharmony_ci
622e41f4b71Sopenharmony_ci### off('message')
623e41f4b71Sopenharmony_ci
624e41f4b71Sopenharmony_cioff(type: 'message', callback?: Callback\<SocketMessageInfo\>): void
625e41f4b71Sopenharmony_ci
626e41f4b71Sopenharmony_ciUnsubscribes from **message** events of the UDP socket connection. This API uses an asynchronous callback to return the result.
627e41f4b71Sopenharmony_ci
628e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
629e41f4b71Sopenharmony_ci
630e41f4b71Sopenharmony_ci**Parameters**
631e41f4b71Sopenharmony_ci
632e41f4b71Sopenharmony_ci| Name  | Type                                                        | Mandatory| Description                                     |
633e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------- |
634e41f4b71Sopenharmony_ci| type     | string                                                       | Yes  | Event type.<br/> **message**: message receiving event.|
635e41f4b71Sopenharmony_ci| callback | Callback\<[SocketMessageInfo](#socketmessageinfo11)\> | No  | Callback used to return the result.                               |
636e41f4b71Sopenharmony_ci
637e41f4b71Sopenharmony_ci**Example**
638e41f4b71Sopenharmony_ci
639e41f4b71Sopenharmony_ci```ts
640e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
641e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
642e41f4b71Sopenharmony_ci
643e41f4b71Sopenharmony_cilet udp: socket.UDPSocket = socket.constructUDPSocketInstance();
644e41f4b71Sopenharmony_cilet messageView = '';
645e41f4b71Sopenharmony_cilet callback = (value: socket.SocketMessageInfo) => {
646e41f4b71Sopenharmony_ci  for (let i: number = 0; i < value.message.byteLength; i++) {
647e41f4b71Sopenharmony_ci    let uint8Array = new Uint8Array(value.message) 
648e41f4b71Sopenharmony_ci    let messages = uint8Array[i]
649e41f4b71Sopenharmony_ci    let message = String.fromCharCode(messages);
650e41f4b71Sopenharmony_ci    messageView += message;
651e41f4b71Sopenharmony_ci  }
652e41f4b71Sopenharmony_ci  console.log('on message message: ' + JSON.stringify(messageView));
653e41f4b71Sopenharmony_ci  console.log('remoteInfo: ' + JSON.stringify(value.remoteInfo));
654e41f4b71Sopenharmony_ci}
655e41f4b71Sopenharmony_ciudp.on('message', callback);
656e41f4b71Sopenharmony_ci// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
657e41f4b71Sopenharmony_ciudp.off('message', callback);
658e41f4b71Sopenharmony_ciudp.off('message');
659e41f4b71Sopenharmony_ci```
660e41f4b71Sopenharmony_ci
661e41f4b71Sopenharmony_ci### on('listening' | 'close')
662e41f4b71Sopenharmony_ci
663e41f4b71Sopenharmony_cion(type: 'listening' | 'close', callback: Callback\<void\>): void
664e41f4b71Sopenharmony_ci
665e41f4b71Sopenharmony_ciSubscribes to **listening** events or **close** events of the UDP socket connection. This API uses an asynchronous callback to return the result.
666e41f4b71Sopenharmony_ci
667e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
668e41f4b71Sopenharmony_ci
669e41f4b71Sopenharmony_ci**Parameters**
670e41f4b71Sopenharmony_ci
671e41f4b71Sopenharmony_ci| Name  | Type            | Mandatory| Description                                                        |
672e41f4b71Sopenharmony_ci| -------- | ---------------- | ---- | ------------------------------------------------------------ |
673e41f4b71Sopenharmony_ci| type     | string           | Yes  | Event type.<br>- **listening**: data packet message event.<br>- **close**: close event.|
674e41f4b71Sopenharmony_ci| callback | Callback\<void\> | Yes  | Callback used to return the result.            |
675e41f4b71Sopenharmony_ci
676e41f4b71Sopenharmony_ci**Example**
677e41f4b71Sopenharmony_ci
678e41f4b71Sopenharmony_ci```ts
679e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
680e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
681e41f4b71Sopenharmony_ci
682e41f4b71Sopenharmony_cilet udp: socket.UDPSocket = socket.constructUDPSocketInstance();
683e41f4b71Sopenharmony_ciudp.on('listening', () => {
684e41f4b71Sopenharmony_ci  console.log("on listening success");
685e41f4b71Sopenharmony_ci});
686e41f4b71Sopenharmony_ciudp.on('close', () => {
687e41f4b71Sopenharmony_ci  console.log("on close success");
688e41f4b71Sopenharmony_ci});
689e41f4b71Sopenharmony_ci```
690e41f4b71Sopenharmony_ci
691e41f4b71Sopenharmony_ci### off('listening' | 'close')
692e41f4b71Sopenharmony_ci
693e41f4b71Sopenharmony_cioff(type: 'listening' | 'close', callback?: Callback\<void\>): void
694e41f4b71Sopenharmony_ci
695e41f4b71Sopenharmony_ciUnsubscribes from **listening** events or **close** events of the UDP socket connection. This API uses an asynchronous callback to return the result.
696e41f4b71Sopenharmony_ci
697e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
698e41f4b71Sopenharmony_ci
699e41f4b71Sopenharmony_ci**Parameters**
700e41f4b71Sopenharmony_ci
701e41f4b71Sopenharmony_ci| Name  | Type            | Mandatory| Description                                                        |
702e41f4b71Sopenharmony_ci| -------- | ---------------- | ---- | ------------------------------------------------------------ |
703e41f4b71Sopenharmony_ci| type     | string           | Yes  | Event type.<br>- **listening**: data packet message event.<br>- **close**: close event.|
704e41f4b71Sopenharmony_ci| callback | Callback\<void\> | No  | Callback used to return the result.      |
705e41f4b71Sopenharmony_ci
706e41f4b71Sopenharmony_ci**Example**
707e41f4b71Sopenharmony_ci
708e41f4b71Sopenharmony_ci```ts
709e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
710e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
711e41f4b71Sopenharmony_ci
712e41f4b71Sopenharmony_cilet udp: socket.UDPSocket = socket.constructUDPSocketInstance();
713e41f4b71Sopenharmony_cilet callback1 = () => {
714e41f4b71Sopenharmony_ci  console.log("on listening, success");
715e41f4b71Sopenharmony_ci}
716e41f4b71Sopenharmony_ciudp.on('listening', callback1);
717e41f4b71Sopenharmony_ci// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
718e41f4b71Sopenharmony_ciudp.off('listening', callback1);
719e41f4b71Sopenharmony_ciudp.off('listening');
720e41f4b71Sopenharmony_cilet callback2 = () => {
721e41f4b71Sopenharmony_ci  console.log("on close, success");
722e41f4b71Sopenharmony_ci}
723e41f4b71Sopenharmony_ciudp.on('close', callback2);
724e41f4b71Sopenharmony_ci// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
725e41f4b71Sopenharmony_ciudp.off('close', callback2);
726e41f4b71Sopenharmony_ciudp.off('close');
727e41f4b71Sopenharmony_ci```
728e41f4b71Sopenharmony_ci
729e41f4b71Sopenharmony_ci### on('error')
730e41f4b71Sopenharmony_ci
731e41f4b71Sopenharmony_cion(type: 'error', callback: ErrorCallback): void
732e41f4b71Sopenharmony_ci
733e41f4b71Sopenharmony_ciSubscribes to **error** events of the UDP socket connection. This API uses an asynchronous callback to return the result.
734e41f4b71Sopenharmony_ci
735e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
736e41f4b71Sopenharmony_ci
737e41f4b71Sopenharmony_ci**Parameters**
738e41f4b71Sopenharmony_ci
739e41f4b71Sopenharmony_ci| Name  | Type         | Mandatory| Description                                |
740e41f4b71Sopenharmony_ci| -------- | ------------- | ---- | ------------------------------------ |
741e41f4b71Sopenharmony_ci| type     | string        | Yes  | Event type.<br/> **error**: error event.|
742e41f4b71Sopenharmony_ci| callback | ErrorCallback | Yes  | Callback used to return the result.                       |
743e41f4b71Sopenharmony_ci
744e41f4b71Sopenharmony_ci**Example**
745e41f4b71Sopenharmony_ci
746e41f4b71Sopenharmony_ci```ts
747e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
748e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
749e41f4b71Sopenharmony_ci
750e41f4b71Sopenharmony_cilet udp: socket.UDPSocket = socket.constructUDPSocketInstance();
751e41f4b71Sopenharmony_ciudp.on('error', (err: BusinessError) => {
752e41f4b71Sopenharmony_ci  console.log("on error, err:" + JSON.stringify(err))
753e41f4b71Sopenharmony_ci});
754e41f4b71Sopenharmony_ci```
755e41f4b71Sopenharmony_ci
756e41f4b71Sopenharmony_ci### off('error')
757e41f4b71Sopenharmony_ci
758e41f4b71Sopenharmony_cioff(type: 'error', callback?: ErrorCallback): void
759e41f4b71Sopenharmony_ci
760e41f4b71Sopenharmony_ciUnsubscribes from **error** events of the UDP socket connection. This API uses an asynchronous callback to return the result.
761e41f4b71Sopenharmony_ci
762e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
763e41f4b71Sopenharmony_ci
764e41f4b71Sopenharmony_ci**Parameters**
765e41f4b71Sopenharmony_ci
766e41f4b71Sopenharmony_ci| Name  | Type         | Mandatory| Description                                |
767e41f4b71Sopenharmony_ci| -------- | ------------- | ---- | ------------------------------------ |
768e41f4b71Sopenharmony_ci| type     | string        | Yes  | Event type.<br/> **error**: error event.|
769e41f4b71Sopenharmony_ci| callback | ErrorCallback | No  | Callback used to return the result.       |
770e41f4b71Sopenharmony_ci
771e41f4b71Sopenharmony_ci**Example**
772e41f4b71Sopenharmony_ci
773e41f4b71Sopenharmony_ci```ts
774e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
775e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
776e41f4b71Sopenharmony_ci
777e41f4b71Sopenharmony_cilet udp: socket.UDPSocket = socket.constructUDPSocketInstance();
778e41f4b71Sopenharmony_cilet callback = (err: BusinessError) => {
779e41f4b71Sopenharmony_ci  console.log("on error, err:" + JSON.stringify(err));
780e41f4b71Sopenharmony_ci}
781e41f4b71Sopenharmony_ciudp.on('error', callback);
782e41f4b71Sopenharmony_ci// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
783e41f4b71Sopenharmony_ciudp.off('error', callback);
784e41f4b71Sopenharmony_ciudp.off('error');
785e41f4b71Sopenharmony_ci```
786e41f4b71Sopenharmony_ci
787e41f4b71Sopenharmony_ci## NetAddress
788e41f4b71Sopenharmony_ci
789e41f4b71Sopenharmony_ciDefines the destination address.
790e41f4b71Sopenharmony_ci
791e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
792e41f4b71Sopenharmony_ci
793e41f4b71Sopenharmony_ci| Name | Type  | Mandatory| Description                                                        |
794e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ------------------------------------------------------------ |
795e41f4b71Sopenharmony_ci| address<sup>11+</sup> | string | Yes  | Bound IP address.                                          |
796e41f4b71Sopenharmony_ci| port    | number | No  | Port number. The value ranges from **0** to **65535**. If this parameter is not specified, the system randomly allocates a port.          |
797e41f4b71Sopenharmony_ci| family  | number | No  | Network protocol type.<br>- **1**: IPv4<br>- **2**: IPv6<br>The default value is **1**. For an IPv6 address, this field must be explicitly set to **2**.|
798e41f4b71Sopenharmony_ci## UDPSendOptions
799e41f4b71Sopenharmony_ci
800e41f4b71Sopenharmony_ciDefines the parameters for sending data over a UDP socket connection.
801e41f4b71Sopenharmony_ci
802e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
803e41f4b71Sopenharmony_ci
804e41f4b71Sopenharmony_ci| Name | Type                              | Mandatory| Description          |
805e41f4b71Sopenharmony_ci| ------- | ---------------------------------- | ---- | -------------- |
806e41f4b71Sopenharmony_ci| data    | string \| ArrayBuffer                          | Yes  | Data to send.  |
807e41f4b71Sopenharmony_ci| address | [NetAddress](#netaddress) | Yes  | Destination address.|
808e41f4b71Sopenharmony_ci
809e41f4b71Sopenharmony_ci## UDPExtraOptions
810e41f4b71Sopenharmony_ci
811e41f4b71Sopenharmony_ciDefines other properties of the UDP socket connection. This API inherits from [ExtraOptionsBase](#extraoptionsbase7).
812e41f4b71Sopenharmony_ci
813e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
814e41f4b71Sopenharmony_ci
815e41f4b71Sopenharmony_ci| Name           | Type   | Mandatory| Description                            |
816e41f4b71Sopenharmony_ci| ----------------- | ------- | ---- | -------------------------------- |
817e41f4b71Sopenharmony_ci| broadcast         | boolean | No  | Whether to send broadcast messages. The default value is **false**. |
818e41f4b71Sopenharmony_ci
819e41f4b71Sopenharmony_ci## SocketMessageInfo<sup>11+</sup>
820e41f4b71Sopenharmony_ci
821e41f4b71Sopenharmony_ciDefines the socket connection information.
822e41f4b71Sopenharmony_ci
823e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
824e41f4b71Sopenharmony_ci
825e41f4b71Sopenharmony_ci| Name       | Type  | Mandatory| Description                                 |
826e41f4b71Sopenharmony_ci| ---------- | ------ | ---- | ------------------------------------- |
827e41f4b71Sopenharmony_ci| message    | ArrayBuffer | Yes  | Received **message** event.|
828e41f4b71Sopenharmony_ci| remoteInfo | [SocketRemoteInfo](#socketremoteinfo) | Yes  | Socket connection information.|
829e41f4b71Sopenharmony_ci
830e41f4b71Sopenharmony_ci## SocketStateBase
831e41f4b71Sopenharmony_ci
832e41f4b71Sopenharmony_ciDefines the status of the socket connection.
833e41f4b71Sopenharmony_ci
834e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
835e41f4b71Sopenharmony_ci
836e41f4b71Sopenharmony_ci| Name     | Type   | Mandatory| Description      |
837e41f4b71Sopenharmony_ci| ----------- | ------- | ---- | ---------- |
838e41f4b71Sopenharmony_ci| isBound     | boolean | Yes  | Whether the connection is in the bound state.|
839e41f4b71Sopenharmony_ci| isClose     | boolean | Yes  | Whether the connection is in the closed state.|
840e41f4b71Sopenharmony_ci| isConnected | boolean | Yes  | Whether the connection is in the connected state.|
841e41f4b71Sopenharmony_ci
842e41f4b71Sopenharmony_ci## SocketRemoteInfo
843e41f4b71Sopenharmony_ci
844e41f4b71Sopenharmony_ciDefines information about the socket connection.
845e41f4b71Sopenharmony_ci
846e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
847e41f4b71Sopenharmony_ci
848e41f4b71Sopenharmony_ci| Name | Type  | Mandatory| Description                                                        |
849e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ------------------------------------------------------------ |
850e41f4b71Sopenharmony_ci| address | string | Yes  | Bound IP address.                                          |
851e41f4b71Sopenharmony_ci| family  | 'IPv4' \| 'IPv6' | Yes  | Network protocol type.<br>- IPv4<br>- IPv6<br>The default value is **IPv4**.|
852e41f4b71Sopenharmony_ci| port    | number | Yes  | Port number. The value ranges from **0** to **65535**.                                       |
853e41f4b71Sopenharmony_ci| size    | number | Yes  | Length of the server response message, in bytes.                                  |
854e41f4b71Sopenharmony_ci
855e41f4b71Sopenharmony_ci## Description of UDP Error Codes
856e41f4b71Sopenharmony_ci
857e41f4b71Sopenharmony_ciThe UDP error code mapping is in the format of 2301000 + Linux kernel error code.
858e41f4b71Sopenharmony_ci
859e41f4b71Sopenharmony_ciFor details about error codes, see [Socket Error Codes](errorcode-net-socket.md).
860e41f4b71Sopenharmony_ci
861e41f4b71Sopenharmony_ci## socket.constructMulticastSocketInstance<sup>11+</sup>
862e41f4b71Sopenharmony_ci
863e41f4b71Sopenharmony_ciconstructMulticastSocketInstance(): MulticastSocket
864e41f4b71Sopenharmony_ci
865e41f4b71Sopenharmony_ciCreates a **MulticastSocket** object.
866e41f4b71Sopenharmony_ci
867e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
868e41f4b71Sopenharmony_ci
869e41f4b71Sopenharmony_ci**Return value**
870e41f4b71Sopenharmony_ci
871e41f4b71Sopenharmony_ci| Type                              | Description                   |
872e41f4b71Sopenharmony_ci| ----------------------------------- | ----------------------------- |
873e41f4b71Sopenharmony_ci| [MulticastSocket](#multicastsocket11) | **MulticastSocket** object.|
874e41f4b71Sopenharmony_ci
875e41f4b71Sopenharmony_ci**Example**
876e41f4b71Sopenharmony_ci
877e41f4b71Sopenharmony_ci```ts
878e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
879e41f4b71Sopenharmony_cilet multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance();
880e41f4b71Sopenharmony_ci```
881e41f4b71Sopenharmony_ci## MulticastSocket<sup>11+</sup>
882e41f4b71Sopenharmony_ci
883e41f4b71Sopenharmony_ciDefines a **MulticastSocket** connection. Before calling MulticastSocket APIs, you need to call [socket.constructMulticastSocketInstance](#socketconstructmulticastsocketinstance11) to create a **MulticastSocket** object.
884e41f4b71Sopenharmony_ci
885e41f4b71Sopenharmony_ci### addMembership<sup>11+</sup>
886e41f4b71Sopenharmony_ci
887e41f4b71Sopenharmony_ciaddMembership(multicastAddress: NetAddress, callback: AsyncCallback\<void\>): void;
888e41f4b71Sopenharmony_ci
889e41f4b71Sopenharmony_ciAdds a member to a multicast group. This API uses an asynchronous callback to return the result.
890e41f4b71Sopenharmony_ci
891e41f4b71Sopenharmony_ci> **NOTE**
892e41f4b71Sopenharmony_ci> The IP addresses used for multicast belong to a specific range, for example, 224.0.0.0 to 239.255.255.255.
893e41f4b71Sopenharmony_ci> A member in a multicast group can serve as a sender or a receiver. Data is transmitted in broadcast mode, regardless of the client or server.
894e41f4b71Sopenharmony_ci
895e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
896e41f4b71Sopenharmony_ci
897e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
898e41f4b71Sopenharmony_ci
899e41f4b71Sopenharmony_ci**Parameters**
900e41f4b71Sopenharmony_ci
901e41f4b71Sopenharmony_ci| Name            | Type                          | Mandatory| Description                                      |
902e41f4b71Sopenharmony_ci| ----------------- | ----------------------------- | ---- | ----------------------------------------- |
903e41f4b71Sopenharmony_ci| multicastAddress  | [NetAddress](#netaddress)     |  Yes | Destination address. For details, see [NetAddress](#netaddress).|
904e41f4b71Sopenharmony_ci| callback          | AsyncCallback\<void\>         |  Yes | Callback used to return the result. If the operation fails, an error message is returned.                                |
905e41f4b71Sopenharmony_ci
906e41f4b71Sopenharmony_ci**Error codes**
907e41f4b71Sopenharmony_ci
908e41f4b71Sopenharmony_ci| ID| Error Message                |
909e41f4b71Sopenharmony_ci| ------- | ----------------------- |
910e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
911e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
912e41f4b71Sopenharmony_ci| 2301022 | Invalid argument.       |
913e41f4b71Sopenharmony_ci| 2301088 | Not a socket.           |
914e41f4b71Sopenharmony_ci| 2301098 | Address in use.         |
915e41f4b71Sopenharmony_ci
916e41f4b71Sopenharmony_ci**Example**
917e41f4b71Sopenharmony_ci
918e41f4b71Sopenharmony_ci```ts
919e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
920e41f4b71Sopenharmony_ci
921e41f4b71Sopenharmony_cilet multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance();
922e41f4b71Sopenharmony_cilet addr: socket.NetAddress = {
923e41f4b71Sopenharmony_ci  address: '239.255.0.1',
924e41f4b71Sopenharmony_ci  port: 8080
925e41f4b71Sopenharmony_ci}
926e41f4b71Sopenharmony_cimulticast.addMembership(addr, (err: Object) => {
927e41f4b71Sopenharmony_ci  if (err) {
928e41f4b71Sopenharmony_ci    console.log('add membership fail, err: ' + JSON.stringify(err));
929e41f4b71Sopenharmony_ci    return;
930e41f4b71Sopenharmony_ci  }
931e41f4b71Sopenharmony_ci  console.log('add membership success');
932e41f4b71Sopenharmony_ci})
933e41f4b71Sopenharmony_ci```
934e41f4b71Sopenharmony_ci
935e41f4b71Sopenharmony_ci### addMembership<sup>11+</sup>
936e41f4b71Sopenharmony_ci
937e41f4b71Sopenharmony_ciaddMembership(multicastAddress: NetAddress): Promise\<void\>;
938e41f4b71Sopenharmony_ci
939e41f4b71Sopenharmony_ciAdds a member to a multicast group. This API uses a promise to return the result.
940e41f4b71Sopenharmony_ci
941e41f4b71Sopenharmony_ci> **NOTE**
942e41f4b71Sopenharmony_ci> The IP addresses used for multicast belong to a specific range, for example, 224.0.0.0 to 239.255.255.255.
943e41f4b71Sopenharmony_ci> A member in a multicast group can serve as a sender or a receiver. Data is transmitted in broadcast mode, regardless of the client or server.
944e41f4b71Sopenharmony_ci
945e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
946e41f4b71Sopenharmony_ci
947e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
948e41f4b71Sopenharmony_ci
949e41f4b71Sopenharmony_ci**Parameters**
950e41f4b71Sopenharmony_ci
951e41f4b71Sopenharmony_ci| Name            | Type                          | Mandatory| Description                                          |
952e41f4b71Sopenharmony_ci| ----------------- | ----------------------------- | ---- | --------------------------------------------  |
953e41f4b71Sopenharmony_ci| multicastAddress  | [NetAddress](#netaddress)     |  Yes | Destination address. For details, see [NetAddress](#netaddress).|
954e41f4b71Sopenharmony_ci
955e41f4b71Sopenharmony_ci**Return value**
956e41f4b71Sopenharmony_ci
957e41f4b71Sopenharmony_ci| Type           | Description                                              |
958e41f4b71Sopenharmony_ci|  -------------- |  -----------------------------------------------  |
959e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result.|
960e41f4b71Sopenharmony_ci
961e41f4b71Sopenharmony_ci**Error codes**
962e41f4b71Sopenharmony_ci
963e41f4b71Sopenharmony_ci| ID| Error Message                |
964e41f4b71Sopenharmony_ci| ------- | ----------------------- |
965e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
966e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
967e41f4b71Sopenharmony_ci| 2301088 | Not a socket.           |
968e41f4b71Sopenharmony_ci| 2301098 | Address in use.         |
969e41f4b71Sopenharmony_ci
970e41f4b71Sopenharmony_ci**Example**
971e41f4b71Sopenharmony_ci
972e41f4b71Sopenharmony_ci```ts
973e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
974e41f4b71Sopenharmony_ci
975e41f4b71Sopenharmony_cilet multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance();
976e41f4b71Sopenharmony_cilet addr: socket.NetAddress = {
977e41f4b71Sopenharmony_ci  address: '239.255.0.1',
978e41f4b71Sopenharmony_ci  port: 8080
979e41f4b71Sopenharmony_ci}
980e41f4b71Sopenharmony_cimulticast.addMembership(addr).then(() => {
981e41f4b71Sopenharmony_ci  console.log('addMembership success');
982e41f4b71Sopenharmony_ci}).catch((err: Object) => {
983e41f4b71Sopenharmony_ci  console.log('addMembership fail');
984e41f4b71Sopenharmony_ci});
985e41f4b71Sopenharmony_ci```
986e41f4b71Sopenharmony_ci
987e41f4b71Sopenharmony_ci### dropMembership<sup>11+</sup>
988e41f4b71Sopenharmony_ci
989e41f4b71Sopenharmony_cidropMembership(multicastAddress: NetAddress, callback: AsyncCallback\<void\>): void;
990e41f4b71Sopenharmony_ci
991e41f4b71Sopenharmony_ciDrops a member from a multicast group. This API uses an asynchronous callback to return the result.
992e41f4b71Sopenharmony_ci
993e41f4b71Sopenharmony_ci> **NOTE**
994e41f4b71Sopenharmony_ci> The IP addresses used for multicast belong to a specific range, for example, 224.0.0.0 to 239.255.255.255.
995e41f4b71Sopenharmony_ci> You can drop only a member that has been added to a multicast group by using [addMembership](#addmembership11).
996e41f4b71Sopenharmony_ci
997e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
998e41f4b71Sopenharmony_ci
999e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
1000e41f4b71Sopenharmony_ci
1001e41f4b71Sopenharmony_ci**Parameters**
1002e41f4b71Sopenharmony_ci
1003e41f4b71Sopenharmony_ci| Name            | Type                          | Mandatory| Description                                        |
1004e41f4b71Sopenharmony_ci| ----------------- | ----------------------------- | ---- | ------------------------------------------- |
1005e41f4b71Sopenharmony_ci| multicastAddress  | [NetAddress](#netaddress)     |  Yes | Destination address. For details, see [NetAddress](#netaddress).  |
1006e41f4b71Sopenharmony_ci| callback          | AsyncCallback\<void\>         |  Yes | Callback used to return the result. If the operation fails, an error message is returned.|
1007e41f4b71Sopenharmony_ci
1008e41f4b71Sopenharmony_ci**Error codes**
1009e41f4b71Sopenharmony_ci
1010e41f4b71Sopenharmony_ci| ID| Error Message                |
1011e41f4b71Sopenharmony_ci| ------- | ----------------------- |
1012e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
1013e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
1014e41f4b71Sopenharmony_ci| 2301088 | Not a socket.           |
1015e41f4b71Sopenharmony_ci| 2301098 | Address in use.         |
1016e41f4b71Sopenharmony_ci
1017e41f4b71Sopenharmony_ci**Example**
1018e41f4b71Sopenharmony_ci
1019e41f4b71Sopenharmony_ci```ts
1020e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
1021e41f4b71Sopenharmony_ci
1022e41f4b71Sopenharmony_cilet multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance();
1023e41f4b71Sopenharmony_cilet addr: socket.NetAddress = {
1024e41f4b71Sopenharmony_ci  address: '239.255.0.1',
1025e41f4b71Sopenharmony_ci  port: 8080
1026e41f4b71Sopenharmony_ci}
1027e41f4b71Sopenharmony_cimulticast.dropMembership(addr, (err: Object) => {
1028e41f4b71Sopenharmony_ci  if (err) {
1029e41f4b71Sopenharmony_ci    console.log('drop membership fail, err: ' + JSON.stringify(err));
1030e41f4b71Sopenharmony_ci    return;
1031e41f4b71Sopenharmony_ci  }
1032e41f4b71Sopenharmony_ci  console.log('drop membership success');
1033e41f4b71Sopenharmony_ci})
1034e41f4b71Sopenharmony_ci```
1035e41f4b71Sopenharmony_ci
1036e41f4b71Sopenharmony_ci### dropMembership<sup>11+</sup>
1037e41f4b71Sopenharmony_ci
1038e41f4b71Sopenharmony_cidropMembership(multicastAddress: NetAddress): Promise\<void\>;
1039e41f4b71Sopenharmony_ci
1040e41f4b71Sopenharmony_ciDrops a member from a multicast group. This API uses a promise to return the result.
1041e41f4b71Sopenharmony_ci
1042e41f4b71Sopenharmony_ci> **NOTE**
1043e41f4b71Sopenharmony_ci> The IP addresses used for multicast belong to a specific range, for example, 224.0.0.0 to 239.255.255.255.
1044e41f4b71Sopenharmony_ci> You can drop only a member that has been added to a multicast group by using [addMembership](#addmembership11).
1045e41f4b71Sopenharmony_ci
1046e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
1047e41f4b71Sopenharmony_ci
1048e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
1049e41f4b71Sopenharmony_ci
1050e41f4b71Sopenharmony_ci**Parameters**
1051e41f4b71Sopenharmony_ci
1052e41f4b71Sopenharmony_ci| Name            | Type                                  | Mandatory| Description                                          |
1053e41f4b71Sopenharmony_ci| ----------------- | ------------------------------------- | ---- | --------------------------------------------  |
1054e41f4b71Sopenharmony_ci| multicastAddress  | [NetAddress](#netaddress) |  Yes | Destination address. For details, see [NetAddress](#netaddress).    |
1055e41f4b71Sopenharmony_ci
1056e41f4b71Sopenharmony_ci**Return value**
1057e41f4b71Sopenharmony_ci
1058e41f4b71Sopenharmony_ci| Type           | Description                                             |
1059e41f4b71Sopenharmony_ci|  -------------- |  ----------------------------------------------- |
1060e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result.|
1061e41f4b71Sopenharmony_ci
1062e41f4b71Sopenharmony_ci**Error codes**
1063e41f4b71Sopenharmony_ci
1064e41f4b71Sopenharmony_ci| ID| Error Message                |
1065e41f4b71Sopenharmony_ci| ------- | ----------------------- |
1066e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
1067e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
1068e41f4b71Sopenharmony_ci| 2301088 | Not a socket.           |
1069e41f4b71Sopenharmony_ci| 2301098 | Address in use.         |
1070e41f4b71Sopenharmony_ci
1071e41f4b71Sopenharmony_ci**Example**
1072e41f4b71Sopenharmony_ci
1073e41f4b71Sopenharmony_ci```ts
1074e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
1075e41f4b71Sopenharmony_ci
1076e41f4b71Sopenharmony_cilet multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance();
1077e41f4b71Sopenharmony_cilet addr: socket.NetAddress = {
1078e41f4b71Sopenharmony_ci  address: '239.255.0.1',
1079e41f4b71Sopenharmony_ci  port: 8080
1080e41f4b71Sopenharmony_ci}
1081e41f4b71Sopenharmony_cimulticast.dropMembership(addr).then(() => {
1082e41f4b71Sopenharmony_ci  console.log('drop membership success');
1083e41f4b71Sopenharmony_ci}).catch((err: Object) => {
1084e41f4b71Sopenharmony_ci  console.log('drop membership fail');
1085e41f4b71Sopenharmony_ci});
1086e41f4b71Sopenharmony_ci```
1087e41f4b71Sopenharmony_ci
1088e41f4b71Sopenharmony_ci### setMulticastTTL<sup>11+</sup>
1089e41f4b71Sopenharmony_ci
1090e41f4b71Sopenharmony_cisetMulticastTTL(ttl: number, callback: AsyncCallback\<void\>): void;
1091e41f4b71Sopenharmony_ci
1092e41f4b71Sopenharmony_ciSets the time to live (TTL) for multicast packets. This API uses an asynchronous callback to return the result.
1093e41f4b71Sopenharmony_ci
1094e41f4b71Sopenharmony_ci> **NOTE**
1095e41f4b71Sopenharmony_ci> TTL is used to limit the maximum number of router hops for packet transmission on a network.
1096e41f4b71Sopenharmony_ci> The value ranges from 0 to 255. The default value is **1**.
1097e41f4b71Sopenharmony_ci> If the TTL value is **1**, multicast packets can be transmitted only to the host directly connected to the sender. If the TTL is set to a large value, multicast packets can be transmitted over a longer distance.
1098e41f4b71Sopenharmony_ci> This API is effective only after [addMembership](#addmembership11) is called.
1099e41f4b71Sopenharmony_ci
1100e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
1101e41f4b71Sopenharmony_ci
1102e41f4b71Sopenharmony_ci**Parameters**
1103e41f4b71Sopenharmony_ci
1104e41f4b71Sopenharmony_ci| Name        | Type                  | Mandatory| Description                        |
1105e41f4b71Sopenharmony_ci| ------------- | --------------------- | ---- | ----------------------------- |
1106e41f4b71Sopenharmony_ci| ttl           | number                |  Yes | TTL value. The value is of the number type.|
1107e41f4b71Sopenharmony_ci| callback      | AsyncCallback\<void\> |  Yes | Callback used to return the result. If the operation fails, an error message is returned.   |
1108e41f4b71Sopenharmony_ci
1109e41f4b71Sopenharmony_ci**Error codes**
1110e41f4b71Sopenharmony_ci
1111e41f4b71Sopenharmony_ci| ID| Error Message                |
1112e41f4b71Sopenharmony_ci| ------- | ----------------------- |
1113e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
1114e41f4b71Sopenharmony_ci| 2301022 | Invalid argument.       |
1115e41f4b71Sopenharmony_ci| 2301088 | Not a socket.           |
1116e41f4b71Sopenharmony_ci
1117e41f4b71Sopenharmony_ci**Example**
1118e41f4b71Sopenharmony_ci
1119e41f4b71Sopenharmony_ci```ts
1120e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
1121e41f4b71Sopenharmony_ci
1122e41f4b71Sopenharmony_cilet multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance();
1123e41f4b71Sopenharmony_cilet ttl = 8
1124e41f4b71Sopenharmony_cimulticast.setMulticastTTL(ttl, (err: Object) => {
1125e41f4b71Sopenharmony_ci  if (err) {
1126e41f4b71Sopenharmony_ci    console.log('set ttl fail, err: ' + JSON.stringify(err));
1127e41f4b71Sopenharmony_ci    return;
1128e41f4b71Sopenharmony_ci  }
1129e41f4b71Sopenharmony_ci  console.log('set ttl success');
1130e41f4b71Sopenharmony_ci})
1131e41f4b71Sopenharmony_ci```
1132e41f4b71Sopenharmony_ci
1133e41f4b71Sopenharmony_ci### setMulticastTTL<sup>11+</sup>
1134e41f4b71Sopenharmony_ci
1135e41f4b71Sopenharmony_cisetMulticastTTL(ttl: number): Promise\<void\>;
1136e41f4b71Sopenharmony_ci
1137e41f4b71Sopenharmony_ciSets the TTL for multicast packets. This API uses a promise to return the result.
1138e41f4b71Sopenharmony_ci
1139e41f4b71Sopenharmony_ci> **NOTE**
1140e41f4b71Sopenharmony_ci> TTL is used to limit the maximum number of router hops for packet transmission on a network.
1141e41f4b71Sopenharmony_ci> The value ranges from 0 to 255. The default value is **1**.
1142e41f4b71Sopenharmony_ci> If the TTL value is **1**, multicast packets can be transmitted only to the host directly connected to the sender. If the TTL is set to a large value, multicast packets can be transmitted over a longer distance.
1143e41f4b71Sopenharmony_ci> This API is effective only after [addMembership](#addmembership11) is called.
1144e41f4b71Sopenharmony_ci
1145e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
1146e41f4b71Sopenharmony_ci
1147e41f4b71Sopenharmony_ci**Parameters**
1148e41f4b71Sopenharmony_ci
1149e41f4b71Sopenharmony_ci| Name        | Type                  | Mandatory| Description                          |
1150e41f4b71Sopenharmony_ci| ------------- | ---------------------- | ---- | ------------------------------ |
1151e41f4b71Sopenharmony_ci| ttl           | number                 |  Yes | TTL value. The value is of the number type.|
1152e41f4b71Sopenharmony_ci
1153e41f4b71Sopenharmony_ci**Return value**
1154e41f4b71Sopenharmony_ci
1155e41f4b71Sopenharmony_ci| Type           | Description                                            |
1156e41f4b71Sopenharmony_ci|  -------------- |  ---------------------------------------------- |
1157e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result.|
1158e41f4b71Sopenharmony_ci
1159e41f4b71Sopenharmony_ci**Error codes**
1160e41f4b71Sopenharmony_ci
1161e41f4b71Sopenharmony_ci| ID| Error Message                |
1162e41f4b71Sopenharmony_ci| ------- | ----------------------- |
1163e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
1164e41f4b71Sopenharmony_ci| 2301022 | Invalid argument.       |
1165e41f4b71Sopenharmony_ci| 2301088 | Not a socket.           |
1166e41f4b71Sopenharmony_ci
1167e41f4b71Sopenharmony_ci**Example**
1168e41f4b71Sopenharmony_ci
1169e41f4b71Sopenharmony_ci```ts
1170e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
1171e41f4b71Sopenharmony_ci
1172e41f4b71Sopenharmony_cilet multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance();
1173e41f4b71Sopenharmony_cimulticast.setMulticastTTL(8).then(() => {
1174e41f4b71Sopenharmony_ci  console.log('set ttl success');
1175e41f4b71Sopenharmony_ci}).catch((err: Object) => {
1176e41f4b71Sopenharmony_ci  console.log('set ttl failed');
1177e41f4b71Sopenharmony_ci});
1178e41f4b71Sopenharmony_ci```
1179e41f4b71Sopenharmony_ci
1180e41f4b71Sopenharmony_ci### getMulticastTTL<sup>11+</sup>
1181e41f4b71Sopenharmony_ci
1182e41f4b71Sopenharmony_cigetMulticastTTL(callback: AsyncCallback\<number\>): void;
1183e41f4b71Sopenharmony_ci
1184e41f4b71Sopenharmony_ciObtains the TTL for multicast packets. This API uses an asynchronous callback to return the result.
1185e41f4b71Sopenharmony_ci
1186e41f4b71Sopenharmony_ci> **NOTE**
1187e41f4b71Sopenharmony_ci> TTL is used to limit the maximum number of router hops for packet transmission on a network.
1188e41f4b71Sopenharmony_ci> The value ranges from 0 to 255. The default value is **1**.
1189e41f4b71Sopenharmony_ci> If the TTL value is **1**, multicast packets can be transmitted only to the host directly connected to the sender. If the TTL is set to a large value, multicast packets can be transmitted over a longer distance.
1190e41f4b71Sopenharmony_ci> This API is effective only after [addMembership](#addmembership11) is called.
1191e41f4b71Sopenharmony_ci
1192e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
1193e41f4b71Sopenharmony_ci
1194e41f4b71Sopenharmony_ci**Parameters**
1195e41f4b71Sopenharmony_ci
1196e41f4b71Sopenharmony_ci| Name        | Type                    | Mandatory| Description                        |
1197e41f4b71Sopenharmony_ci| ------------- | ----------------------- | ---- | --------------------------- |
1198e41f4b71Sopenharmony_ci| callback      | AsyncCallback\<number\> |  Yes | Callback used to return the result. If the operation fails, an error message is returned. |
1199e41f4b71Sopenharmony_ci
1200e41f4b71Sopenharmony_ci**Error codes**
1201e41f4b71Sopenharmony_ci
1202e41f4b71Sopenharmony_ci| ID| Error Message                |
1203e41f4b71Sopenharmony_ci| ------- | ----------------------- |
1204e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
1205e41f4b71Sopenharmony_ci| 2301088 | Not a socket.           |
1206e41f4b71Sopenharmony_ci
1207e41f4b71Sopenharmony_ci**Example**
1208e41f4b71Sopenharmony_ci
1209e41f4b71Sopenharmony_ci```ts
1210e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
1211e41f4b71Sopenharmony_ci
1212e41f4b71Sopenharmony_cilet multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance();
1213e41f4b71Sopenharmony_cimulticast.getMulticastTTL((err: Object, value: Number) => {
1214e41f4b71Sopenharmony_ci  if (err) {
1215e41f4b71Sopenharmony_ci    console.log('set ttl fail, err: ' + JSON.stringify(err));
1216e41f4b71Sopenharmony_ci    return;
1217e41f4b71Sopenharmony_ci  }
1218e41f4b71Sopenharmony_ci  console.log('set ttl success, value: ' + JSON.stringify(value));
1219e41f4b71Sopenharmony_ci})
1220e41f4b71Sopenharmony_ci```
1221e41f4b71Sopenharmony_ci
1222e41f4b71Sopenharmony_ci### getMulticastTTL<sup>11+</sup>
1223e41f4b71Sopenharmony_ci
1224e41f4b71Sopenharmony_cigetMulticastTTL(): Promise\<number\>;
1225e41f4b71Sopenharmony_ci
1226e41f4b71Sopenharmony_ciObtains the TTL for multicast packets. This API uses a promise to return the result.
1227e41f4b71Sopenharmony_ci
1228e41f4b71Sopenharmony_ci> **NOTE**
1229e41f4b71Sopenharmony_ci> TTL is used to limit the maximum number of router hops for packet transmission on a network.
1230e41f4b71Sopenharmony_ci> The value ranges from 0 to 255. The default value is **1**.
1231e41f4b71Sopenharmony_ci> If the TTL value is **1**, multicast packets can be transmitted only to the host directly connected to the sender. If the TTL is set to a large value, multicast packets can be transmitted over a longer distance.
1232e41f4b71Sopenharmony_ci> This API is effective only after [addMembership](#addmembership11) is called.
1233e41f4b71Sopenharmony_ci
1234e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
1235e41f4b71Sopenharmony_ci
1236e41f4b71Sopenharmony_ci**Return value**
1237e41f4b71Sopenharmony_ci
1238e41f4b71Sopenharmony_ci| Type              | Description                       |
1239e41f4b71Sopenharmony_ci| ----------------   | --------------------------- |
1240e41f4b71Sopenharmony_ci| Promise\<number\> | Promise used to return the result.|
1241e41f4b71Sopenharmony_ci
1242e41f4b71Sopenharmony_ci**Error codes**
1243e41f4b71Sopenharmony_ci
1244e41f4b71Sopenharmony_ci| ID| Error Message               |
1245e41f4b71Sopenharmony_ci| ------- | ----------------------- |
1246e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
1247e41f4b71Sopenharmony_ci| 2301088 | Not a socket.           |
1248e41f4b71Sopenharmony_ci
1249e41f4b71Sopenharmony_ci**Example**
1250e41f4b71Sopenharmony_ci
1251e41f4b71Sopenharmony_ci```ts
1252e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
1253e41f4b71Sopenharmony_ci
1254e41f4b71Sopenharmony_cilet multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance();
1255e41f4b71Sopenharmony_cimulticast.getMulticastTTL().then((value: Number) => {
1256e41f4b71Sopenharmony_ci  console.log('ttl: ', JSON.stringify(value));
1257e41f4b71Sopenharmony_ci}).catch((err: Object) => {
1258e41f4b71Sopenharmony_ci  console.log('set ttl failed');
1259e41f4b71Sopenharmony_ci});
1260e41f4b71Sopenharmony_ci```
1261e41f4b71Sopenharmony_ci
1262e41f4b71Sopenharmony_ci### setLoopbackMode<sup>11+</sup>
1263e41f4b71Sopenharmony_ci
1264e41f4b71Sopenharmony_cisetLoopbackMode(flag: boolean, callback: AsyncCallback\<void\>): void;
1265e41f4b71Sopenharmony_ci
1266e41f4b71Sopenharmony_ciSets the loopback mode flag for multicast communication. This API uses an asynchronous callback to return the result.
1267e41f4b71Sopenharmony_ci
1268e41f4b71Sopenharmony_ci> **NOTE**
1269e41f4b71Sopenharmony_ci> Use this API to enable or disable the loopback mode. By default, the loopback mode is enabled.
1270e41f4b71Sopenharmony_ci> The value **true** indicates that the host is allowed to receive the multicast packets sent by itself, and the value **false** indicates the opposite.
1271e41f4b71Sopenharmony_ci> This API is effective only after [addMembership](#addmembership11) is called.
1272e41f4b71Sopenharmony_ci
1273e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
1274e41f4b71Sopenharmony_ci
1275e41f4b71Sopenharmony_ci**Parameters**
1276e41f4b71Sopenharmony_ci
1277e41f4b71Sopenharmony_ci| Name        | Type                 | Mandatory| Description                        |
1278e41f4b71Sopenharmony_ci| ------------- | --------------------- | ---- | ---------------------------- |
1279e41f4b71Sopenharmony_ci| flag          | boolean               |  Yes | Loopback mode flag. The value is of the Boolean type. |
1280e41f4b71Sopenharmony_ci| callback      | AsyncCallback\<void\> |  Yes | Callback used to return the result. If the operation fails, an error message is returned.   |
1281e41f4b71Sopenharmony_ci
1282e41f4b71Sopenharmony_ci**Error codes**
1283e41f4b71Sopenharmony_ci
1284e41f4b71Sopenharmony_ci| ID| Error Message                |
1285e41f4b71Sopenharmony_ci| ------- | ----------------------- |
1286e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
1287e41f4b71Sopenharmony_ci| 2301088 | Not a socket.           |
1288e41f4b71Sopenharmony_ci
1289e41f4b71Sopenharmony_ci**Example**
1290e41f4b71Sopenharmony_ci
1291e41f4b71Sopenharmony_ci```ts
1292e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
1293e41f4b71Sopenharmony_ci
1294e41f4b71Sopenharmony_cilet multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance();
1295e41f4b71Sopenharmony_cimulticast.setLoopbackMode(false, (err: Object) => {
1296e41f4b71Sopenharmony_ci  if (err) {
1297e41f4b71Sopenharmony_ci    console.log('set loopback mode fail, err: ' + JSON.stringify(err));
1298e41f4b71Sopenharmony_ci    return;
1299e41f4b71Sopenharmony_ci  }
1300e41f4b71Sopenharmony_ci  console.log('set loopback mode success');
1301e41f4b71Sopenharmony_ci})
1302e41f4b71Sopenharmony_ci```
1303e41f4b71Sopenharmony_ci
1304e41f4b71Sopenharmony_ci### setLoopbackMode<sup>11+</sup>
1305e41f4b71Sopenharmony_ci
1306e41f4b71Sopenharmony_cisetLoopbackMode(flag: boolean): Promise\<void\>;
1307e41f4b71Sopenharmony_ci
1308e41f4b71Sopenharmony_ciSets the loopback mode flag for multicast communication. This API uses an asynchronous callback to return the result.
1309e41f4b71Sopenharmony_ci
1310e41f4b71Sopenharmony_ci> **NOTE**
1311e41f4b71Sopenharmony_ci> Use this API to enable or disable the loopback mode. By default, the loopback mode is enabled.
1312e41f4b71Sopenharmony_ci> The value **true** indicates that the host is allowed to receive the multicast packets sent by itself, and the value **false** indicates the opposite.
1313e41f4b71Sopenharmony_ci> This API is effective only after [addMembership](#addmembership11) is called.
1314e41f4b71Sopenharmony_ci
1315e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
1316e41f4b71Sopenharmony_ci
1317e41f4b71Sopenharmony_ci**Parameters**
1318e41f4b71Sopenharmony_ci
1319e41f4b71Sopenharmony_ci| Name        | Type                  | Mandatory| Description                            |
1320e41f4b71Sopenharmony_ci| ------------- | ---------------------- | ---- | -------------------------------- |
1321e41f4b71Sopenharmony_ci| flag          | boolean                |  Yes | Loopback mode flag. The value is of the Boolean type.|
1322e41f4b71Sopenharmony_ci
1323e41f4b71Sopenharmony_ci**Return value**
1324e41f4b71Sopenharmony_ci
1325e41f4b71Sopenharmony_ci| Type           | Description                                            |
1326e41f4b71Sopenharmony_ci|  -------------- |  ---------------------------------------------- |
1327e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result.|
1328e41f4b71Sopenharmony_ci
1329e41f4b71Sopenharmony_ci**Error codes**
1330e41f4b71Sopenharmony_ci
1331e41f4b71Sopenharmony_ci| ID| Error Message               |
1332e41f4b71Sopenharmony_ci| ------- | ----------------------- |
1333e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
1334e41f4b71Sopenharmony_ci| 2301088 | Not a socket.           |
1335e41f4b71Sopenharmony_ci
1336e41f4b71Sopenharmony_ci**Example**
1337e41f4b71Sopenharmony_ci
1338e41f4b71Sopenharmony_ci```ts
1339e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
1340e41f4b71Sopenharmony_ci
1341e41f4b71Sopenharmony_cilet multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance();
1342e41f4b71Sopenharmony_cimulticast.setLoopbackMode(false).then(() => {
1343e41f4b71Sopenharmony_ci  console.log('set loopback mode success');
1344e41f4b71Sopenharmony_ci}).catch((err: Object) => {
1345e41f4b71Sopenharmony_ci  console.log('set loopback mode failed');
1346e41f4b71Sopenharmony_ci});
1347e41f4b71Sopenharmony_ci```
1348e41f4b71Sopenharmony_ci
1349e41f4b71Sopenharmony_ci### getLoopbackMode<sup>11+</sup>
1350e41f4b71Sopenharmony_ci
1351e41f4b71Sopenharmony_cigetLoopbackMode(callback: AsyncCallback\<boolean\>): void;
1352e41f4b71Sopenharmony_ci
1353e41f4b71Sopenharmony_ciObtains the loopback mode flag for multicast communication. This API uses a promise to return the result.
1354e41f4b71Sopenharmony_ci
1355e41f4b71Sopenharmony_ci> **NOTE**
1356e41f4b71Sopenharmony_ci> Use this API to check whether the loopback mode is enabled.
1357e41f4b71Sopenharmony_ci> The value **true** indicates that the loopback mode is enabled, and the value **false** indicates the opposite. When the loopback mode is disabled, the host is does not receive the multicast packets sent by itself.
1358e41f4b71Sopenharmony_ci> This API is effective only after [addMembership](#addmembership11) is called.
1359e41f4b71Sopenharmony_ci
1360e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
1361e41f4b71Sopenharmony_ci
1362e41f4b71Sopenharmony_ci**Parameters**
1363e41f4b71Sopenharmony_ci
1364e41f4b71Sopenharmony_ci| Name        | Type                    | Mandatory| Description                        |
1365e41f4b71Sopenharmony_ci| ------------- | ----------------------- | ---- | --------------------------- |
1366e41f4b71Sopenharmony_ci| callback      | AsyncCallback\<number\> |  Yes | Callback used to return the result. If the operation fails, an error message is returned. |
1367e41f4b71Sopenharmony_ci
1368e41f4b71Sopenharmony_ci**Error codes**
1369e41f4b71Sopenharmony_ci
1370e41f4b71Sopenharmony_ci| ID| Error Message               |
1371e41f4b71Sopenharmony_ci| ------- | ----------------------- |
1372e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
1373e41f4b71Sopenharmony_ci| 2301088 | Not a socket.           |
1374e41f4b71Sopenharmony_ci
1375e41f4b71Sopenharmony_ci**Example**
1376e41f4b71Sopenharmony_ci
1377e41f4b71Sopenharmony_ci```ts
1378e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
1379e41f4b71Sopenharmony_ci
1380e41f4b71Sopenharmony_cilet multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance();
1381e41f4b71Sopenharmony_cimulticast.getLoopbackMode((err: Object, value: Boolean) => {
1382e41f4b71Sopenharmony_ci  if (err) {
1383e41f4b71Sopenharmony_ci    console.log('get loopback mode fail, err: ' + JSON.stringify(err));
1384e41f4b71Sopenharmony_ci    return;
1385e41f4b71Sopenharmony_ci  }
1386e41f4b71Sopenharmony_ci  console.log('get loopback mode success, value: ' + JSON.stringify(value));
1387e41f4b71Sopenharmony_ci})
1388e41f4b71Sopenharmony_ci```
1389e41f4b71Sopenharmony_ci
1390e41f4b71Sopenharmony_ci### getLoopbackMode<sup>11+</sup>
1391e41f4b71Sopenharmony_ci
1392e41f4b71Sopenharmony_cigetLoopbackMode(): Promise\<boolean\>;
1393e41f4b71Sopenharmony_ci
1394e41f4b71Sopenharmony_ciObtains the loopback mode flag for multicast communication. This API uses a promise to return the result.
1395e41f4b71Sopenharmony_ci
1396e41f4b71Sopenharmony_ci> **NOTE**
1397e41f4b71Sopenharmony_ci> Use this API to check whether the loopback mode is enabled.
1398e41f4b71Sopenharmony_ci> The value **true** indicates that the loopback mode is enabled, and the value **false** indicates the opposite. When the loopback mode is disabled, the host is does not receive the multicast packets sent by itself.
1399e41f4b71Sopenharmony_ci> This API is effective only after [addMembership](#addmembership11) is called.
1400e41f4b71Sopenharmony_ci
1401e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
1402e41f4b71Sopenharmony_ci                                                      
1403e41f4b71Sopenharmony_ci**Return value**
1404e41f4b71Sopenharmony_ci
1405e41f4b71Sopenharmony_ci| Type               | Description                       |
1406e41f4b71Sopenharmony_ci| ----------------  | --------------------------- |
1407e41f4b71Sopenharmony_ci| Promise\<boolean\> | Promise used to return the result.|
1408e41f4b71Sopenharmony_ci
1409e41f4b71Sopenharmony_ci**Error codes**
1410e41f4b71Sopenharmony_ci
1411e41f4b71Sopenharmony_ci| ID| Error Message               |
1412e41f4b71Sopenharmony_ci| ------- | ----------------------- |
1413e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
1414e41f4b71Sopenharmony_ci| 2301088 | Not a socket.           |
1415e41f4b71Sopenharmony_ci
1416e41f4b71Sopenharmony_ci**Example**
1417e41f4b71Sopenharmony_ci
1418e41f4b71Sopenharmony_ci```ts
1419e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
1420e41f4b71Sopenharmony_ci
1421e41f4b71Sopenharmony_cilet multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance();
1422e41f4b71Sopenharmony_cimulticast.getLoopbackMode().then((value: Boolean) => {
1423e41f4b71Sopenharmony_ci  console.log('loopback mode: ', JSON.stringify(value));
1424e41f4b71Sopenharmony_ci}).catch((err: Object) => {
1425e41f4b71Sopenharmony_ci  console.log('get loopback mode failed');
1426e41f4b71Sopenharmony_ci});
1427e41f4b71Sopenharmony_ci```
1428e41f4b71Sopenharmony_ci
1429e41f4b71Sopenharmony_ci## socket.constructTCPSocketInstance<sup>7+</sup>
1430e41f4b71Sopenharmony_ci
1431e41f4b71Sopenharmony_ciconstructTCPSocketInstance(): TCPSocket
1432e41f4b71Sopenharmony_ci
1433e41f4b71Sopenharmony_ciCreates a **TCPSocket** object.
1434e41f4b71Sopenharmony_ci
1435e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
1436e41f4b71Sopenharmony_ci
1437e41f4b71Sopenharmony_ci**Return value**
1438e41f4b71Sopenharmony_ci
1439e41f4b71Sopenharmony_ci| Type                              | Description                   |
1440e41f4b71Sopenharmony_ci| --------------------------------- | ---------------------- |
1441e41f4b71Sopenharmony_ci| [TCPSocket](#tcpsocket) | **TCPSocket** object.|
1442e41f4b71Sopenharmony_ci
1443e41f4b71Sopenharmony_ci**Example**
1444e41f4b71Sopenharmony_ci
1445e41f4b71Sopenharmony_ci```ts
1446e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
1447e41f4b71Sopenharmony_cilet tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
1448e41f4b71Sopenharmony_ci```
1449e41f4b71Sopenharmony_ci
1450e41f4b71Sopenharmony_ci## TCPSocket
1451e41f4b71Sopenharmony_ci
1452e41f4b71Sopenharmony_ciDefines a TCP socket connection. Before calling TCPSocket APIs, you need to call [socket.constructTCPSocketInstance](#socketconstructtcpsocketinstance7) to create a **TCPSocket** object.
1453e41f4b71Sopenharmony_ci
1454e41f4b71Sopenharmony_ci### bind
1455e41f4b71Sopenharmony_ci
1456e41f4b71Sopenharmony_cibind(address: NetAddress, callback: AsyncCallback\<void\>): void
1457e41f4b71Sopenharmony_ci
1458e41f4b71Sopenharmony_ciBinds an IP address and a port number. The port number can be specified or randomly allocated by the system. This API uses an asynchronous callback to return the result.
1459e41f4b71Sopenharmony_ci
1460e41f4b71Sopenharmony_ci> **NOTE**
1461e41f4b71Sopenharmony_ci> If the bind operation fails due to a port conflict, the system will randomly allocate a port number.
1462e41f4b71Sopenharmony_ci> The TCP client can call **tcp.bind** to explicitly bind the IP address and port number, and then call **tcp.connect** to connect to the server. Alternatively, the TCP client can directly call **tcp.connect** to automatically bind the IP address and port number to connect to the server.
1463e41f4b71Sopenharmony_ci> If the IP address is **localhost** or **127.0.0.1**, only local loopback access is allowed; that is, the TCP client and the server are deployed on the same device.
1464e41f4b71Sopenharmony_ci
1465e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
1466e41f4b71Sopenharmony_ci
1467e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
1468e41f4b71Sopenharmony_ci
1469e41f4b71Sopenharmony_ci**Parameters**
1470e41f4b71Sopenharmony_ci
1471e41f4b71Sopenharmony_ci| Name  | Type                              | Mandatory| Description                                                  |
1472e41f4b71Sopenharmony_ci| -------- | ---------------------------------- | ---- | ------------------------------------------------------ |
1473e41f4b71Sopenharmony_ci| address  | [NetAddress](#netaddress) | Yes  | Destination address. For details, see [NetAddress](#netaddress).|
1474e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void\>              | Yes  | Callback used to return the result. If the operation fails, an error message is returned.                  |
1475e41f4b71Sopenharmony_ci
1476e41f4b71Sopenharmony_ci**Error codes**
1477e41f4b71Sopenharmony_ci
1478e41f4b71Sopenharmony_ci| ID| Error Message                |
1479e41f4b71Sopenharmony_ci| ------- | ----------------------- |
1480e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
1481e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
1482e41f4b71Sopenharmony_ci
1483e41f4b71Sopenharmony_ci**Example**
1484e41f4b71Sopenharmony_ci
1485e41f4b71Sopenharmony_ci```ts
1486e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
1487e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
1488e41f4b71Sopenharmony_ci
1489e41f4b71Sopenharmony_cilet tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
1490e41f4b71Sopenharmony_cilet bindAddr: socket.NetAddress = {
1491e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
1492e41f4b71Sopenharmony_ci  port: 8080
1493e41f4b71Sopenharmony_ci}
1494e41f4b71Sopenharmony_citcp.bind(bindAddr, (err: BusinessError) => {
1495e41f4b71Sopenharmony_ci  if (err) {
1496e41f4b71Sopenharmony_ci    console.log('bind fail');
1497e41f4b71Sopenharmony_ci    return;
1498e41f4b71Sopenharmony_ci  }
1499e41f4b71Sopenharmony_ci  console.log('bind success');
1500e41f4b71Sopenharmony_ci})
1501e41f4b71Sopenharmony_ci```
1502e41f4b71Sopenharmony_ci
1503e41f4b71Sopenharmony_ci### bind
1504e41f4b71Sopenharmony_ci
1505e41f4b71Sopenharmony_cibind(address: NetAddress): Promise\<void\>
1506e41f4b71Sopenharmony_ci
1507e41f4b71Sopenharmony_ciBinds an IP address and a port number. The port number can be specified or randomly allocated by the system. This API uses a promise to return the result.
1508e41f4b71Sopenharmony_ci
1509e41f4b71Sopenharmony_ci> **NOTE**
1510e41f4b71Sopenharmony_ci> If the bind operation fails due to a port conflict, the system will randomly allocate a port number.
1511e41f4b71Sopenharmony_ci> The TCP client can call **tcp.bind** to explicitly bind the IP address and port number, and then call **tcp.connect** to connect to the server. Alternatively, the TCP client can directly call **tcp.connect** to automatically bind the IP address and port number to connect to the server.
1512e41f4b71Sopenharmony_ci> If the IP address is **localhost** or **127.0.0.1**, only local loopback access is allowed; that is, the TCP client and the server are deployed on the same device.
1513e41f4b71Sopenharmony_ci
1514e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
1515e41f4b71Sopenharmony_ci
1516e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
1517e41f4b71Sopenharmony_ci
1518e41f4b71Sopenharmony_ci**Parameters**
1519e41f4b71Sopenharmony_ci
1520e41f4b71Sopenharmony_ci| Name | Type                              | Mandatory| Description                                                  |
1521e41f4b71Sopenharmony_ci| ------- | ---------------------------------- | ---- | ------------------------------------------------------ |
1522e41f4b71Sopenharmony_ci| address | [NetAddress](#netaddress) | Yes  | Destination address. For details, see [NetAddress](#netaddress).|
1523e41f4b71Sopenharmony_ci
1524e41f4b71Sopenharmony_ci**Return value**
1525e41f4b71Sopenharmony_ci
1526e41f4b71Sopenharmony_ci| Type           | Description                                                    |
1527e41f4b71Sopenharmony_ci| --------------- | ------------------------------------------------------- |
1528e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result.|
1529e41f4b71Sopenharmony_ci
1530e41f4b71Sopenharmony_ci**Error codes**
1531e41f4b71Sopenharmony_ci
1532e41f4b71Sopenharmony_ci| ID| Error Message                |
1533e41f4b71Sopenharmony_ci| ------- | ----------------------- |
1534e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
1535e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
1536e41f4b71Sopenharmony_ci
1537e41f4b71Sopenharmony_ci**Example**
1538e41f4b71Sopenharmony_ci
1539e41f4b71Sopenharmony_ci```ts
1540e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
1541e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
1542e41f4b71Sopenharmony_ci
1543e41f4b71Sopenharmony_cilet tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
1544e41f4b71Sopenharmony_cilet bindAddr: socket.NetAddress = {
1545e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
1546e41f4b71Sopenharmony_ci  port: 8080
1547e41f4b71Sopenharmony_ci}
1548e41f4b71Sopenharmony_citcp.bind(bindAddr).then(() => {
1549e41f4b71Sopenharmony_ci  console.log('bind success');
1550e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
1551e41f4b71Sopenharmony_ci  console.log('bind fail');
1552e41f4b71Sopenharmony_ci});
1553e41f4b71Sopenharmony_ci```
1554e41f4b71Sopenharmony_ci
1555e41f4b71Sopenharmony_ci### connect
1556e41f4b71Sopenharmony_ci
1557e41f4b71Sopenharmony_ciconnect(options: TCPConnectOptions, callback: AsyncCallback\<void\>): void
1558e41f4b71Sopenharmony_ci
1559e41f4b71Sopenharmony_ciSets up a connection to the specified IP address and port number. This API uses an asynchronous callback to return the result.
1560e41f4b71Sopenharmony_ci
1561e41f4b71Sopenharmony_ci> **NOTE**
1562e41f4b71Sopenharmony_ci> This API allows you to connect to the TCP server without first executing **tcp.bind**.
1563e41f4b71Sopenharmony_ci
1564e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
1565e41f4b71Sopenharmony_ci
1566e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
1567e41f4b71Sopenharmony_ci
1568e41f4b71Sopenharmony_ci**Parameters**
1569e41f4b71Sopenharmony_ci
1570e41f4b71Sopenharmony_ci| Name  | Type                                    | Mandatory| Description                                                        |
1571e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ------------------------------------------------------------ |
1572e41f4b71Sopenharmony_ci| options  | [TCPConnectOptions](#tcpconnectoptions) | Yes  | TCP socket connection parameters. For details, see [TCPConnectOptions](#tcpconnectoptions).|
1573e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void\>                    | Yes  | Callback used to return the result. If the operation fails, an error message is returned.                     |
1574e41f4b71Sopenharmony_ci
1575e41f4b71Sopenharmony_ci**Error codes**
1576e41f4b71Sopenharmony_ci
1577e41f4b71Sopenharmony_ci| ID| Error Message                |
1578e41f4b71Sopenharmony_ci| ------- | ----------------------- |
1579e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
1580e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
1581e41f4b71Sopenharmony_ci
1582e41f4b71Sopenharmony_ci**Example**
1583e41f4b71Sopenharmony_ci
1584e41f4b71Sopenharmony_ci```ts
1585e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
1586e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
1587e41f4b71Sopenharmony_ci
1588e41f4b71Sopenharmony_cilet tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
1589e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
1590e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
1591e41f4b71Sopenharmony_ci  port: 8080
1592e41f4b71Sopenharmony_ci}
1593e41f4b71Sopenharmony_cilet tcpconnectoptions: socket.TCPConnectOptions = {
1594e41f4b71Sopenharmony_ci  address: netAddress,
1595e41f4b71Sopenharmony_ci  timeout: 6000
1596e41f4b71Sopenharmony_ci}
1597e41f4b71Sopenharmony_citcp.connect(tcpconnectoptions, (err: BusinessError) => {
1598e41f4b71Sopenharmony_ci  if (err) {
1599e41f4b71Sopenharmony_ci    console.log('connect fail');
1600e41f4b71Sopenharmony_ci    return;
1601e41f4b71Sopenharmony_ci  }
1602e41f4b71Sopenharmony_ci  console.log('connect success');
1603e41f4b71Sopenharmony_ci})
1604e41f4b71Sopenharmony_ci```
1605e41f4b71Sopenharmony_ci
1606e41f4b71Sopenharmony_ci### connect
1607e41f4b71Sopenharmony_ci
1608e41f4b71Sopenharmony_ciconnect(options: TCPConnectOptions): Promise\<void\>
1609e41f4b71Sopenharmony_ci
1610e41f4b71Sopenharmony_ciSets up a connection to the specified IP address and port number. This API uses a promise to return the result.
1611e41f4b71Sopenharmony_ci
1612e41f4b71Sopenharmony_ci> **NOTE**
1613e41f4b71Sopenharmony_ci> This API allows you to connect to the TCP server without first executing **tcp.bind**.
1614e41f4b71Sopenharmony_ci
1615e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
1616e41f4b71Sopenharmony_ci
1617e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
1618e41f4b71Sopenharmony_ci
1619e41f4b71Sopenharmony_ci**Parameters**
1620e41f4b71Sopenharmony_ci
1621e41f4b71Sopenharmony_ci| Name | Type                                    | Mandatory| Description                                                        |
1622e41f4b71Sopenharmony_ci| ------- | ---------------------------------------- | ---- | ------------------------------------------------------------ |
1623e41f4b71Sopenharmony_ci| options | [TCPConnectOptions](#tcpconnectoptions) | Yes  | TCP socket connection parameters. For details, see [TCPConnectOptions](#tcpconnectoptions).|
1624e41f4b71Sopenharmony_ci
1625e41f4b71Sopenharmony_ci**Return value**
1626e41f4b71Sopenharmony_ci
1627e41f4b71Sopenharmony_ci| Type           | Description                                                      |
1628e41f4b71Sopenharmony_ci| -------------- | --------------------------------------------------------- |
1629e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result.|
1630e41f4b71Sopenharmony_ci
1631e41f4b71Sopenharmony_ci**Error codes**
1632e41f4b71Sopenharmony_ci
1633e41f4b71Sopenharmony_ci| ID| Error Message                |
1634e41f4b71Sopenharmony_ci| ------- | ----------------------- |
1635e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
1636e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
1637e41f4b71Sopenharmony_ci
1638e41f4b71Sopenharmony_ci**Example**
1639e41f4b71Sopenharmony_ci
1640e41f4b71Sopenharmony_ci```ts
1641e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
1642e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
1643e41f4b71Sopenharmony_ci
1644e41f4b71Sopenharmony_cilet tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
1645e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
1646e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
1647e41f4b71Sopenharmony_ci  port: 8080
1648e41f4b71Sopenharmony_ci}
1649e41f4b71Sopenharmony_cilet tcpconnectoptions: socket.TCPConnectOptions = {
1650e41f4b71Sopenharmony_ci  address: netAddress,
1651e41f4b71Sopenharmony_ci  timeout: 6000
1652e41f4b71Sopenharmony_ci}
1653e41f4b71Sopenharmony_citcp.connect(tcpconnectoptions).then(() => {
1654e41f4b71Sopenharmony_ci  console.log('connect success')
1655e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
1656e41f4b71Sopenharmony_ci  console.log('connect fail');
1657e41f4b71Sopenharmony_ci});
1658e41f4b71Sopenharmony_ci```
1659e41f4b71Sopenharmony_ci
1660e41f4b71Sopenharmony_ci### send
1661e41f4b71Sopenharmony_ci
1662e41f4b71Sopenharmony_cisend(options: TCPSendOptions, callback: AsyncCallback\<void\>): void
1663e41f4b71Sopenharmony_ci
1664e41f4b71Sopenharmony_ciSends data over a TCP socket connection. This API uses an asynchronous callback to return the result.
1665e41f4b71Sopenharmony_ci
1666e41f4b71Sopenharmony_ci> **NOTE**
1667e41f4b71Sopenharmony_ci> This API can be called only after **connect** is successfully called. Call the API in the worker thread or taskpool thread as this operation is time-consuming.
1668e41f4b71Sopenharmony_ci
1669e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
1670e41f4b71Sopenharmony_ci
1671e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
1672e41f4b71Sopenharmony_ci
1673e41f4b71Sopenharmony_ci**Parameters**
1674e41f4b71Sopenharmony_ci
1675e41f4b71Sopenharmony_ci| Name  | Type                                   | Mandatory| Description                                                        |
1676e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
1677e41f4b71Sopenharmony_ci| options  | [TCPSendOptions](#tcpsendoptions) | Yes  | Parameters for sending data over a TCP socket connection. For details, see [TCPSendOptions](#tcpsendoptions).|
1678e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void\>                   | Yes  | Callback used to return the result. If the operation fails, an error message is returned.                          |
1679e41f4b71Sopenharmony_ci
1680e41f4b71Sopenharmony_ci**Error codes**
1681e41f4b71Sopenharmony_ci
1682e41f4b71Sopenharmony_ci| ID| Error Message                |
1683e41f4b71Sopenharmony_ci| ------- | ----------------------- |
1684e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
1685e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
1686e41f4b71Sopenharmony_ci
1687e41f4b71Sopenharmony_ci**Example**
1688e41f4b71Sopenharmony_ci
1689e41f4b71Sopenharmony_ci```ts
1690e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
1691e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
1692e41f4b71Sopenharmony_ci
1693e41f4b71Sopenharmony_cilet tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
1694e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
1695e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
1696e41f4b71Sopenharmony_ci  port: 8080
1697e41f4b71Sopenharmony_ci}
1698e41f4b71Sopenharmony_cilet tcpconnectoptions: socket.TCPConnectOptions = {
1699e41f4b71Sopenharmony_ci  address: netAddress,
1700e41f4b71Sopenharmony_ci  timeout: 6000
1701e41f4b71Sopenharmony_ci}
1702e41f4b71Sopenharmony_citcp.connect(tcpconnectoptions, () => {
1703e41f4b71Sopenharmony_ci  console.log('connect success');
1704e41f4b71Sopenharmony_ci  let tcpSendOptions: socket.TCPSendOptions = {
1705e41f4b71Sopenharmony_ci    data: 'Hello, server!'
1706e41f4b71Sopenharmony_ci  }
1707e41f4b71Sopenharmony_ci  tcp.send(tcpSendOptions, (err: BusinessError) => {
1708e41f4b71Sopenharmony_ci    if (err) {
1709e41f4b71Sopenharmony_ci      console.log('send fail');
1710e41f4b71Sopenharmony_ci      return;
1711e41f4b71Sopenharmony_ci    }
1712e41f4b71Sopenharmony_ci    console.log('send success');
1713e41f4b71Sopenharmony_ci  })
1714e41f4b71Sopenharmony_ci})
1715e41f4b71Sopenharmony_ci```
1716e41f4b71Sopenharmony_ci
1717e41f4b71Sopenharmony_ci### send
1718e41f4b71Sopenharmony_ci
1719e41f4b71Sopenharmony_cisend(options: TCPSendOptions): Promise\<void\>
1720e41f4b71Sopenharmony_ci
1721e41f4b71Sopenharmony_ciSends data over a TCP socket connection. This API uses a promise to return the result.
1722e41f4b71Sopenharmony_ci
1723e41f4b71Sopenharmony_ci> **NOTE**
1724e41f4b71Sopenharmony_ci> This API can be called only after **connect** is successfully called. Call the API in the worker thread or taskpool thread as this operation is time-consuming.
1725e41f4b71Sopenharmony_ci
1726e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
1727e41f4b71Sopenharmony_ci
1728e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
1729e41f4b71Sopenharmony_ci
1730e41f4b71Sopenharmony_ci**Parameters**
1731e41f4b71Sopenharmony_ci
1732e41f4b71Sopenharmony_ci| Name | Type                                   | Mandatory| Description                                                        |
1733e41f4b71Sopenharmony_ci| ------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
1734e41f4b71Sopenharmony_ci| options | [TCPSendOptions](#tcpsendoptions) | Yes  | Parameters for sending data over a TCP socket connection. For details, see [TCPSendOptions](#tcpsendoptions).|
1735e41f4b71Sopenharmony_ci
1736e41f4b71Sopenharmony_ci**Return value**
1737e41f4b71Sopenharmony_ci
1738e41f4b71Sopenharmony_ci| Type           | Description                                              |
1739e41f4b71Sopenharmony_ci| -------------- | ------------------------------------------------- |
1740e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result.|
1741e41f4b71Sopenharmony_ci
1742e41f4b71Sopenharmony_ci**Error codes**
1743e41f4b71Sopenharmony_ci
1744e41f4b71Sopenharmony_ci| ID| Error Message                |
1745e41f4b71Sopenharmony_ci| ------- | ----------------------- |
1746e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
1747e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
1748e41f4b71Sopenharmony_ci
1749e41f4b71Sopenharmony_ci**Example**
1750e41f4b71Sopenharmony_ci
1751e41f4b71Sopenharmony_ci```ts
1752e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
1753e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
1754e41f4b71Sopenharmony_ci
1755e41f4b71Sopenharmony_cilet tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
1756e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
1757e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
1758e41f4b71Sopenharmony_ci  port: 8080
1759e41f4b71Sopenharmony_ci}
1760e41f4b71Sopenharmony_cilet tcpconnectoptions: socket.TCPConnectOptions = {
1761e41f4b71Sopenharmony_ci  address: netAddress,
1762e41f4b71Sopenharmony_ci  timeout: 6000
1763e41f4b71Sopenharmony_ci}
1764e41f4b71Sopenharmony_citcp.connect(tcpconnectoptions, () => {
1765e41f4b71Sopenharmony_ci  console.log('connect success');
1766e41f4b71Sopenharmony_ci  let tcpSendOptions: socket.TCPSendOptions = {
1767e41f4b71Sopenharmony_ci    data: 'Hello, server!'
1768e41f4b71Sopenharmony_ci  }
1769e41f4b71Sopenharmony_ci  tcp.send(tcpSendOptions).then(() => {
1770e41f4b71Sopenharmony_ci    console.log('send success');
1771e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
1772e41f4b71Sopenharmony_ci    console.log('send fail');
1773e41f4b71Sopenharmony_ci  });
1774e41f4b71Sopenharmony_ci})
1775e41f4b71Sopenharmony_ci```
1776e41f4b71Sopenharmony_ci
1777e41f4b71Sopenharmony_ci### close
1778e41f4b71Sopenharmony_ci
1779e41f4b71Sopenharmony_ciclose(callback: AsyncCallback\<void\>): void
1780e41f4b71Sopenharmony_ci
1781e41f4b71Sopenharmony_ciCloses a TCP socket connection. This API uses an asynchronous callback to return the result.
1782e41f4b71Sopenharmony_ci
1783e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
1784e41f4b71Sopenharmony_ci
1785e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
1786e41f4b71Sopenharmony_ci
1787e41f4b71Sopenharmony_ci**Parameters**
1788e41f4b71Sopenharmony_ci
1789e41f4b71Sopenharmony_ci| Name  | Type                 | Mandatory| Description      |
1790e41f4b71Sopenharmony_ci| -------- | --------------------- | ---- | ---------- |
1791e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void\> | Yes  | Callback used to return the result. If the operation fails, an error message is returned.|
1792e41f4b71Sopenharmony_ci
1793e41f4b71Sopenharmony_ci**Error codes**
1794e41f4b71Sopenharmony_ci
1795e41f4b71Sopenharmony_ci| ID| Error Message                |
1796e41f4b71Sopenharmony_ci| ------- | ----------------------- |
1797e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
1798e41f4b71Sopenharmony_ci
1799e41f4b71Sopenharmony_ci**Example**
1800e41f4b71Sopenharmony_ci
1801e41f4b71Sopenharmony_ci```ts
1802e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
1803e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
1804e41f4b71Sopenharmony_ci
1805e41f4b71Sopenharmony_cilet tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
1806e41f4b71Sopenharmony_ci
1807e41f4b71Sopenharmony_citcp.close((err: BusinessError) => {
1808e41f4b71Sopenharmony_ci  if (err) {
1809e41f4b71Sopenharmony_ci    console.log('close fail');
1810e41f4b71Sopenharmony_ci    return;
1811e41f4b71Sopenharmony_ci  }
1812e41f4b71Sopenharmony_ci  console.log('close success');
1813e41f4b71Sopenharmony_ci})
1814e41f4b71Sopenharmony_ci```
1815e41f4b71Sopenharmony_ci
1816e41f4b71Sopenharmony_ci### close
1817e41f4b71Sopenharmony_ci
1818e41f4b71Sopenharmony_ciclose(): Promise\<void\>
1819e41f4b71Sopenharmony_ci
1820e41f4b71Sopenharmony_ciCloses a TCP socket connection. This API uses a promise to return the result.
1821e41f4b71Sopenharmony_ci
1822e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
1823e41f4b71Sopenharmony_ci
1824e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
1825e41f4b71Sopenharmony_ci
1826e41f4b71Sopenharmony_ci**Return value**
1827e41f4b71Sopenharmony_ci
1828e41f4b71Sopenharmony_ci| Type           | Description                                      |
1829e41f4b71Sopenharmony_ci| -------------- | ----------------------------------------- |
1830e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result.|
1831e41f4b71Sopenharmony_ci
1832e41f4b71Sopenharmony_ci**Error codes**
1833e41f4b71Sopenharmony_ci
1834e41f4b71Sopenharmony_ci| ID| Error Message                |
1835e41f4b71Sopenharmony_ci| ------- | ----------------------- |
1836e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
1837e41f4b71Sopenharmony_ci
1838e41f4b71Sopenharmony_ci**Example**
1839e41f4b71Sopenharmony_ci
1840e41f4b71Sopenharmony_ci```ts
1841e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
1842e41f4b71Sopenharmony_ci
1843e41f4b71Sopenharmony_cilet tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
1844e41f4b71Sopenharmony_ci
1845e41f4b71Sopenharmony_citcp.close().then(() => {
1846e41f4b71Sopenharmony_ci  console.log('close success');
1847e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
1848e41f4b71Sopenharmony_ci  console.log('close fail');
1849e41f4b71Sopenharmony_ci});
1850e41f4b71Sopenharmony_ci```
1851e41f4b71Sopenharmony_ci
1852e41f4b71Sopenharmony_ci### getRemoteAddress
1853e41f4b71Sopenharmony_ci
1854e41f4b71Sopenharmony_cigetRemoteAddress(callback: AsyncCallback\<NetAddress\>): void
1855e41f4b71Sopenharmony_ci
1856e41f4b71Sopenharmony_ciObtains the remote address of a socket connection. This API uses an asynchronous callback to return the result.
1857e41f4b71Sopenharmony_ci
1858e41f4b71Sopenharmony_ci> **NOTE**
1859e41f4b71Sopenharmony_ci> This API can be called only after **connect** is successfully called.
1860e41f4b71Sopenharmony_ci
1861e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
1862e41f4b71Sopenharmony_ci
1863e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
1864e41f4b71Sopenharmony_ci
1865e41f4b71Sopenharmony_ci**Parameters**
1866e41f4b71Sopenharmony_ci
1867e41f4b71Sopenharmony_ci| Name  | Type                                             | Mandatory| Description      |
1868e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | ---- | ---------- |
1869e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[NetAddress](#netaddress)\> | Yes  | Callback used to return the result. If the operation is successful, the remote address is returned. If the operation fails, an error message is returned.|
1870e41f4b71Sopenharmony_ci
1871e41f4b71Sopenharmony_ci**Error codes**
1872e41f4b71Sopenharmony_ci
1873e41f4b71Sopenharmony_ci| ID| Error Message                |
1874e41f4b71Sopenharmony_ci| ------- | ----------------------- |
1875e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
1876e41f4b71Sopenharmony_ci
1877e41f4b71Sopenharmony_ci**Example**
1878e41f4b71Sopenharmony_ci
1879e41f4b71Sopenharmony_ci```ts
1880e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
1881e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
1882e41f4b71Sopenharmony_ci
1883e41f4b71Sopenharmony_cilet tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
1884e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
1885e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
1886e41f4b71Sopenharmony_ci  port: 8080
1887e41f4b71Sopenharmony_ci}
1888e41f4b71Sopenharmony_cilet tcpconnectoptions: socket.TCPConnectOptions = {
1889e41f4b71Sopenharmony_ci  address: netAddress,
1890e41f4b71Sopenharmony_ci  timeout: 6000
1891e41f4b71Sopenharmony_ci}
1892e41f4b71Sopenharmony_citcp.connect(tcpconnectoptions, () => {
1893e41f4b71Sopenharmony_ci  console.log('connect success');
1894e41f4b71Sopenharmony_ci  tcp.getRemoteAddress((err: BusinessError, data: socket.NetAddress) => {
1895e41f4b71Sopenharmony_ci    if (err) {
1896e41f4b71Sopenharmony_ci      console.log('getRemoteAddressfail');
1897e41f4b71Sopenharmony_ci      return;
1898e41f4b71Sopenharmony_ci    }
1899e41f4b71Sopenharmony_ci    console.log('getRemoteAddresssuccess:' + JSON.stringify(data));
1900e41f4b71Sopenharmony_ci  })
1901e41f4b71Sopenharmony_ci});
1902e41f4b71Sopenharmony_ci```
1903e41f4b71Sopenharmony_ci
1904e41f4b71Sopenharmony_ci### getRemoteAddress
1905e41f4b71Sopenharmony_ci
1906e41f4b71Sopenharmony_cigetRemoteAddress(): Promise\<NetAddress\>
1907e41f4b71Sopenharmony_ci
1908e41f4b71Sopenharmony_ciObtains the remote address of a socket connection. This API uses a promise to return the result.
1909e41f4b71Sopenharmony_ci
1910e41f4b71Sopenharmony_ci> **NOTE**
1911e41f4b71Sopenharmony_ci> This API can be called only after **connect** is successfully called.
1912e41f4b71Sopenharmony_ci
1913e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
1914e41f4b71Sopenharmony_ci
1915e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
1916e41f4b71Sopenharmony_ci
1917e41f4b71Sopenharmony_ci**Return value**
1918e41f4b71Sopenharmony_ci
1919e41f4b71Sopenharmony_ci| Type                                       | Description                                       |
1920e41f4b71Sopenharmony_ci| ------------------------------------------ | ------------------------------------------ |
1921e41f4b71Sopenharmony_ci| Promise\<[NetAddress](#netaddress)\> | Promise used to return the result.|
1922e41f4b71Sopenharmony_ci
1923e41f4b71Sopenharmony_ci**Error codes**
1924e41f4b71Sopenharmony_ci
1925e41f4b71Sopenharmony_ci| ID| Error Message                |
1926e41f4b71Sopenharmony_ci| ------- | ----------------------- |
1927e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
1928e41f4b71Sopenharmony_ci
1929e41f4b71Sopenharmony_ci**Example**
1930e41f4b71Sopenharmony_ci
1931e41f4b71Sopenharmony_ci```ts
1932e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
1933e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
1934e41f4b71Sopenharmony_ci
1935e41f4b71Sopenharmony_cilet tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
1936e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
1937e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
1938e41f4b71Sopenharmony_ci  port: 8080
1939e41f4b71Sopenharmony_ci}
1940e41f4b71Sopenharmony_cilet tcpconnectoptions: socket.TCPConnectOptions = {
1941e41f4b71Sopenharmony_ci  address: netAddress,
1942e41f4b71Sopenharmony_ci  timeout: 6000
1943e41f4b71Sopenharmony_ci}
1944e41f4b71Sopenharmony_citcp.connect(tcpconnectoptions).then(() => {
1945e41f4b71Sopenharmony_ci  console.log('connect success');
1946e41f4b71Sopenharmony_ci  tcp.getRemoteAddress().then(() => {
1947e41f4b71Sopenharmony_ci    console.log('getRemoteAddress success');
1948e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
1949e41f4b71Sopenharmony_ci    console.log('getRemoteAddressfail');
1950e41f4b71Sopenharmony_ci  });
1951e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
1952e41f4b71Sopenharmony_ci  console.log('connect fail');
1953e41f4b71Sopenharmony_ci});
1954e41f4b71Sopenharmony_ci```
1955e41f4b71Sopenharmony_ci
1956e41f4b71Sopenharmony_ci### getState
1957e41f4b71Sopenharmony_ci
1958e41f4b71Sopenharmony_cigetState(callback: AsyncCallback\<SocketStateBase\>): void
1959e41f4b71Sopenharmony_ci
1960e41f4b71Sopenharmony_ciObtains the status of the TCP socket connection. This API uses an asynchronous callback to return the result.
1961e41f4b71Sopenharmony_ci
1962e41f4b71Sopenharmony_ci> **NOTE**
1963e41f4b71Sopenharmony_ci> This API can be called only after **bind** or **connect** is successfully called.
1964e41f4b71Sopenharmony_ci
1965e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
1966e41f4b71Sopenharmony_ci
1967e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
1968e41f4b71Sopenharmony_ci
1969e41f4b71Sopenharmony_ci**Parameters**
1970e41f4b71Sopenharmony_ci
1971e41f4b71Sopenharmony_ci| Name  | Type                                                  | Mandatory| Description      |
1972e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------ | ---- | ---------- |
1973e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[SocketStateBase](#socketstatebase)\> | Yes  | Callback used to return the result. If the operation is successful, the status of the TCP socket is returned. If the operation fails, an error message is returned.|
1974e41f4b71Sopenharmony_ci
1975e41f4b71Sopenharmony_ci**Error codes**
1976e41f4b71Sopenharmony_ci
1977e41f4b71Sopenharmony_ci| ID| Error Message                |
1978e41f4b71Sopenharmony_ci| ------- | ----------------------- |
1979e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
1980e41f4b71Sopenharmony_ci
1981e41f4b71Sopenharmony_ci**Example**
1982e41f4b71Sopenharmony_ci
1983e41f4b71Sopenharmony_ci```ts
1984e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
1985e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
1986e41f4b71Sopenharmony_ci
1987e41f4b71Sopenharmony_cilet tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
1988e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
1989e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
1990e41f4b71Sopenharmony_ci  port: 8080
1991e41f4b71Sopenharmony_ci}
1992e41f4b71Sopenharmony_cilet tcpconnectoptions: socket.TCPConnectOptions = {
1993e41f4b71Sopenharmony_ci  address: netAddress,
1994e41f4b71Sopenharmony_ci  timeout: 6000
1995e41f4b71Sopenharmony_ci}
1996e41f4b71Sopenharmony_citcp.connect(tcpconnectoptions, () => {
1997e41f4b71Sopenharmony_ci  console.log('connect success');
1998e41f4b71Sopenharmony_ci  tcp.getState((err: BusinessError, data: socket.SocketStateBase) => {
1999e41f4b71Sopenharmony_ci    if (err) {
2000e41f4b71Sopenharmony_ci      console.log('getState fail');
2001e41f4b71Sopenharmony_ci      return;
2002e41f4b71Sopenharmony_ci    }
2003e41f4b71Sopenharmony_ci    console.log('getState success:' + JSON.stringify(data));
2004e41f4b71Sopenharmony_ci  });
2005e41f4b71Sopenharmony_ci});
2006e41f4b71Sopenharmony_ci```
2007e41f4b71Sopenharmony_ci
2008e41f4b71Sopenharmony_ci### getState
2009e41f4b71Sopenharmony_ci
2010e41f4b71Sopenharmony_cigetState(): Promise\<SocketStateBase\>
2011e41f4b71Sopenharmony_ci
2012e41f4b71Sopenharmony_ciObtains the status of the TCP socket connection. This API uses a promise to return the result.
2013e41f4b71Sopenharmony_ci
2014e41f4b71Sopenharmony_ci> **NOTE**
2015e41f4b71Sopenharmony_ci> This API can be called only after **bind** or **connect** is successfully called.
2016e41f4b71Sopenharmony_ci
2017e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
2018e41f4b71Sopenharmony_ci
2019e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
2020e41f4b71Sopenharmony_ci
2021e41f4b71Sopenharmony_ci**Return value**
2022e41f4b71Sopenharmony_ci
2023e41f4b71Sopenharmony_ci| Type                                            | Description                                      |
2024e41f4b71Sopenharmony_ci| ----------------------------------------------- | ----------------------------------------- |
2025e41f4b71Sopenharmony_ci| Promise\<[SocketStateBase](#socketstatebase)\> | Promise used to return the result.|
2026e41f4b71Sopenharmony_ci
2027e41f4b71Sopenharmony_ci**Error codes**
2028e41f4b71Sopenharmony_ci
2029e41f4b71Sopenharmony_ci| ID| Error Message                |
2030e41f4b71Sopenharmony_ci| ------- | ----------------------- |
2031e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
2032e41f4b71Sopenharmony_ci
2033e41f4b71Sopenharmony_ci**Example**
2034e41f4b71Sopenharmony_ci
2035e41f4b71Sopenharmony_ci```ts
2036e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
2037e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
2038e41f4b71Sopenharmony_ci
2039e41f4b71Sopenharmony_cilet tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
2040e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
2041e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
2042e41f4b71Sopenharmony_ci  port: 8080
2043e41f4b71Sopenharmony_ci}
2044e41f4b71Sopenharmony_cilet tcpconnectoptions: socket.TCPConnectOptions = {
2045e41f4b71Sopenharmony_ci  address: netAddress,
2046e41f4b71Sopenharmony_ci  timeout: 6000
2047e41f4b71Sopenharmony_ci}
2048e41f4b71Sopenharmony_citcp.connect(tcpconnectoptions).then(() => {
2049e41f4b71Sopenharmony_ci  console.log('connect success');
2050e41f4b71Sopenharmony_ci  tcp.getState().then(() => {
2051e41f4b71Sopenharmony_ci    console.log('getState success');
2052e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
2053e41f4b71Sopenharmony_ci    console.log('getState fail');
2054e41f4b71Sopenharmony_ci  });
2055e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
2056e41f4b71Sopenharmony_ci  console.log('connect fail');
2057e41f4b71Sopenharmony_ci});
2058e41f4b71Sopenharmony_ci```
2059e41f4b71Sopenharmony_ci
2060e41f4b71Sopenharmony_ci### getSocketFd<sup>10+</sup>
2061e41f4b71Sopenharmony_ci
2062e41f4b71Sopenharmony_cigetSocketFd(callback: AsyncCallback\<number\>): void
2063e41f4b71Sopenharmony_ci
2064e41f4b71Sopenharmony_ciObtains the file descriptor of the **TCPSocket** object. This API uses an asynchronous callback to return the result.
2065e41f4b71Sopenharmony_ci
2066e41f4b71Sopenharmony_ci> **NOTE**
2067e41f4b71Sopenharmony_ci> This API can be called only after **bind** or **connect** is successfully called.
2068e41f4b71Sopenharmony_ci
2069e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
2070e41f4b71Sopenharmony_ci
2071e41f4b71Sopenharmony_ci**Parameters**
2072e41f4b71Sopenharmony_ci
2073e41f4b71Sopenharmony_ci| Name  | Type                                                  | Mandatory| Description      |
2074e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------ | ---- | ---------- |
2075e41f4b71Sopenharmony_ci| callback | AsyncCallback\<number\> | Yes  | Callback used to return the result. If the operation is successful, the file descriptor of the socket is returned. Otherwise, **undefined** is returned.|
2076e41f4b71Sopenharmony_ci
2077e41f4b71Sopenharmony_ci**Example**
2078e41f4b71Sopenharmony_ci
2079e41f4b71Sopenharmony_ci```ts
2080e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
2081e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
2082e41f4b71Sopenharmony_ci
2083e41f4b71Sopenharmony_cilet tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
2084e41f4b71Sopenharmony_cilet bindAddr: socket.NetAddress = {
2085e41f4b71Sopenharmony_ci  address: '0.0.0.0'
2086e41f4b71Sopenharmony_ci}
2087e41f4b71Sopenharmony_citcp.bind(bindAddr)
2088e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
2089e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
2090e41f4b71Sopenharmony_ci  port: 8080
2091e41f4b71Sopenharmony_ci}
2092e41f4b71Sopenharmony_cilet tcpconnectoptions: socket.TCPConnectOptions = {
2093e41f4b71Sopenharmony_ci  address: netAddress,
2094e41f4b71Sopenharmony_ci  timeout: 6000
2095e41f4b71Sopenharmony_ci}
2096e41f4b71Sopenharmony_citcp.connect(tcpconnectoptions)
2097e41f4b71Sopenharmony_citcp.getSocketFd((err: BusinessError, data: number) => {
2098e41f4b71Sopenharmony_ci  console.info("getSocketFd failed: " + err);
2099e41f4b71Sopenharmony_ci  console.info("tunenlfd: " + data);
2100e41f4b71Sopenharmony_ci})
2101e41f4b71Sopenharmony_ci```
2102e41f4b71Sopenharmony_ci### getSocketFd<sup>10+</sup>
2103e41f4b71Sopenharmony_ci
2104e41f4b71Sopenharmony_cigetSocketFd(): Promise\<number\>
2105e41f4b71Sopenharmony_ci
2106e41f4b71Sopenharmony_ciObtains the file descriptor of the **TCPSocket** object. This API uses a promise to return the result.
2107e41f4b71Sopenharmony_ci
2108e41f4b71Sopenharmony_ci> **NOTE**
2109e41f4b71Sopenharmony_ci> This API can be called only after **bind** or **connect** is successfully called.
2110e41f4b71Sopenharmony_ci
2111e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
2112e41f4b71Sopenharmony_ci
2113e41f4b71Sopenharmony_ci**Return value**
2114e41f4b71Sopenharmony_ci
2115e41f4b71Sopenharmony_ci| Type                                            | Description                                      |
2116e41f4b71Sopenharmony_ci| ----------------------------------------------- | ----------------------------------------- |
2117e41f4b71Sopenharmony_ci| Promise\<number\> | Promise used to return the result.|
2118e41f4b71Sopenharmony_ci
2119e41f4b71Sopenharmony_ci**Example**
2120e41f4b71Sopenharmony_ci
2121e41f4b71Sopenharmony_ci```ts
2122e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
2123e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
2124e41f4b71Sopenharmony_ci
2125e41f4b71Sopenharmony_cilet tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
2126e41f4b71Sopenharmony_cilet bindAddr: socket.NetAddress = {
2127e41f4b71Sopenharmony_ci  address: '0.0.0.0'
2128e41f4b71Sopenharmony_ci}
2129e41f4b71Sopenharmony_citcp.bind(bindAddr)
2130e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
2131e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
2132e41f4b71Sopenharmony_ci  port: 8080
2133e41f4b71Sopenharmony_ci}
2134e41f4b71Sopenharmony_cilet tcpconnectoptions: socket.TCPConnectOptions = {
2135e41f4b71Sopenharmony_ci  address: netAddress,
2136e41f4b71Sopenharmony_ci  timeout: 6000
2137e41f4b71Sopenharmony_ci}
2138e41f4b71Sopenharmony_citcp.connect(tcpconnectoptions)
2139e41f4b71Sopenharmony_citcp.getSocketFd().then((data: number) => {
2140e41f4b71Sopenharmony_ci  console.info("tunenlfd: " + data);
2141e41f4b71Sopenharmony_ci})
2142e41f4b71Sopenharmony_ci```
2143e41f4b71Sopenharmony_ci
2144e41f4b71Sopenharmony_ci### setExtraOptions
2145e41f4b71Sopenharmony_ci
2146e41f4b71Sopenharmony_cisetExtraOptions(options: TCPExtraOptions, callback: AsyncCallback\<void\>): void
2147e41f4b71Sopenharmony_ci
2148e41f4b71Sopenharmony_ciSets other properties of the TCP socket connection. This API uses an asynchronous callback to return the result.
2149e41f4b71Sopenharmony_ci
2150e41f4b71Sopenharmony_ci> **NOTE**
2151e41f4b71Sopenharmony_ci> This API can be called only after **bind** or **connect** is successfully called.
2152e41f4b71Sopenharmony_ci
2153e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
2154e41f4b71Sopenharmony_ci
2155e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
2156e41f4b71Sopenharmony_ci
2157e41f4b71Sopenharmony_ci**Parameters**
2158e41f4b71Sopenharmony_ci
2159e41f4b71Sopenharmony_ci| Name  | Type                                     | Mandatory| Description                                                        |
2160e41f4b71Sopenharmony_ci| -------- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
2161e41f4b71Sopenharmony_ci| options  | [TCPExtraOptions](#tcpextraoptions) | Yes  | Other properties of the TCP socket connection. For details, see [TCPExtraOptions](#tcpextraoptions).|
2162e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void\>                     | Yes  | Callback used to return the result. If the operation fails, an error message is returned.              |
2163e41f4b71Sopenharmony_ci
2164e41f4b71Sopenharmony_ci**Error codes**
2165e41f4b71Sopenharmony_ci
2166e41f4b71Sopenharmony_ci| ID| Error Message                |
2167e41f4b71Sopenharmony_ci| ------- | ----------------------- |
2168e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
2169e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
2170e41f4b71Sopenharmony_ci
2171e41f4b71Sopenharmony_ci**Example**
2172e41f4b71Sopenharmony_ci
2173e41f4b71Sopenharmony_ci```ts
2174e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
2175e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
2176e41f4b71Sopenharmony_ci
2177e41f4b71Sopenharmony_cilet tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
2178e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
2179e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
2180e41f4b71Sopenharmony_ci  port: 8080
2181e41f4b71Sopenharmony_ci}
2182e41f4b71Sopenharmony_cilet tcpconnectoptions: socket.TCPConnectOptions = {
2183e41f4b71Sopenharmony_ci  address: netAddress,
2184e41f4b71Sopenharmony_ci  timeout: 6000
2185e41f4b71Sopenharmony_ci}
2186e41f4b71Sopenharmony_ci
2187e41f4b71Sopenharmony_ciinterface SocketLinger {
2188e41f4b71Sopenharmony_ci  on: boolean;
2189e41f4b71Sopenharmony_ci  linger: number;
2190e41f4b71Sopenharmony_ci}
2191e41f4b71Sopenharmony_ci
2192e41f4b71Sopenharmony_citcp.connect(tcpconnectoptions, () => {
2193e41f4b71Sopenharmony_ci  console.log('connect success');
2194e41f4b71Sopenharmony_ci  let tcpExtraOptions: socket.TCPExtraOptions = {
2195e41f4b71Sopenharmony_ci    keepAlive: true,
2196e41f4b71Sopenharmony_ci    OOBInline: true,
2197e41f4b71Sopenharmony_ci    TCPNoDelay: true,
2198e41f4b71Sopenharmony_ci    socketLinger: { on: true, linger: 10 } as SocketLinger,
2199e41f4b71Sopenharmony_ci    receiveBufferSize: 1000,
2200e41f4b71Sopenharmony_ci    sendBufferSize: 1000,
2201e41f4b71Sopenharmony_ci    reuseAddress: true,
2202e41f4b71Sopenharmony_ci    socketTimeout: 3000
2203e41f4b71Sopenharmony_ci  }
2204e41f4b71Sopenharmony_ci  tcp.setExtraOptions(tcpExtraOptions, (err: BusinessError) => {
2205e41f4b71Sopenharmony_ci    if (err) {
2206e41f4b71Sopenharmony_ci      console.log('setExtraOptions fail');
2207e41f4b71Sopenharmony_ci      return;
2208e41f4b71Sopenharmony_ci    }
2209e41f4b71Sopenharmony_ci    console.log('setExtraOptions success');
2210e41f4b71Sopenharmony_ci  });
2211e41f4b71Sopenharmony_ci});
2212e41f4b71Sopenharmony_ci```
2213e41f4b71Sopenharmony_ci
2214e41f4b71Sopenharmony_ci### setExtraOptions
2215e41f4b71Sopenharmony_ci
2216e41f4b71Sopenharmony_cisetExtraOptions(options: TCPExtraOptions): Promise\<void\>
2217e41f4b71Sopenharmony_ci
2218e41f4b71Sopenharmony_ciSets other properties of the TCP socket connection. This API uses a promise to return the result.
2219e41f4b71Sopenharmony_ci
2220e41f4b71Sopenharmony_ci> **NOTE**
2221e41f4b71Sopenharmony_ci> This API can be called only after **bind** or **connect** is successfully called.
2222e41f4b71Sopenharmony_ci
2223e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
2224e41f4b71Sopenharmony_ci
2225e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
2226e41f4b71Sopenharmony_ci
2227e41f4b71Sopenharmony_ci**Parameters**
2228e41f4b71Sopenharmony_ci
2229e41f4b71Sopenharmony_ci| Name | Type                                     | Mandatory| Description                                                        |
2230e41f4b71Sopenharmony_ci| ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
2231e41f4b71Sopenharmony_ci| options | [TCPExtraOptions](#tcpextraoptions) | Yes  | Other properties of the TCP socket connection. For details, see [TCPExtraOptions](#tcpextraoptions).|
2232e41f4b71Sopenharmony_ci
2233e41f4b71Sopenharmony_ci**Return value**
2234e41f4b71Sopenharmony_ci
2235e41f4b71Sopenharmony_ci| Type           | Description                                                |
2236e41f4b71Sopenharmony_ci| -------------- | --------------------------------------------------- |
2237e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result.|
2238e41f4b71Sopenharmony_ci
2239e41f4b71Sopenharmony_ci**Error codes**
2240e41f4b71Sopenharmony_ci
2241e41f4b71Sopenharmony_ci| ID| Error Message                |
2242e41f4b71Sopenharmony_ci| ------- | ----------------------- |
2243e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
2244e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
2245e41f4b71Sopenharmony_ci
2246e41f4b71Sopenharmony_ci**Example**
2247e41f4b71Sopenharmony_ci
2248e41f4b71Sopenharmony_ci```ts
2249e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
2250e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
2251e41f4b71Sopenharmony_ci
2252e41f4b71Sopenharmony_cilet tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
2253e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
2254e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
2255e41f4b71Sopenharmony_ci  port: 8080
2256e41f4b71Sopenharmony_ci}
2257e41f4b71Sopenharmony_cilet tcpconnectoptions: socket.TCPConnectOptions = {
2258e41f4b71Sopenharmony_ci  address: netAddress,
2259e41f4b71Sopenharmony_ci  timeout: 6000
2260e41f4b71Sopenharmony_ci}
2261e41f4b71Sopenharmony_ci
2262e41f4b71Sopenharmony_ciinterface SocketLinger {
2263e41f4b71Sopenharmony_ci  on: boolean;
2264e41f4b71Sopenharmony_ci  linger: number;
2265e41f4b71Sopenharmony_ci}
2266e41f4b71Sopenharmony_ci
2267e41f4b71Sopenharmony_citcp.connect(tcpconnectoptions, () => {
2268e41f4b71Sopenharmony_ci  console.log('connect success');
2269e41f4b71Sopenharmony_ci  let tcpExtraOptions: socket.TCPExtraOptions = {
2270e41f4b71Sopenharmony_ci    keepAlive: true,
2271e41f4b71Sopenharmony_ci    OOBInline: true,
2272e41f4b71Sopenharmony_ci    TCPNoDelay: true,
2273e41f4b71Sopenharmony_ci    socketLinger: { on: true, linger: 10 } as SocketLinger,
2274e41f4b71Sopenharmony_ci    receiveBufferSize: 1000,
2275e41f4b71Sopenharmony_ci    sendBufferSize: 1000,
2276e41f4b71Sopenharmony_ci    reuseAddress: true,
2277e41f4b71Sopenharmony_ci    socketTimeout: 3000
2278e41f4b71Sopenharmony_ci  }
2279e41f4b71Sopenharmony_ci  tcp.setExtraOptions(tcpExtraOptions).then(() => {
2280e41f4b71Sopenharmony_ci    console.log('setExtraOptions success');
2281e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
2282e41f4b71Sopenharmony_ci    console.log('setExtraOptions fail');
2283e41f4b71Sopenharmony_ci  });
2284e41f4b71Sopenharmony_ci});
2285e41f4b71Sopenharmony_ci```
2286e41f4b71Sopenharmony_ci
2287e41f4b71Sopenharmony_ci### getLocalAddress<sup>12+</sup>
2288e41f4b71Sopenharmony_ci
2289e41f4b71Sopenharmony_cigetLocalAddress(): Promise\<NetAddress\>
2290e41f4b71Sopenharmony_ci
2291e41f4b71Sopenharmony_ciObtains the local socket address of a **TCPSocket** connection. This API uses a promise to return the result.
2292e41f4b71Sopenharmony_ci
2293e41f4b71Sopenharmony_ci> **NOTE**
2294e41f4b71Sopenharmony_ci> This API can be called only after **bind** is successfully called.
2295e41f4b71Sopenharmony_ci
2296e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
2297e41f4b71Sopenharmony_ci
2298e41f4b71Sopenharmony_ci**Return value**
2299e41f4b71Sopenharmony_ci
2300e41f4b71Sopenharmony_ci| Type           | Description                                                |
2301e41f4b71Sopenharmony_ci|  -------------- |  --------------------------------------------------- |
2302e41f4b71Sopenharmony_ci| Promise\<[NetAddress](#netaddress)\> | Promise used to return the result.|
2303e41f4b71Sopenharmony_ci
2304e41f4b71Sopenharmony_ci**Error codes**
2305e41f4b71Sopenharmony_ci
2306e41f4b71Sopenharmony_ci| ID| Error Message                                   |
2307e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- |
2308e41f4b71Sopenharmony_ci| 2300002  | System internal error.                      |
2309e41f4b71Sopenharmony_ci| 2301009  | Bad file descriptor.                            |
2310e41f4b71Sopenharmony_ci| 2303188  | Socket operation on non-socket. |
2311e41f4b71Sopenharmony_ci
2312e41f4b71Sopenharmony_ci**Example**
2313e41f4b71Sopenharmony_ci
2314e41f4b71Sopenharmony_ci```ts
2315e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
2316e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
2317e41f4b71Sopenharmony_ci
2318e41f4b71Sopenharmony_cilet tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
2319e41f4b71Sopenharmony_cilet bindAddr: socket.NetAddress = {
2320e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
2321e41f4b71Sopenharmony_ci  family: 1,
2322e41f4b71Sopenharmony_ci  port: 8080
2323e41f4b71Sopenharmony_ci}
2324e41f4b71Sopenharmony_citcp.bind(bindAddr).then(() => {
2325e41f4b71Sopenharmony_ci  tcp.getLocalAddress().then((localAddress: socket.NetAddress) => {
2326e41f4b71Sopenharmony_ci    console.info("SUCCESS! Address:" + JSON.stringify(localAddress));
2327e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
2328e41f4b71Sopenharmony_ci    console.error("FAILED! Error:" + JSON.stringify(err));
2329e41f4b71Sopenharmony_ci  })
2330e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
2331e41f4b71Sopenharmony_ci  console.error('bind fail');
2332e41f4b71Sopenharmony_ci});
2333e41f4b71Sopenharmony_ci```
2334e41f4b71Sopenharmony_ci
2335e41f4b71Sopenharmony_ci### on('message')
2336e41f4b71Sopenharmony_ci
2337e41f4b71Sopenharmony_cion(type: 'message', callback: Callback<SocketMessageInfo\>): void
2338e41f4b71Sopenharmony_ci
2339e41f4b71Sopenharmony_ciSubscribes to **message** events of the TCP socket connection. This API uses an asynchronous callback to return the result.
2340e41f4b71Sopenharmony_ci
2341e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
2342e41f4b71Sopenharmony_ci
2343e41f4b71Sopenharmony_ci**Parameters**
2344e41f4b71Sopenharmony_ci
2345e41f4b71Sopenharmony_ci| Name  | Type                                                        | Mandatory| Description                                     |
2346e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------- |
2347e41f4b71Sopenharmony_ci| type     | string                                                       | Yes  | Event type.<br/> **message**: message receiving event.|
2348e41f4b71Sopenharmony_ci| callback | Callback\<[SocketMessageInfo](#socketmessageinfo11)\> | Yes  | Callback used to return the result.                           |
2349e41f4b71Sopenharmony_ci
2350e41f4b71Sopenharmony_ci**Example**
2351e41f4b71Sopenharmony_ci
2352e41f4b71Sopenharmony_ci```ts
2353e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
2354e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
2355e41f4b71Sopenharmony_ci
2356e41f4b71Sopenharmony_cilet tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
2357e41f4b71Sopenharmony_cilet messageView = '';
2358e41f4b71Sopenharmony_citcp.on('message', (value: socket.SocketMessageInfo) => {
2359e41f4b71Sopenharmony_ci  for (let i: number = 0; i < value.message.byteLength; i++) {
2360e41f4b71Sopenharmony_ci    let uint8Array = new Uint8Array(value.message) 
2361e41f4b71Sopenharmony_ci    let messages = uint8Array[i]
2362e41f4b71Sopenharmony_ci    let message = String.fromCharCode(messages);
2363e41f4b71Sopenharmony_ci    messageView += message;
2364e41f4b71Sopenharmony_ci  }
2365e41f4b71Sopenharmony_ci  console.log('on message message: ' + JSON.stringify(messageView));
2366e41f4b71Sopenharmony_ci  console.log('remoteInfo: ' + JSON.stringify(value.remoteInfo));
2367e41f4b71Sopenharmony_ci});
2368e41f4b71Sopenharmony_ci```
2369e41f4b71Sopenharmony_ci
2370e41f4b71Sopenharmony_ci### off('message')
2371e41f4b71Sopenharmony_ci
2372e41f4b71Sopenharmony_cioff(type: 'message', callback?: Callback<SocketMessageInfo\>): void
2373e41f4b71Sopenharmony_ci
2374e41f4b71Sopenharmony_ciUnsubscribes from **message** events of the TCP socket connection. This API uses an asynchronous callback to return the result.
2375e41f4b71Sopenharmony_ci
2376e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
2377e41f4b71Sopenharmony_ci
2378e41f4b71Sopenharmony_ci**Parameters**
2379e41f4b71Sopenharmony_ci
2380e41f4b71Sopenharmony_ci| Name  | Type                                                        | Mandatory| Description                                     |
2381e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------- |
2382e41f4b71Sopenharmony_ci| type     | string                                                       | Yes  | Event type.<br/> **message**: message receiving event.|
2383e41f4b71Sopenharmony_ci| callback | Callback\<[SocketMessageInfo](#socketmessageinfo11)\> | No  | Callback used to return the result.                              |
2384e41f4b71Sopenharmony_ci
2385e41f4b71Sopenharmony_ci**Example**
2386e41f4b71Sopenharmony_ci
2387e41f4b71Sopenharmony_ci```ts
2388e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
2389e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
2390e41f4b71Sopenharmony_ci
2391e41f4b71Sopenharmony_cilet tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
2392e41f4b71Sopenharmony_cilet messageView = '';
2393e41f4b71Sopenharmony_cilet callback = (value: socket.SocketMessageInfo) => {
2394e41f4b71Sopenharmony_ci  for (let i: number = 0; i < value.message.byteLength; i++) {
2395e41f4b71Sopenharmony_ci    let uint8Array = new Uint8Array(value.message) 
2396e41f4b71Sopenharmony_ci    let messages = uint8Array[i]
2397e41f4b71Sopenharmony_ci    let message = String.fromCharCode(messages);
2398e41f4b71Sopenharmony_ci    messageView += message;
2399e41f4b71Sopenharmony_ci  }
2400e41f4b71Sopenharmony_ci  console.log('on message message: ' + JSON.stringify(messageView));
2401e41f4b71Sopenharmony_ci  console.log('remoteInfo: ' + JSON.stringify(value.remoteInfo));
2402e41f4b71Sopenharmony_ci}
2403e41f4b71Sopenharmony_citcp.on('message', callback);
2404e41f4b71Sopenharmony_ci// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
2405e41f4b71Sopenharmony_citcp.off('message', callback);
2406e41f4b71Sopenharmony_citcp.off('message');
2407e41f4b71Sopenharmony_ci```
2408e41f4b71Sopenharmony_ci
2409e41f4b71Sopenharmony_ci### on('connect' | 'close')
2410e41f4b71Sopenharmony_ci
2411e41f4b71Sopenharmony_cion(type: 'connect' | 'close', callback: Callback\<void\>): void
2412e41f4b71Sopenharmony_ci
2413e41f4b71Sopenharmony_ciSubscribes to connection or close events of the TCP socket connection. This API uses an asynchronous callback to return the result.
2414e41f4b71Sopenharmony_ci
2415e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
2416e41f4b71Sopenharmony_ci
2417e41f4b71Sopenharmony_ci**Parameters**
2418e41f4b71Sopenharmony_ci
2419e41f4b71Sopenharmony_ci| Name  | Type            | Mandatory| Description                                                        |
2420e41f4b71Sopenharmony_ci| -------- | ---------------- | ---- | ------------------------------------------------------------ |
2421e41f4b71Sopenharmony_ci| type     | string           | Yes  | Event type.<br>- **connect**: connection event.<br>- **close**: close event.|
2422e41f4b71Sopenharmony_ci| callback | Callback\<void\> | Yes  | Callback used to return the result.             |
2423e41f4b71Sopenharmony_ci
2424e41f4b71Sopenharmony_ci**Example**
2425e41f4b71Sopenharmony_ci
2426e41f4b71Sopenharmony_ci```ts
2427e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
2428e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
2429e41f4b71Sopenharmony_ci
2430e41f4b71Sopenharmony_cilet tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
2431e41f4b71Sopenharmony_citcp.on('connect', () => {
2432e41f4b71Sopenharmony_ci  console.log("on connect success")
2433e41f4b71Sopenharmony_ci});
2434e41f4b71Sopenharmony_citcp.on('close', () => {
2435e41f4b71Sopenharmony_ci  console.log("on close success")
2436e41f4b71Sopenharmony_ci});
2437e41f4b71Sopenharmony_ci```
2438e41f4b71Sopenharmony_ci
2439e41f4b71Sopenharmony_ci### off('connect' | 'close')
2440e41f4b71Sopenharmony_ci
2441e41f4b71Sopenharmony_cioff(type: 'connect' | 'close', callback?: Callback\<void\>): void
2442e41f4b71Sopenharmony_ci
2443e41f4b71Sopenharmony_ciUnsubscribes from connection or close events of the TCP socket connection. This API uses an asynchronous callback to return the result.
2444e41f4b71Sopenharmony_ci
2445e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
2446e41f4b71Sopenharmony_ci
2447e41f4b71Sopenharmony_ci**Parameters**
2448e41f4b71Sopenharmony_ci
2449e41f4b71Sopenharmony_ci| Name  | Type            | Mandatory| Description                                                        |
2450e41f4b71Sopenharmony_ci| -------- | ---------------- | ---- | ------------------------------------------------------------ |
2451e41f4b71Sopenharmony_ci| type     | string           | Yes  | Event type.<br>- **connect**: connection event.<br>- **close**: close event.|
2452e41f4b71Sopenharmony_ci| callback | Callback\<void\> | No  | Callback used to return the result.                         |
2453e41f4b71Sopenharmony_ci
2454e41f4b71Sopenharmony_ci**Example**
2455e41f4b71Sopenharmony_ci
2456e41f4b71Sopenharmony_ci```ts
2457e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
2458e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
2459e41f4b71Sopenharmony_ci
2460e41f4b71Sopenharmony_cilet tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
2461e41f4b71Sopenharmony_cilet callback1 = () => {
2462e41f4b71Sopenharmony_ci  console.log("on connect success");
2463e41f4b71Sopenharmony_ci}
2464e41f4b71Sopenharmony_citcp.on('connect', callback1);
2465e41f4b71Sopenharmony_ci// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
2466e41f4b71Sopenharmony_citcp.off('connect', callback1);
2467e41f4b71Sopenharmony_citcp.off('connect');
2468e41f4b71Sopenharmony_cilet callback2 = () => {
2469e41f4b71Sopenharmony_ci  console.log("on close success");
2470e41f4b71Sopenharmony_ci}
2471e41f4b71Sopenharmony_citcp.on('close', callback2);
2472e41f4b71Sopenharmony_ci// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
2473e41f4b71Sopenharmony_citcp.off('close', callback2);
2474e41f4b71Sopenharmony_citcp.off('close');
2475e41f4b71Sopenharmony_ci```
2476e41f4b71Sopenharmony_ci
2477e41f4b71Sopenharmony_ci### on('error')
2478e41f4b71Sopenharmony_ci
2479e41f4b71Sopenharmony_cion(type: 'error', callback: ErrorCallback): void
2480e41f4b71Sopenharmony_ci
2481e41f4b71Sopenharmony_ciSubscribes to **error** events of the TCP socket connection. This API uses an asynchronous callback to return the result.
2482e41f4b71Sopenharmony_ci
2483e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
2484e41f4b71Sopenharmony_ci
2485e41f4b71Sopenharmony_ci**Parameters**
2486e41f4b71Sopenharmony_ci
2487e41f4b71Sopenharmony_ci| Name  | Type         | Mandatory| Description                                |
2488e41f4b71Sopenharmony_ci| -------- | ------------- | ---- | ------------------------------------ |
2489e41f4b71Sopenharmony_ci| type     | string        | Yes  | Event type.<br/> **error**: error event.|
2490e41f4b71Sopenharmony_ci| callback | ErrorCallback | Yes  | Callback used to return the result.                            |
2491e41f4b71Sopenharmony_ci
2492e41f4b71Sopenharmony_ci**Example**
2493e41f4b71Sopenharmony_ci
2494e41f4b71Sopenharmony_ci```ts
2495e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
2496e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
2497e41f4b71Sopenharmony_ci
2498e41f4b71Sopenharmony_cilet tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
2499e41f4b71Sopenharmony_citcp.on('error', (err: BusinessError) => {
2500e41f4b71Sopenharmony_ci  console.log("on error, err:" + JSON.stringify(err))
2501e41f4b71Sopenharmony_ci});
2502e41f4b71Sopenharmony_ci```
2503e41f4b71Sopenharmony_ci
2504e41f4b71Sopenharmony_ci### off('error')
2505e41f4b71Sopenharmony_ci
2506e41f4b71Sopenharmony_cioff(type: 'error', callback?: ErrorCallback): void
2507e41f4b71Sopenharmony_ci
2508e41f4b71Sopenharmony_ciUnsubscribes from **error** events of the TCP socket connection. This API uses an asynchronous callback to return the result.
2509e41f4b71Sopenharmony_ci
2510e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
2511e41f4b71Sopenharmony_ci
2512e41f4b71Sopenharmony_ci**Parameters**
2513e41f4b71Sopenharmony_ci
2514e41f4b71Sopenharmony_ci| Name  | Type         | Mandatory| Description                                |
2515e41f4b71Sopenharmony_ci| -------- | ------------- | ---- | ------------------------------------ |
2516e41f4b71Sopenharmony_ci| type     | string        | Yes  | Event type.<br/> **error**: error event.|
2517e41f4b71Sopenharmony_ci| callback | ErrorCallback | No  | Callback used to return the result.                            |
2518e41f4b71Sopenharmony_ci
2519e41f4b71Sopenharmony_ci**Example**
2520e41f4b71Sopenharmony_ci
2521e41f4b71Sopenharmony_ci```ts
2522e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
2523e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
2524e41f4b71Sopenharmony_ci
2525e41f4b71Sopenharmony_cilet tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
2526e41f4b71Sopenharmony_cilet callback = (err: BusinessError) => {
2527e41f4b71Sopenharmony_ci  console.log("on error, err:" + JSON.stringify(err));
2528e41f4b71Sopenharmony_ci}
2529e41f4b71Sopenharmony_citcp.on('error', callback);
2530e41f4b71Sopenharmony_ci// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
2531e41f4b71Sopenharmony_citcp.off('error', callback);
2532e41f4b71Sopenharmony_citcp.off('error');
2533e41f4b71Sopenharmony_ci```
2534e41f4b71Sopenharmony_ci
2535e41f4b71Sopenharmony_ci## TCPConnectOptions
2536e41f4b71Sopenharmony_ci
2537e41f4b71Sopenharmony_ciDefines TCP socket connection parameters.
2538e41f4b71Sopenharmony_ci
2539e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
2540e41f4b71Sopenharmony_ci
2541e41f4b71Sopenharmony_ci| Name | Type                              | Mandatory| Description                      |
2542e41f4b71Sopenharmony_ci| ------- | ---------------------------------- | ---- | -------------------------- |
2543e41f4b71Sopenharmony_ci| address | [NetAddress](#netaddress) | Yes  | Bound IP address and port number.      |
2544e41f4b71Sopenharmony_ci| timeout | number                             | No  | Timeout duration of the TCP socket connection, in ms.|
2545e41f4b71Sopenharmony_ci
2546e41f4b71Sopenharmony_ci## TCPSendOptions
2547e41f4b71Sopenharmony_ci
2548e41f4b71Sopenharmony_ciDefines the parameters for sending data over a TCP socket connection.
2549e41f4b71Sopenharmony_ci
2550e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
2551e41f4b71Sopenharmony_ci
2552e41f4b71Sopenharmony_ci| Name  | Type  | Mandatory| Description                                                        |
2553e41f4b71Sopenharmony_ci| -------- | ------ | ---- | ------------------------------------------------------------ |
2554e41f4b71Sopenharmony_ci| data     | string\| ArrayBuffer  | Yes  | Data to send.                                                |
2555e41f4b71Sopenharmony_ci| encoding | string | No  | Character encoding format. The options are as follows: **UTF-8**, **UTF-16BE**, **UTF-16LE**, **UTF-16**, **US-AECII**, and **ISO-8859-1**. The default value is **UTF-8**.|
2556e41f4b71Sopenharmony_ci
2557e41f4b71Sopenharmony_ci## TCPExtraOptions
2558e41f4b71Sopenharmony_ci
2559e41f4b71Sopenharmony_ciDefines other properties of the TCP socket connection. This API inherits from [ExtraOptionsBase](#extraoptionsbase7).
2560e41f4b71Sopenharmony_ci
2561e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
2562e41f4b71Sopenharmony_ci
2563e41f4b71Sopenharmony_ci| Name           | Type   | Mandatory| Description                                                        |
2564e41f4b71Sopenharmony_ci| ----------------- | ------- | ---- | ------------------------------------------------------------ |
2565e41f4b71Sopenharmony_ci| keepAlive         | boolean | No  | Whether to keep the connection alive. The default value is **false**.                                 |
2566e41f4b71Sopenharmony_ci| OOBInline         | boolean | No  | Whether to enable OOBInline. The default value is **false**.                                |
2567e41f4b71Sopenharmony_ci| TCPNoDelay        | boolean | No  | Whether to enable no-delay on the TCP socket connection. The default value is **false**.                      |
2568e41f4b71Sopenharmony_ci| socketLinger      | \{on:boolean, linger:number\}  | No  | Socket linger.<br>- **on**: whether to enable socket linger. The value true means to enable socket linger and false means the opposite.<br>- **linger**: linger time, in ms. The value ranges from **0** to **65535**.<br>Specify this parameter only when **on** is set to **true**.|
2569e41f4b71Sopenharmony_ci
2570e41f4b71Sopenharmony_ci## socket.constructTCPSocketServerInstance<sup>10+</sup>
2571e41f4b71Sopenharmony_ci
2572e41f4b71Sopenharmony_ciconstructTCPSocketServerInstance(): TCPSocketServer
2573e41f4b71Sopenharmony_ci
2574e41f4b71Sopenharmony_ciCreates a **TCPSocketServer** object.
2575e41f4b71Sopenharmony_ci
2576e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
2577e41f4b71Sopenharmony_ci
2578e41f4b71Sopenharmony_ci**Return value**
2579e41f4b71Sopenharmony_ci
2580e41f4b71Sopenharmony_ci| Type                               | Description                         |
2581e41f4b71Sopenharmony_ci|  ---------------------------------- |  ---------------------------- |
2582e41f4b71Sopenharmony_ci| [TCPSocketServer](#tcpsocketserver10) | **TCPSocketServer** object.|
2583e41f4b71Sopenharmony_ci
2584e41f4b71Sopenharmony_ci**Example**
2585e41f4b71Sopenharmony_ci
2586e41f4b71Sopenharmony_ci```ts
2587e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
2588e41f4b71Sopenharmony_cilet tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance();
2589e41f4b71Sopenharmony_ci```
2590e41f4b71Sopenharmony_ci
2591e41f4b71Sopenharmony_ci## TCPSocketServer<sup>10+</sup>
2592e41f4b71Sopenharmony_ci
2593e41f4b71Sopenharmony_ciDefines a TCP socket server connection. Before calling TCPSocketServer APIs, you need to call [socket.constructTCPSocketServerInstance](#socketconstructtcpsocketserverinstance10) to create a **TCPSocketServer** object.
2594e41f4b71Sopenharmony_ci
2595e41f4b71Sopenharmony_ci### listen<sup>10+</sup>
2596e41f4b71Sopenharmony_ci
2597e41f4b71Sopenharmony_cilisten(address: NetAddress, callback: AsyncCallback\<void\>): void
2598e41f4b71Sopenharmony_ci
2599e41f4b71Sopenharmony_ciBinds the IP address and port number. The port number can be specified or randomly allocated by the system. The server listens to and accepts TCP socket connections established over the socket. Multiple threads are used to process client data concurrently. This API uses an asynchronous callback to return the result.
2600e41f4b71Sopenharmony_ci
2601e41f4b71Sopenharmony_ci> **NOTE**<br>
2602e41f4b71Sopenharmony_ci> The server uses this API to perform the **bind**, **listen**, and **accept** operations. If the **bind** operation fails, the system randomly allocates a port number.
2603e41f4b71Sopenharmony_ci
2604e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
2605e41f4b71Sopenharmony_ci
2606e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
2607e41f4b71Sopenharmony_ci
2608e41f4b71Sopenharmony_ci**Parameters**
2609e41f4b71Sopenharmony_ci
2610e41f4b71Sopenharmony_ci| Name  | Type                     | Mandatory| Description                                         |
2611e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | --------------------------------------------- |
2612e41f4b71Sopenharmony_ci| address  | [NetAddress](#netaddress) | Yes  | Destination address.|
2613e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void\>     | Yes  | Callback used to return the result. If the operation fails, an error message is returned.   |
2614e41f4b71Sopenharmony_ci
2615e41f4b71Sopenharmony_ci**Error codes**
2616e41f4b71Sopenharmony_ci
2617e41f4b71Sopenharmony_ci| ID| Error Message                                   |
2618e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- |
2619e41f4b71Sopenharmony_ci| 401      | Parameter error.                            |
2620e41f4b71Sopenharmony_ci| 201      | Permission denied.                          |
2621e41f4b71Sopenharmony_ci| 2300002  | System internal error.                      |
2622e41f4b71Sopenharmony_ci| 2303109  | Bad file number.                            |
2623e41f4b71Sopenharmony_ci| 2303111  | Resource temporarily unavailable. Try again.|
2624e41f4b71Sopenharmony_ci| 2303198  | Address already in use.                     |
2625e41f4b71Sopenharmony_ci| 2303199  | Cannot assign requested address.            |
2626e41f4b71Sopenharmony_ci
2627e41f4b71Sopenharmony_ci**Example**
2628e41f4b71Sopenharmony_ci
2629e41f4b71Sopenharmony_ci```ts
2630e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
2631e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
2632e41f4b71Sopenharmony_ci
2633e41f4b71Sopenharmony_cilet tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance();
2634e41f4b71Sopenharmony_cilet listenAddr: socket.NetAddress = {
2635e41f4b71Sopenharmony_ci  address:  '192.168.xx.xxx',
2636e41f4b71Sopenharmony_ci  port: 8080,
2637e41f4b71Sopenharmony_ci  family: 1
2638e41f4b71Sopenharmony_ci}
2639e41f4b71Sopenharmony_citcpServer.listen(listenAddr, (err: BusinessError) => {
2640e41f4b71Sopenharmony_ci  if (err) {
2641e41f4b71Sopenharmony_ci    console.log("listen fail");
2642e41f4b71Sopenharmony_ci    return;
2643e41f4b71Sopenharmony_ci  }
2644e41f4b71Sopenharmony_ci  console.log("listen success");
2645e41f4b71Sopenharmony_ci})
2646e41f4b71Sopenharmony_ci```
2647e41f4b71Sopenharmony_ci
2648e41f4b71Sopenharmony_ci### listen<sup>10+</sup>
2649e41f4b71Sopenharmony_ci
2650e41f4b71Sopenharmony_cilisten(address: NetAddress): Promise\<void\>
2651e41f4b71Sopenharmony_ci
2652e41f4b71Sopenharmony_ciBinds the IP address and port number. The port number can be specified or randomly allocated by the system. The server listens to and accepts TCP socket connections established over the socket. Multiple threads are used to process client data concurrently. This API uses a promise to return the result.
2653e41f4b71Sopenharmony_ci
2654e41f4b71Sopenharmony_ci> **NOTE**<br>
2655e41f4b71Sopenharmony_ci> The server uses this API to perform the **bind**, **listen**, and **accept** operations. If the **bind** operation fails, the system randomly allocates a port number.
2656e41f4b71Sopenharmony_ci
2657e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
2658e41f4b71Sopenharmony_ci
2659e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
2660e41f4b71Sopenharmony_ci
2661e41f4b71Sopenharmony_ci**Parameters**
2662e41f4b71Sopenharmony_ci
2663e41f4b71Sopenharmony_ci| Name | Type                     | Mandatory| Description                                         |
2664e41f4b71Sopenharmony_ci| ------- | ------------------------- | ---- | --------------------------------------------- |
2665e41f4b71Sopenharmony_ci| address | [NetAddress](#netaddress) | Yes  | Destination address.|
2666e41f4b71Sopenharmony_ci
2667e41f4b71Sopenharmony_ci**Return value**
2668e41f4b71Sopenharmony_ci
2669e41f4b71Sopenharmony_ci| Type           | Description                                                        |
2670e41f4b71Sopenharmony_ci|  -------------- |  ----------------------------------------------------------- |
2671e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
2672e41f4b71Sopenharmony_ci
2673e41f4b71Sopenharmony_ci**Error codes**
2674e41f4b71Sopenharmony_ci
2675e41f4b71Sopenharmony_ci| ID| Error Message                                   |
2676e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- |
2677e41f4b71Sopenharmony_ci| 401      | Parameter error.                            |
2678e41f4b71Sopenharmony_ci| 201      | Permission denied.                          |
2679e41f4b71Sopenharmony_ci| 2300002  | System internal error.                      |
2680e41f4b71Sopenharmony_ci| 2303109  | Bad file number.                            |
2681e41f4b71Sopenharmony_ci| 2303111  | Resource temporarily unavailable. Try again.|
2682e41f4b71Sopenharmony_ci| 2303198  | Address already in use.                     |
2683e41f4b71Sopenharmony_ci| 2303199  | Cannot assign requested address.            |
2684e41f4b71Sopenharmony_ci
2685e41f4b71Sopenharmony_ci**Example**
2686e41f4b71Sopenharmony_ci
2687e41f4b71Sopenharmony_ci```ts
2688e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
2689e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
2690e41f4b71Sopenharmony_ci
2691e41f4b71Sopenharmony_cilet tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance();
2692e41f4b71Sopenharmony_cilet listenAddr: socket.NetAddress = {
2693e41f4b71Sopenharmony_ci  address:  '192.168.xx.xxx',
2694e41f4b71Sopenharmony_ci  port: 8080,
2695e41f4b71Sopenharmony_ci  family: 1
2696e41f4b71Sopenharmony_ci}
2697e41f4b71Sopenharmony_citcpServer.listen(listenAddr).then(() => {
2698e41f4b71Sopenharmony_ci  console.log('listen success');
2699e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
2700e41f4b71Sopenharmony_ci  console.log('listen fail');
2701e41f4b71Sopenharmony_ci});
2702e41f4b71Sopenharmony_ci```
2703e41f4b71Sopenharmony_ci
2704e41f4b71Sopenharmony_ci### getState<sup>10+</sup>
2705e41f4b71Sopenharmony_ci
2706e41f4b71Sopenharmony_cigetState(callback: AsyncCallback\<SocketStateBase\>): void
2707e41f4b71Sopenharmony_ci
2708e41f4b71Sopenharmony_ciObtains the status of a TCP socket server connection. This API uses an asynchronous callback to return the result.
2709e41f4b71Sopenharmony_ci
2710e41f4b71Sopenharmony_ci> **NOTE**
2711e41f4b71Sopenharmony_ci> This API can be called only after **listen** is successfully called.
2712e41f4b71Sopenharmony_ci
2713e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
2714e41f4b71Sopenharmony_ci
2715e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
2716e41f4b71Sopenharmony_ci
2717e41f4b71Sopenharmony_ci**Parameters**
2718e41f4b71Sopenharmony_ci
2719e41f4b71Sopenharmony_ci| Name  | Type                                              | Mandatory| Description      |
2720e41f4b71Sopenharmony_ci| -------- | -------------------------------------------------- | ---- | ---------- |
2721e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[SocketStateBase](#socketstatebase)\> | Yes  | Callback used to return the result. If the operation fails, an error message is returned.|
2722e41f4b71Sopenharmony_ci
2723e41f4b71Sopenharmony_ci**Error codes**
2724e41f4b71Sopenharmony_ci
2725e41f4b71Sopenharmony_ci| ID| Error Message                       |
2726e41f4b71Sopenharmony_ci| -------- | ------------------------------- |
2727e41f4b71Sopenharmony_ci| 401      | Parameter error.                |
2728e41f4b71Sopenharmony_ci| 201      | Permission denied.              |
2729e41f4b71Sopenharmony_ci| 2300002  | System internal error.          |
2730e41f4b71Sopenharmony_ci| 2303188  | Socket operation on non-socket. |
2731e41f4b71Sopenharmony_ci
2732e41f4b71Sopenharmony_ci**Example**
2733e41f4b71Sopenharmony_ci
2734e41f4b71Sopenharmony_ci```ts
2735e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
2736e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
2737e41f4b71Sopenharmony_ci
2738e41f4b71Sopenharmony_cilet tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance();
2739e41f4b71Sopenharmony_cilet listenAddr: socket.NetAddress = {
2740e41f4b71Sopenharmony_ci  address:  '192.168.xx.xxx',
2741e41f4b71Sopenharmony_ci  port: 8080,
2742e41f4b71Sopenharmony_ci  family: 1
2743e41f4b71Sopenharmony_ci}
2744e41f4b71Sopenharmony_citcpServer.listen(listenAddr, (err: BusinessError) => {
2745e41f4b71Sopenharmony_ci  if (err) {
2746e41f4b71Sopenharmony_ci    console.log("listen fail");
2747e41f4b71Sopenharmony_ci    return;
2748e41f4b71Sopenharmony_ci  }
2749e41f4b71Sopenharmony_ci  console.log("listen success");
2750e41f4b71Sopenharmony_ci})
2751e41f4b71Sopenharmony_citcpServer.getState((err: BusinessError, data: socket.SocketStateBase) => {
2752e41f4b71Sopenharmony_ci  if (err) {
2753e41f4b71Sopenharmony_ci    console.log('getState fail');
2754e41f4b71Sopenharmony_ci    return;
2755e41f4b71Sopenharmony_ci  }
2756e41f4b71Sopenharmony_ci  console.log('getState success:' + JSON.stringify(data));
2757e41f4b71Sopenharmony_ci})
2758e41f4b71Sopenharmony_ci```
2759e41f4b71Sopenharmony_ci
2760e41f4b71Sopenharmony_ci### getState<sup>10+</sup>
2761e41f4b71Sopenharmony_ci
2762e41f4b71Sopenharmony_cigetState(): Promise\<SocketStateBase\>
2763e41f4b71Sopenharmony_ci
2764e41f4b71Sopenharmony_ciObtains the status of a TCP socket server connection. This API uses a promise to return the result.
2765e41f4b71Sopenharmony_ci
2766e41f4b71Sopenharmony_ci> **NOTE**
2767e41f4b71Sopenharmony_ci> This API can be called only after **listen** is successfully called.
2768e41f4b71Sopenharmony_ci
2769e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
2770e41f4b71Sopenharmony_ci
2771e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
2772e41f4b71Sopenharmony_ci
2773e41f4b71Sopenharmony_ci**Return value**
2774e41f4b71Sopenharmony_ci
2775e41f4b71Sopenharmony_ci| Type                                        | Description                                      |
2776e41f4b71Sopenharmony_ci|  ------------------------------------------- |  ----------------------------------------- |
2777e41f4b71Sopenharmony_ci| Promise\<[SocketStateBase](#socketstatebase)\> | Promise used to return the result.|
2778e41f4b71Sopenharmony_ci
2779e41f4b71Sopenharmony_ci**Error codes**
2780e41f4b71Sopenharmony_ci
2781e41f4b71Sopenharmony_ci| ID| Error Message                       |
2782e41f4b71Sopenharmony_ci| -------- | ------------------------------- |
2783e41f4b71Sopenharmony_ci| 201      | Permission denied.              |
2784e41f4b71Sopenharmony_ci| 2300002  | System internal error.          |
2785e41f4b71Sopenharmony_ci| 2303188  | Socket operation on non-socket. |
2786e41f4b71Sopenharmony_ci
2787e41f4b71Sopenharmony_ci**Example**
2788e41f4b71Sopenharmony_ci
2789e41f4b71Sopenharmony_ci```ts
2790e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
2791e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
2792e41f4b71Sopenharmony_ci
2793e41f4b71Sopenharmony_cilet tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance();
2794e41f4b71Sopenharmony_cilet listenAddr: socket.NetAddress = {
2795e41f4b71Sopenharmony_ci  address:  '192.168.xx.xxx',
2796e41f4b71Sopenharmony_ci  port: 8080,
2797e41f4b71Sopenharmony_ci  family: 1
2798e41f4b71Sopenharmony_ci}
2799e41f4b71Sopenharmony_citcpServer.listen(listenAddr, (err: BusinessError) => {
2800e41f4b71Sopenharmony_ci  if (err) {
2801e41f4b71Sopenharmony_ci    console.log("listen fail");
2802e41f4b71Sopenharmony_ci    return;
2803e41f4b71Sopenharmony_ci  }
2804e41f4b71Sopenharmony_ci  console.log("listen success");
2805e41f4b71Sopenharmony_ci})
2806e41f4b71Sopenharmony_citcpServer.getState().then((data: socket.SocketStateBase) => {
2807e41f4b71Sopenharmony_ci  console.log('getState success' + JSON.stringify(data));
2808e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
2809e41f4b71Sopenharmony_ci  console.log('getState fail');
2810e41f4b71Sopenharmony_ci});
2811e41f4b71Sopenharmony_ci```
2812e41f4b71Sopenharmony_ci
2813e41f4b71Sopenharmony_ci### setExtraOptions<sup>10+</sup>
2814e41f4b71Sopenharmony_ci
2815e41f4b71Sopenharmony_cisetExtraOptions(options: TCPExtraOptions, callback: AsyncCallback\<void\>): void
2816e41f4b71Sopenharmony_ci
2817e41f4b71Sopenharmony_ciSets other properties of a TCP socket server connection. This API uses an asynchronous callback to return the result.
2818e41f4b71Sopenharmony_ci
2819e41f4b71Sopenharmony_ci> **NOTE**
2820e41f4b71Sopenharmony_ci> This API can be called only after **listen** is successfully called.
2821e41f4b71Sopenharmony_ci
2822e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
2823e41f4b71Sopenharmony_ci
2824e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
2825e41f4b71Sopenharmony_ci
2826e41f4b71Sopenharmony_ci**Parameters**
2827e41f4b71Sopenharmony_ci
2828e41f4b71Sopenharmony_ci| Name  | Type                               | Mandatory| Description                                                        |
2829e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | ---- | ------------------------------------------------------------ |
2830e41f4b71Sopenharmony_ci| options  | [TCPExtraOptions](#tcpextraoptions) | Yes  | Other properties of a TCP socket server connection.|
2831e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void\>               | Yes  | Callback used to return the result. If the operation fails, an error message is returned.               |
2832e41f4b71Sopenharmony_ci
2833e41f4b71Sopenharmony_ci**Error codes**
2834e41f4b71Sopenharmony_ci
2835e41f4b71Sopenharmony_ci| ID| Error Message                       |
2836e41f4b71Sopenharmony_ci| -------- | ------------------------------- |
2837e41f4b71Sopenharmony_ci| 401      | Parameter error.                |
2838e41f4b71Sopenharmony_ci| 201      | Permission denied.              |
2839e41f4b71Sopenharmony_ci| 2300002  | System internal error.          |
2840e41f4b71Sopenharmony_ci| 2303188  | Socket operation on non-socket. |
2841e41f4b71Sopenharmony_ci
2842e41f4b71Sopenharmony_ci**Example**
2843e41f4b71Sopenharmony_ci
2844e41f4b71Sopenharmony_ci```ts
2845e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
2846e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
2847e41f4b71Sopenharmony_ci
2848e41f4b71Sopenharmony_cilet tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance();
2849e41f4b71Sopenharmony_cilet listenAddr: socket.NetAddress = {
2850e41f4b71Sopenharmony_ci  address:  '192.168.xx.xxx',
2851e41f4b71Sopenharmony_ci  port: 8080,
2852e41f4b71Sopenharmony_ci  family: 1
2853e41f4b71Sopenharmony_ci}
2854e41f4b71Sopenharmony_citcpServer.listen(listenAddr, (err: BusinessError) => {
2855e41f4b71Sopenharmony_ci  if (err) {
2856e41f4b71Sopenharmony_ci    console.log("listen fail");
2857e41f4b71Sopenharmony_ci    return;
2858e41f4b71Sopenharmony_ci  }
2859e41f4b71Sopenharmony_ci  console.log("listen success");
2860e41f4b71Sopenharmony_ci})
2861e41f4b71Sopenharmony_ci
2862e41f4b71Sopenharmony_ciinterface SocketLinger {
2863e41f4b71Sopenharmony_ci  on: boolean;
2864e41f4b71Sopenharmony_ci  linger: number;
2865e41f4b71Sopenharmony_ci}
2866e41f4b71Sopenharmony_ci
2867e41f4b71Sopenharmony_cilet tcpExtraOptions: socket.TCPExtraOptions = {
2868e41f4b71Sopenharmony_ci  keepAlive: true,
2869e41f4b71Sopenharmony_ci  OOBInline: true,
2870e41f4b71Sopenharmony_ci  TCPNoDelay: true,
2871e41f4b71Sopenharmony_ci  socketLinger: { on: true, linger: 10 } as SocketLinger,
2872e41f4b71Sopenharmony_ci  receiveBufferSize: 1000,
2873e41f4b71Sopenharmony_ci  sendBufferSize: 1000,
2874e41f4b71Sopenharmony_ci  reuseAddress: true,
2875e41f4b71Sopenharmony_ci  socketTimeout: 3000
2876e41f4b71Sopenharmony_ci}
2877e41f4b71Sopenharmony_citcpServer.setExtraOptions(tcpExtraOptions, (err: BusinessError) => {
2878e41f4b71Sopenharmony_ci  if (err) {
2879e41f4b71Sopenharmony_ci    console.log('setExtraOptions fail');
2880e41f4b71Sopenharmony_ci    return;
2881e41f4b71Sopenharmony_ci  }
2882e41f4b71Sopenharmony_ci  console.log('setExtraOptions success');
2883e41f4b71Sopenharmony_ci});
2884e41f4b71Sopenharmony_ci```
2885e41f4b71Sopenharmony_ci
2886e41f4b71Sopenharmony_ci### setExtraOptions<sup>10+</sup>
2887e41f4b71Sopenharmony_ci
2888e41f4b71Sopenharmony_cisetExtraOptions(options: TCPExtraOptions): Promise\<void\>
2889e41f4b71Sopenharmony_ci
2890e41f4b71Sopenharmony_ciSets other properties of a TCP socket server connection. This API uses a promise to return the result.
2891e41f4b71Sopenharmony_ci
2892e41f4b71Sopenharmony_ci> **NOTE**
2893e41f4b71Sopenharmony_ci> This API can be called only after **listen** is successfully called.
2894e41f4b71Sopenharmony_ci
2895e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
2896e41f4b71Sopenharmony_ci
2897e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
2898e41f4b71Sopenharmony_ci
2899e41f4b71Sopenharmony_ci**Parameters**
2900e41f4b71Sopenharmony_ci
2901e41f4b71Sopenharmony_ci| Name | Type                               | Mandatory| Description                                                        |
2902e41f4b71Sopenharmony_ci| ------- | ----------------------------------- | ---- | ------------------------------------------------------------ |
2903e41f4b71Sopenharmony_ci| options | [TCPExtraOptions](#tcpextraoptions) | Yes  | Other properties of a TCP socket server connection.|
2904e41f4b71Sopenharmony_ci
2905e41f4b71Sopenharmony_ci**Return value**
2906e41f4b71Sopenharmony_ci
2907e41f4b71Sopenharmony_ci| Type           | Description                                                      |
2908e41f4b71Sopenharmony_ci|  -------------- |  --------------------------------------------------------- |
2909e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
2910e41f4b71Sopenharmony_ci
2911e41f4b71Sopenharmony_ci**Error codes**
2912e41f4b71Sopenharmony_ci
2913e41f4b71Sopenharmony_ci| ID| Error Message                       |
2914e41f4b71Sopenharmony_ci| -------- | ------------------------------- |
2915e41f4b71Sopenharmony_ci| 401      | Parameter error.                |
2916e41f4b71Sopenharmony_ci| 201      | Permission denied.              |
2917e41f4b71Sopenharmony_ci| 2300002  | System internal error.          |
2918e41f4b71Sopenharmony_ci| 2303188  | Socket operation on non-socket. |
2919e41f4b71Sopenharmony_ci
2920e41f4b71Sopenharmony_ci**Example**
2921e41f4b71Sopenharmony_ci
2922e41f4b71Sopenharmony_ci```ts
2923e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
2924e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
2925e41f4b71Sopenharmony_ci
2926e41f4b71Sopenharmony_cilet tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance();
2927e41f4b71Sopenharmony_cilet listenAddr: socket.NetAddress = {
2928e41f4b71Sopenharmony_ci  address:  '192.168.xx.xxx',
2929e41f4b71Sopenharmony_ci  port: 8080,
2930e41f4b71Sopenharmony_ci  family: 1
2931e41f4b71Sopenharmony_ci}
2932e41f4b71Sopenharmony_ci
2933e41f4b71Sopenharmony_ciinterface SocketLinger {
2934e41f4b71Sopenharmony_ci  on: boolean;
2935e41f4b71Sopenharmony_ci  linger: number;
2936e41f4b71Sopenharmony_ci}
2937e41f4b71Sopenharmony_ci
2938e41f4b71Sopenharmony_citcpServer.listen(listenAddr, (err: BusinessError) => {
2939e41f4b71Sopenharmony_ci  if (err) {
2940e41f4b71Sopenharmony_ci    console.log("listen fail");
2941e41f4b71Sopenharmony_ci    return;
2942e41f4b71Sopenharmony_ci  }
2943e41f4b71Sopenharmony_ci  console.log("listen success");
2944e41f4b71Sopenharmony_ci})
2945e41f4b71Sopenharmony_ci
2946e41f4b71Sopenharmony_cilet tcpExtraOptions: socket.TCPExtraOptions = {
2947e41f4b71Sopenharmony_ci  keepAlive: true,
2948e41f4b71Sopenharmony_ci  OOBInline: true,
2949e41f4b71Sopenharmony_ci  TCPNoDelay: true,
2950e41f4b71Sopenharmony_ci  socketLinger: { on: true, linger: 10 } as SocketLinger,
2951e41f4b71Sopenharmony_ci  receiveBufferSize: 1000,
2952e41f4b71Sopenharmony_ci  sendBufferSize: 1000,
2953e41f4b71Sopenharmony_ci  reuseAddress: true,
2954e41f4b71Sopenharmony_ci  socketTimeout: 3000
2955e41f4b71Sopenharmony_ci}
2956e41f4b71Sopenharmony_citcpServer.setExtraOptions(tcpExtraOptions).then(() => {
2957e41f4b71Sopenharmony_ci  console.log('setExtraOptions success');
2958e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
2959e41f4b71Sopenharmony_ci  console.log('setExtraOptions fail');
2960e41f4b71Sopenharmony_ci});
2961e41f4b71Sopenharmony_ci```
2962e41f4b71Sopenharmony_ci
2963e41f4b71Sopenharmony_ci### getLocalAddress<sup>12+</sup>
2964e41f4b71Sopenharmony_ci
2965e41f4b71Sopenharmony_cigetLocalAddress(): Promise\<NetAddress\>
2966e41f4b71Sopenharmony_ci
2967e41f4b71Sopenharmony_ciObtains the local socket address of a **TCPSocketServer** connection. This API uses a promise to return the result.
2968e41f4b71Sopenharmony_ci
2969e41f4b71Sopenharmony_ci> **NOTE**
2970e41f4b71Sopenharmony_ci> This API can be called only after **listen** is successfully called.
2971e41f4b71Sopenharmony_ci
2972e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
2973e41f4b71Sopenharmony_ci
2974e41f4b71Sopenharmony_ci**Return value**
2975e41f4b71Sopenharmony_ci
2976e41f4b71Sopenharmony_ci| Type           | Description                                                |
2977e41f4b71Sopenharmony_ci|  -------------- |  --------------------------------------------------- |
2978e41f4b71Sopenharmony_ci| Promise\<[NetAddress](#netaddress)\> | Promise used to return the result.|
2979e41f4b71Sopenharmony_ci
2980e41f4b71Sopenharmony_ci**Error codes**
2981e41f4b71Sopenharmony_ci
2982e41f4b71Sopenharmony_ci| ID| Error Message                                   |
2983e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- |
2984e41f4b71Sopenharmony_ci| 2300002  | System internal error.                      |
2985e41f4b71Sopenharmony_ci| 2301009  | Bad file descriptor.                            |
2986e41f4b71Sopenharmony_ci| 2303188  | Socket operation on non-socket. |
2987e41f4b71Sopenharmony_ci
2988e41f4b71Sopenharmony_ci**Example**
2989e41f4b71Sopenharmony_ci
2990e41f4b71Sopenharmony_ci```ts
2991e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
2992e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
2993e41f4b71Sopenharmony_ci
2994e41f4b71Sopenharmony_cilet tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance();
2995e41f4b71Sopenharmony_cilet listenAddr: socket.NetAddress = {
2996e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
2997e41f4b71Sopenharmony_ci  port: 8080,
2998e41f4b71Sopenharmony_ci  family: 1
2999e41f4b71Sopenharmony_ci}
3000e41f4b71Sopenharmony_citcpServer.listen(listenAddr).then(() => {
3001e41f4b71Sopenharmony_ci  tcpServer.getLocalAddress().then((localAddress: socket.NetAddress) => {
3002e41f4b71Sopenharmony_ci    console.info("SUCCESS! Address:" + JSON.stringify(localAddress));
3003e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
3004e41f4b71Sopenharmony_ci    console.error("FerrorAILED! Error:" + JSON.stringify(err));
3005e41f4b71Sopenharmony_ci  })
3006e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
3007e41f4b71Sopenharmony_ci  console.error('listen fail');
3008e41f4b71Sopenharmony_ci});
3009e41f4b71Sopenharmony_ci```
3010e41f4b71Sopenharmony_ci
3011e41f4b71Sopenharmony_ci### on('connect')<sup>10+</sup>
3012e41f4b71Sopenharmony_ci
3013e41f4b71Sopenharmony_cion(type: 'connect', callback: Callback\<TCPSocketConnection\>): void
3014e41f4b71Sopenharmony_ci
3015e41f4b71Sopenharmony_ciSubscribes to **connect** events of a **TCPSocketServer** object. This API uses an asynchronous callback to return the result.
3016e41f4b71Sopenharmony_ci
3017e41f4b71Sopenharmony_ci> **NOTE**
3018e41f4b71Sopenharmony_ci> This API can be called only after **listen** is successfully called.
3019e41f4b71Sopenharmony_ci
3020e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
3021e41f4b71Sopenharmony_ci
3022e41f4b71Sopenharmony_ci**Parameters**
3023e41f4b71Sopenharmony_ci
3024e41f4b71Sopenharmony_ci| Name  | Type                           | Mandatory| Description                                 |
3025e41f4b71Sopenharmony_ci| -------- | ------------------------------- | ---- | ------------------------------------- |
3026e41f4b71Sopenharmony_ci| type     | string                          | Yes  | Event type.<br/> **connect**: connection event.|
3027e41f4b71Sopenharmony_ci| callback | Callback\<[TCPSocketConnection](#tcpsocketconnection10)\> | Yes  | Callback used to return the result. If the operation fails, an error message is returned.      |
3028e41f4b71Sopenharmony_ci
3029e41f4b71Sopenharmony_ci**Error codes**
3030e41f4b71Sopenharmony_ci
3031e41f4b71Sopenharmony_ci| ID| Error Message        |
3032e41f4b71Sopenharmony_ci| -------- | ---------------- |
3033e41f4b71Sopenharmony_ci| 401      | Parameter error. |
3034e41f4b71Sopenharmony_ci
3035e41f4b71Sopenharmony_ci**Example**
3036e41f4b71Sopenharmony_ci
3037e41f4b71Sopenharmony_ci```ts
3038e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
3039e41f4b71Sopenharmony_ci
3040e41f4b71Sopenharmony_cilet tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance();
3041e41f4b71Sopenharmony_ci
3042e41f4b71Sopenharmony_cilet listenAddr: socket.NetAddress = {
3043e41f4b71Sopenharmony_ci  address:  '192.168.xx.xxx',
3044e41f4b71Sopenharmony_ci  port: 8080,
3045e41f4b71Sopenharmony_ci  family: 1
3046e41f4b71Sopenharmony_ci}
3047e41f4b71Sopenharmony_citcpServer.listen(listenAddr, (err: BusinessError) => {
3048e41f4b71Sopenharmony_ci  if (err) {
3049e41f4b71Sopenharmony_ci    console.log("listen fail");
3050e41f4b71Sopenharmony_ci    return;
3051e41f4b71Sopenharmony_ci  }
3052e41f4b71Sopenharmony_ci  console.log("listen success");
3053e41f4b71Sopenharmony_ci  tcpServer.on('connect', (data: socket.TCPSocketConnection) => {
3054e41f4b71Sopenharmony_ci    console.log(JSON.stringify(data))
3055e41f4b71Sopenharmony_ci  });
3056e41f4b71Sopenharmony_ci})
3057e41f4b71Sopenharmony_ci```
3058e41f4b71Sopenharmony_ci
3059e41f4b71Sopenharmony_ci### off('connect')<sup>10+</sup>
3060e41f4b71Sopenharmony_ci
3061e41f4b71Sopenharmony_cioff(type: 'connect', callback?: Callback\<TCPSocketConnection\>): void
3062e41f4b71Sopenharmony_ci
3063e41f4b71Sopenharmony_ciUnsubscribes from **connect** events of a **TCPSocketServer** object. This API uses an asynchronous callback to return the result.
3064e41f4b71Sopenharmony_ci
3065e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
3066e41f4b71Sopenharmony_ci
3067e41f4b71Sopenharmony_ci**Parameters**
3068e41f4b71Sopenharmony_ci
3069e41f4b71Sopenharmony_ci| Name  | Type                           | Mandatory| Description                                 |
3070e41f4b71Sopenharmony_ci| -------- | ------------------------------- | ---- | ------------------------------------- |
3071e41f4b71Sopenharmony_ci| type     | string                          | Yes  | Event type.<br/> **connect**: connection event.|
3072e41f4b71Sopenharmony_ci| callback | Callback\<[TCPSocketConnection](#tcpsocketconnection10)\> | No  | Callback used to return the result. If the operation fails, an error message is returned.|
3073e41f4b71Sopenharmony_ci
3074e41f4b71Sopenharmony_ci**Error codes**
3075e41f4b71Sopenharmony_ci
3076e41f4b71Sopenharmony_ci| ID| Error Message        |
3077e41f4b71Sopenharmony_ci| -------- | ---------------- |
3078e41f4b71Sopenharmony_ci| 401      | Parameter error. |
3079e41f4b71Sopenharmony_ci
3080e41f4b71Sopenharmony_ci**Example**
3081e41f4b71Sopenharmony_ci
3082e41f4b71Sopenharmony_ci```ts
3083e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
3084e41f4b71Sopenharmony_ci
3085e41f4b71Sopenharmony_cilet tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance();
3086e41f4b71Sopenharmony_ci
3087e41f4b71Sopenharmony_cilet listenAddr: socket.NetAddress = {
3088e41f4b71Sopenharmony_ci  address:  '192.168.xx.xxx',
3089e41f4b71Sopenharmony_ci  port: 8080,
3090e41f4b71Sopenharmony_ci  family: 1
3091e41f4b71Sopenharmony_ci}
3092e41f4b71Sopenharmony_citcpServer.listen(listenAddr, (err: BusinessError) => {
3093e41f4b71Sopenharmony_ci  if (err) {
3094e41f4b71Sopenharmony_ci    console.log("listen fail");
3095e41f4b71Sopenharmony_ci    return;
3096e41f4b71Sopenharmony_ci  }
3097e41f4b71Sopenharmony_ci  console.log("listen success");
3098e41f4b71Sopenharmony_ci  let callback = (data: socket.TCPSocketConnection) => {
3099e41f4b71Sopenharmony_ci    console.log('on connect message: ' + JSON.stringify(data));
3100e41f4b71Sopenharmony_ci  }
3101e41f4b71Sopenharmony_ci  tcpServer.on('connect', callback);
3102e41f4b71Sopenharmony_ci  // You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
3103e41f4b71Sopenharmony_ci  tcpServer.off('connect', callback);
3104e41f4b71Sopenharmony_ci  tcpServer.off('connect');
3105e41f4b71Sopenharmony_ci})
3106e41f4b71Sopenharmony_ci```
3107e41f4b71Sopenharmony_ci
3108e41f4b71Sopenharmony_ci### on('error')<sup>10+</sup>
3109e41f4b71Sopenharmony_ci
3110e41f4b71Sopenharmony_cion(type: 'error', callback: ErrorCallback): void
3111e41f4b71Sopenharmony_ci
3112e41f4b71Sopenharmony_ciSubscribes to **error** events of a **TCPSocketServer** object. This API uses an asynchronous callback to return the result.
3113e41f4b71Sopenharmony_ci
3114e41f4b71Sopenharmony_ci> **NOTE**
3115e41f4b71Sopenharmony_ci> This API can be called only after **listen** is successfully called.
3116e41f4b71Sopenharmony_ci
3117e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
3118e41f4b71Sopenharmony_ci
3119e41f4b71Sopenharmony_ci**Parameters**
3120e41f4b71Sopenharmony_ci
3121e41f4b71Sopenharmony_ci| Name  | Type         | Mandatory| Description                                |
3122e41f4b71Sopenharmony_ci| -------- | ------------- | ---- | ------------------------------------ |
3123e41f4b71Sopenharmony_ci| type     | string        | Yes  | Event type.<br/> **error**: error event.|
3124e41f4b71Sopenharmony_ci| callback | ErrorCallback | Yes  | Callback used to return the result. If the operation fails, an error message is returned.|
3125e41f4b71Sopenharmony_ci
3126e41f4b71Sopenharmony_ci**Error codes**
3127e41f4b71Sopenharmony_ci
3128e41f4b71Sopenharmony_ci| ID| Error Message        |
3129e41f4b71Sopenharmony_ci| -------- | ---------------- |
3130e41f4b71Sopenharmony_ci| 401      | Parameter error. |
3131e41f4b71Sopenharmony_ci
3132e41f4b71Sopenharmony_ci**Example**
3133e41f4b71Sopenharmony_ci
3134e41f4b71Sopenharmony_ci```ts
3135e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
3136e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
3137e41f4b71Sopenharmony_ci
3138e41f4b71Sopenharmony_cilet tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance();
3139e41f4b71Sopenharmony_ci
3140e41f4b71Sopenharmony_cilet listenAddr: socket.NetAddress = {
3141e41f4b71Sopenharmony_ci  address:  '192.168.xx.xxx',
3142e41f4b71Sopenharmony_ci  port: 8080,
3143e41f4b71Sopenharmony_ci  family: 1
3144e41f4b71Sopenharmony_ci}
3145e41f4b71Sopenharmony_citcpServer.listen(listenAddr, (err: BusinessError) => {
3146e41f4b71Sopenharmony_ci  if (err) {
3147e41f4b71Sopenharmony_ci    console.log("listen fail");
3148e41f4b71Sopenharmony_ci    return;
3149e41f4b71Sopenharmony_ci  }
3150e41f4b71Sopenharmony_ci  console.log("listen success");
3151e41f4b71Sopenharmony_ci  tcpServer.on('error', (err: BusinessError) => {
3152e41f4b71Sopenharmony_ci    console.log("on error, err:" + JSON.stringify(err))
3153e41f4b71Sopenharmony_ci  });
3154e41f4b71Sopenharmony_ci})
3155e41f4b71Sopenharmony_ci```
3156e41f4b71Sopenharmony_ci
3157e41f4b71Sopenharmony_ci### off('error')<sup>10+</sup>
3158e41f4b71Sopenharmony_ci
3159e41f4b71Sopenharmony_cioff(type: 'error', callback?: ErrorCallback): void
3160e41f4b71Sopenharmony_ci
3161e41f4b71Sopenharmony_ciUnsubscribes from **error** events of a **TCPSocketServer** object. This API uses an asynchronous callback to return the result.
3162e41f4b71Sopenharmony_ci
3163e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
3164e41f4b71Sopenharmony_ci
3165e41f4b71Sopenharmony_ci**Parameters**
3166e41f4b71Sopenharmony_ci
3167e41f4b71Sopenharmony_ci| Name  | Type         | Mandatory| Description                                |
3168e41f4b71Sopenharmony_ci| -------- | ------------- | ---- | ------------------------------------ |
3169e41f4b71Sopenharmony_ci| type     | string        | Yes  | Event type.<br/> **error**: error event.|
3170e41f4b71Sopenharmony_ci| callback | ErrorCallback | No  | Callback used to return the result. If the operation fails, an error message is returned.                          |
3171e41f4b71Sopenharmony_ci
3172e41f4b71Sopenharmony_ci**Error codes**
3173e41f4b71Sopenharmony_ci
3174e41f4b71Sopenharmony_ci| ID| Error Message        |
3175e41f4b71Sopenharmony_ci| -------- | ---------------- |
3176e41f4b71Sopenharmony_ci| 401      | Parameter error. |
3177e41f4b71Sopenharmony_ci
3178e41f4b71Sopenharmony_ci**Example**
3179e41f4b71Sopenharmony_ci
3180e41f4b71Sopenharmony_ci```ts
3181e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
3182e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
3183e41f4b71Sopenharmony_ci
3184e41f4b71Sopenharmony_cilet tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance();
3185e41f4b71Sopenharmony_ci
3186e41f4b71Sopenharmony_cilet listenAddr: socket.NetAddress = {
3187e41f4b71Sopenharmony_ci  address:  '192.168.xx.xxx',
3188e41f4b71Sopenharmony_ci  port: 8080,
3189e41f4b71Sopenharmony_ci  family: 1
3190e41f4b71Sopenharmony_ci}
3191e41f4b71Sopenharmony_citcpServer.listen(listenAddr, (err: BusinessError) => {
3192e41f4b71Sopenharmony_ci  if (err) {
3193e41f4b71Sopenharmony_ci    console.log("listen fail");
3194e41f4b71Sopenharmony_ci    return;
3195e41f4b71Sopenharmony_ci  }
3196e41f4b71Sopenharmony_ci  console.log("listen success");
3197e41f4b71Sopenharmony_ci  let callback = (err: BusinessError) => {
3198e41f4b71Sopenharmony_ci    console.log("on error, err:" + JSON.stringify(err));
3199e41f4b71Sopenharmony_ci  }
3200e41f4b71Sopenharmony_ci  tcpServer.on('error', callback);
3201e41f4b71Sopenharmony_ci  // You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
3202e41f4b71Sopenharmony_ci  tcpServer.off('error', callback);
3203e41f4b71Sopenharmony_ci  tcpServer.off('error');
3204e41f4b71Sopenharmony_ci})
3205e41f4b71Sopenharmony_ci```
3206e41f4b71Sopenharmony_ci
3207e41f4b71Sopenharmony_ci## TCPSocketConnection<sup>10+</sup>
3208e41f4b71Sopenharmony_ci
3209e41f4b71Sopenharmony_ciDefines a **TCPSocketConnection** object, that is, the connection between the TCPSocket client and the server. Before calling TCPSocketConnection APIs, you need to obtain a **TCPSocketConnection** object.
3210e41f4b71Sopenharmony_ci
3211e41f4b71Sopenharmony_ci> **NOTE**
3212e41f4b71Sopenharmony_ci> The TCPSocket client can call related APIs through the **TCPSocketConnection** object only after a connection is successfully established between the TCPSocket client and the server.
3213e41f4b71Sopenharmony_ci
3214e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
3215e41f4b71Sopenharmony_ci
3216e41f4b71Sopenharmony_ci### Attributes
3217e41f4b71Sopenharmony_ci
3218e41f4b71Sopenharmony_ci| Name    | Type  | Mandatory| Description                                     |
3219e41f4b71Sopenharmony_ci| -------- | ------ | ---- | ----------------------------------------- |
3220e41f4b71Sopenharmony_ci| clientId | number | Yes  | ID of the connection between the client and TCPSocketServer.|
3221e41f4b71Sopenharmony_ci
3222e41f4b71Sopenharmony_ci### send<sup>10+</sup>
3223e41f4b71Sopenharmony_ci
3224e41f4b71Sopenharmony_cisend(options: TCPSendOptions, callback: AsyncCallback\<void\>): void
3225e41f4b71Sopenharmony_ci
3226e41f4b71Sopenharmony_ciSends data over a **TCPSocketConnection** object. This API uses an asynchronous callback to return the result.
3227e41f4b71Sopenharmony_ci
3228e41f4b71Sopenharmony_ci> **NOTE**
3229e41f4b71Sopenharmony_ci> This API can be called only after a connection with the client is set up.
3230e41f4b71Sopenharmony_ci
3231e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
3232e41f4b71Sopenharmony_ci
3233e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
3234e41f4b71Sopenharmony_ci
3235e41f4b71Sopenharmony_ci**Parameters**
3236e41f4b71Sopenharmony_ci
3237e41f4b71Sopenharmony_ci| Name  | Type                             | Mandatory| Description                                                        |
3238e41f4b71Sopenharmony_ci| -------- | --------------------------------- | ---- | ------------------------------------------------------------ |
3239e41f4b71Sopenharmony_ci| options  | [TCPSendOptions](#tcpsendoptions) | Yes  | Defines the parameters for sending data over a TCP socket connection.|
3240e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void\>             | Yes  | Callback used to return the result. If the operation fails, an error message is returned.            |
3241e41f4b71Sopenharmony_ci
3242e41f4b71Sopenharmony_ci**Error codes**
3243e41f4b71Sopenharmony_ci
3244e41f4b71Sopenharmony_ci| ID| Error Message              |
3245e41f4b71Sopenharmony_ci| -------- | ---------------------- |
3246e41f4b71Sopenharmony_ci| 401      | Parameter error.       |
3247e41f4b71Sopenharmony_ci| 201      | Permission denied.     |
3248e41f4b71Sopenharmony_ci| 2300002  | System internal error. |
3249e41f4b71Sopenharmony_ci
3250e41f4b71Sopenharmony_ci**Example**
3251e41f4b71Sopenharmony_ci
3252e41f4b71Sopenharmony_ci```ts
3253e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
3254e41f4b71Sopenharmony_ci
3255e41f4b71Sopenharmony_cilet tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance();
3256e41f4b71Sopenharmony_ci
3257e41f4b71Sopenharmony_citcpServer.on('connect', (client: socket.TCPSocketConnection) => {
3258e41f4b71Sopenharmony_ci  let tcpSendOption: socket.TCPSendOptions = {
3259e41f4b71Sopenharmony_ci    data: 'Hello, client!'
3260e41f4b71Sopenharmony_ci  }
3261e41f4b71Sopenharmony_ci  client.send(tcpSendOption, () => {
3262e41f4b71Sopenharmony_ci    console.log('send success');
3263e41f4b71Sopenharmony_ci  });
3264e41f4b71Sopenharmony_ci});
3265e41f4b71Sopenharmony_ci```
3266e41f4b71Sopenharmony_ci
3267e41f4b71Sopenharmony_ci### send<sup>10+</sup>
3268e41f4b71Sopenharmony_ci
3269e41f4b71Sopenharmony_cisend(options: TCPSendOptions): Promise\<void\>
3270e41f4b71Sopenharmony_ci
3271e41f4b71Sopenharmony_ciSends data over a **TCPSocketConnection** object. This API uses a promise to return the result.
3272e41f4b71Sopenharmony_ci
3273e41f4b71Sopenharmony_ci> **NOTE**
3274e41f4b71Sopenharmony_ci> This API can be called only after a connection with the client is set up.
3275e41f4b71Sopenharmony_ci
3276e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
3277e41f4b71Sopenharmony_ci
3278e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
3279e41f4b71Sopenharmony_ci
3280e41f4b71Sopenharmony_ci**Parameters**
3281e41f4b71Sopenharmony_ci
3282e41f4b71Sopenharmony_ci| Name | Type                             | Mandatory| Description                                                        |
3283e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | ------------------------------------------------------------ |
3284e41f4b71Sopenharmony_ci| options | [TCPSendOptions](#tcpsendoptions) | Yes  | Defines the parameters for sending data over a TCP socket connection.|
3285e41f4b71Sopenharmony_ci
3286e41f4b71Sopenharmony_ci**Return value**
3287e41f4b71Sopenharmony_ci
3288e41f4b71Sopenharmony_ci| Type           | Description                                                        |
3289e41f4b71Sopenharmony_ci|  -------------- |  ----------------------------------------------------------- |
3290e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
3291e41f4b71Sopenharmony_ci
3292e41f4b71Sopenharmony_ci**Error codes**
3293e41f4b71Sopenharmony_ci
3294e41f4b71Sopenharmony_ci| ID| Error Message              |
3295e41f4b71Sopenharmony_ci| -------- | ---------------------- |
3296e41f4b71Sopenharmony_ci| 401      | Parameter error.       |
3297e41f4b71Sopenharmony_ci| 201      | Permission denied.     |
3298e41f4b71Sopenharmony_ci| 2300002  | System internal error. |
3299e41f4b71Sopenharmony_ci
3300e41f4b71Sopenharmony_ci**Example**
3301e41f4b71Sopenharmony_ci
3302e41f4b71Sopenharmony_ci```ts
3303e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
3304e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
3305e41f4b71Sopenharmony_ci
3306e41f4b71Sopenharmony_cilet tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance();
3307e41f4b71Sopenharmony_ci
3308e41f4b71Sopenharmony_citcpServer.on('connect', (client: socket.TCPSocketConnection) => {
3309e41f4b71Sopenharmony_ci  let tcpSendOption: socket.TCPSendOptions = {
3310e41f4b71Sopenharmony_ci    data: 'Hello, client!'
3311e41f4b71Sopenharmony_ci  }
3312e41f4b71Sopenharmony_ci  client.send(tcpSendOption).then(() => {
3313e41f4b71Sopenharmony_ci    console.log('send success');
3314e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
3315e41f4b71Sopenharmony_ci    console.log('send fail');
3316e41f4b71Sopenharmony_ci  });
3317e41f4b71Sopenharmony_ci});
3318e41f4b71Sopenharmony_ci```
3319e41f4b71Sopenharmony_ci
3320e41f4b71Sopenharmony_ci### close<sup>10+</sup>
3321e41f4b71Sopenharmony_ci
3322e41f4b71Sopenharmony_ciclose(callback: AsyncCallback\<void\>): void
3323e41f4b71Sopenharmony_ci
3324e41f4b71Sopenharmony_ciCloses a TCP socket connection. This API uses an asynchronous callback to return the result.
3325e41f4b71Sopenharmony_ci
3326e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
3327e41f4b71Sopenharmony_ci
3328e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
3329e41f4b71Sopenharmony_ci
3330e41f4b71Sopenharmony_ci**Parameters**
3331e41f4b71Sopenharmony_ci
3332e41f4b71Sopenharmony_ci| Name  | Type                 | Mandatory| Description      |
3333e41f4b71Sopenharmony_ci| -------- | --------------------- | ---- | ---------- |
3334e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void\> | Yes  | Callback used to return the result. If the operation fails, an error message is returned.|
3335e41f4b71Sopenharmony_ci
3336e41f4b71Sopenharmony_ci**Error codes**
3337e41f4b71Sopenharmony_ci
3338e41f4b71Sopenharmony_ci| ID| Error Message              |
3339e41f4b71Sopenharmony_ci| -------- | ---------------------- |
3340e41f4b71Sopenharmony_ci| 401      | Parameter error.       |
3341e41f4b71Sopenharmony_ci| 201      | Permission denied.     |
3342e41f4b71Sopenharmony_ci| 2300002  | System internal error. |
3343e41f4b71Sopenharmony_ci
3344e41f4b71Sopenharmony_ci**Example**
3345e41f4b71Sopenharmony_ci
3346e41f4b71Sopenharmony_ci```ts
3347e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
3348e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
3349e41f4b71Sopenharmony_ci
3350e41f4b71Sopenharmony_cilet tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance();
3351e41f4b71Sopenharmony_ci
3352e41f4b71Sopenharmony_citcpServer.on('connect', (client: socket.TCPSocketConnection) => {
3353e41f4b71Sopenharmony_ci  client.close((err: BusinessError) => {
3354e41f4b71Sopenharmony_ci    if (err) {
3355e41f4b71Sopenharmony_ci      console.log('close fail');
3356e41f4b71Sopenharmony_ci      return;
3357e41f4b71Sopenharmony_ci    }
3358e41f4b71Sopenharmony_ci    console.log('close success');
3359e41f4b71Sopenharmony_ci  });
3360e41f4b71Sopenharmony_ci});
3361e41f4b71Sopenharmony_ci```
3362e41f4b71Sopenharmony_ci
3363e41f4b71Sopenharmony_ci### close<sup>10+</sup>
3364e41f4b71Sopenharmony_ci
3365e41f4b71Sopenharmony_ciclose(): Promise\<void\>
3366e41f4b71Sopenharmony_ci
3367e41f4b71Sopenharmony_ciCloses a TCP socket connection. This API uses a promise to return the result.
3368e41f4b71Sopenharmony_ci
3369e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
3370e41f4b71Sopenharmony_ci
3371e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
3372e41f4b71Sopenharmony_ci
3373e41f4b71Sopenharmony_ci**Return value**
3374e41f4b71Sopenharmony_ci
3375e41f4b71Sopenharmony_ci| Type           | Description                                        |
3376e41f4b71Sopenharmony_ci|  -------------- |  ------------------------------------------- |
3377e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
3378e41f4b71Sopenharmony_ci
3379e41f4b71Sopenharmony_ci**Error codes**
3380e41f4b71Sopenharmony_ci
3381e41f4b71Sopenharmony_ci| ID| Error Message              |
3382e41f4b71Sopenharmony_ci| -------- | ---------------------- |
3383e41f4b71Sopenharmony_ci| 201      | Permission denied.     |
3384e41f4b71Sopenharmony_ci| 2300002  | System internal error. |
3385e41f4b71Sopenharmony_ci
3386e41f4b71Sopenharmony_ci**Example**
3387e41f4b71Sopenharmony_ci
3388e41f4b71Sopenharmony_ci```ts
3389e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
3390e41f4b71Sopenharmony_ci
3391e41f4b71Sopenharmony_cilet tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance();
3392e41f4b71Sopenharmony_citcpServer.on('connect', (client: socket.TCPSocketConnection) => {
3393e41f4b71Sopenharmony_ci  client.close().then(() => {
3394e41f4b71Sopenharmony_ci  	console.log('close success');
3395e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
3396e41f4b71Sopenharmony_ci  	console.log('close fail');
3397e41f4b71Sopenharmony_ci  });
3398e41f4b71Sopenharmony_ci});
3399e41f4b71Sopenharmony_ci```
3400e41f4b71Sopenharmony_ci
3401e41f4b71Sopenharmony_ci### getRemoteAddress<sup>10+</sup>
3402e41f4b71Sopenharmony_ci
3403e41f4b71Sopenharmony_cigetRemoteAddress(callback: AsyncCallback\<NetAddress\>): void
3404e41f4b71Sopenharmony_ci
3405e41f4b71Sopenharmony_ciObtains the remote address of a socket connection. This API uses an asynchronous callback to return the result.
3406e41f4b71Sopenharmony_ci
3407e41f4b71Sopenharmony_ci> **NOTE**
3408e41f4b71Sopenharmony_ci> This API can be called only after a connection with the client is set up.
3409e41f4b71Sopenharmony_ci
3410e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
3411e41f4b71Sopenharmony_ci
3412e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
3413e41f4b71Sopenharmony_ci
3414e41f4b71Sopenharmony_ci**Parameters**
3415e41f4b71Sopenharmony_ci
3416e41f4b71Sopenharmony_ci| Name  | Type                                    | Mandatory| Description      |
3417e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ---------- |
3418e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[NetAddress](#netaddress)\> | Yes  | Callback used to return the result. If the operation fails, an error message is returned.|
3419e41f4b71Sopenharmony_ci
3420e41f4b71Sopenharmony_ci**Error codes**
3421e41f4b71Sopenharmony_ci
3422e41f4b71Sopenharmony_ci| ID| Error Message                       |
3423e41f4b71Sopenharmony_ci| -------- | ------------------------------- |
3424e41f4b71Sopenharmony_ci| 401      | Parameter error.                |
3425e41f4b71Sopenharmony_ci| 201      | Permission denied.              |
3426e41f4b71Sopenharmony_ci| 2300002  | System internal error.          |
3427e41f4b71Sopenharmony_ci| 2303188  | Socket operation on non-socket. |
3428e41f4b71Sopenharmony_ci
3429e41f4b71Sopenharmony_ci**Example**
3430e41f4b71Sopenharmony_ci
3431e41f4b71Sopenharmony_ci```ts
3432e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
3433e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
3434e41f4b71Sopenharmony_ci
3435e41f4b71Sopenharmony_cilet tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance();
3436e41f4b71Sopenharmony_citcpServer.on('connect', (client: socket.TCPSocketConnection) => {
3437e41f4b71Sopenharmony_ci  client.getRemoteAddress((err: BusinessError, data: socket.NetAddress) => {
3438e41f4b71Sopenharmony_ci    if (err) {
3439e41f4b71Sopenharmony_ci      console.log('getRemoteAddress fail');
3440e41f4b71Sopenharmony_ci      return;
3441e41f4b71Sopenharmony_ci    }
3442e41f4b71Sopenharmony_ci    console.log('getRemoteAddress success:' + JSON.stringify(data));
3443e41f4b71Sopenharmony_ci  });
3444e41f4b71Sopenharmony_ci});
3445e41f4b71Sopenharmony_ci```
3446e41f4b71Sopenharmony_ci
3447e41f4b71Sopenharmony_ci### getRemoteAddress<sup>10+</sup>
3448e41f4b71Sopenharmony_ci
3449e41f4b71Sopenharmony_cigetRemoteAddress(): Promise\<NetAddress\>
3450e41f4b71Sopenharmony_ci
3451e41f4b71Sopenharmony_ciObtains the remote address of a socket connection. This API uses a promise to return the result.
3452e41f4b71Sopenharmony_ci
3453e41f4b71Sopenharmony_ci> **NOTE**
3454e41f4b71Sopenharmony_ci> This API can be called only after a connection with the client is set up.
3455e41f4b71Sopenharmony_ci
3456e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
3457e41f4b71Sopenharmony_ci
3458e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
3459e41f4b71Sopenharmony_ci
3460e41f4b71Sopenharmony_ci**Return value**
3461e41f4b71Sopenharmony_ci
3462e41f4b71Sopenharmony_ci| Type                              | Description                                       |
3463e41f4b71Sopenharmony_ci|  --------------------------------- |  ------------------------------------------ |
3464e41f4b71Sopenharmony_ci| Promise\<[NetAddress](#netaddress)\> | Promise used to return the result.|
3465e41f4b71Sopenharmony_ci
3466e41f4b71Sopenharmony_ci**Error codes**
3467e41f4b71Sopenharmony_ci
3468e41f4b71Sopenharmony_ci| ID| Error Message                       |
3469e41f4b71Sopenharmony_ci| -------- | ------------------------------- |
3470e41f4b71Sopenharmony_ci| 201      | Permission denied.              |
3471e41f4b71Sopenharmony_ci| 2300002  | System internal error.          |
3472e41f4b71Sopenharmony_ci| 2303188  | Socket operation on non-socket. |
3473e41f4b71Sopenharmony_ci
3474e41f4b71Sopenharmony_ci**Example**
3475e41f4b71Sopenharmony_ci
3476e41f4b71Sopenharmony_ci```ts
3477e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
3478e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
3479e41f4b71Sopenharmony_ci
3480e41f4b71Sopenharmony_cilet tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance();
3481e41f4b71Sopenharmony_citcpServer.on('connect', (client: socket.TCPSocketConnection) => {
3482e41f4b71Sopenharmony_ci  client.getRemoteAddress().then(() => {
3483e41f4b71Sopenharmony_ci    console.log('getRemoteAddress success');
3484e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
3485e41f4b71Sopenharmony_ci    console.log('getRemoteAddress fail');
3486e41f4b71Sopenharmony_ci  });
3487e41f4b71Sopenharmony_ci});
3488e41f4b71Sopenharmony_ci```
3489e41f4b71Sopenharmony_ci
3490e41f4b71Sopenharmony_ci### getLocalAddress<sup>12+</sup>
3491e41f4b71Sopenharmony_ci
3492e41f4b71Sopenharmony_cigetLocalAddress(): Promise\<NetAddress\>
3493e41f4b71Sopenharmony_ci
3494e41f4b71Sopenharmony_ciObtains the local socket address of a **TCPSocketConnection** connection. This API uses a promise to return the result.
3495e41f4b71Sopenharmony_ci
3496e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
3497e41f4b71Sopenharmony_ci
3498e41f4b71Sopenharmony_ci**Return value**
3499e41f4b71Sopenharmony_ci
3500e41f4b71Sopenharmony_ci| Type           | Description                                                |
3501e41f4b71Sopenharmony_ci|  -------------- |  --------------------------------------------------- |
3502e41f4b71Sopenharmony_ci| Promise\<[NetAddress](#netaddress)\> | Promise used to return the result.|
3503e41f4b71Sopenharmony_ci
3504e41f4b71Sopenharmony_ci**Error codes**
3505e41f4b71Sopenharmony_ci
3506e41f4b71Sopenharmony_ci| ID| Error Message                                   |
3507e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- |
3508e41f4b71Sopenharmony_ci| 2300002  | System internal error.                      |
3509e41f4b71Sopenharmony_ci| 2301009  | Bad file descriptor.                            |
3510e41f4b71Sopenharmony_ci| 2303188  | Socket operation on non-socket. |
3511e41f4b71Sopenharmony_ci
3512e41f4b71Sopenharmony_ci**Example**
3513e41f4b71Sopenharmony_ci
3514e41f4b71Sopenharmony_ci```ts
3515e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
3516e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
3517e41f4b71Sopenharmony_ci
3518e41f4b71Sopenharmony_cilet tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance();
3519e41f4b71Sopenharmony_cilet listenAddr: socket.NetAddress = {
3520e41f4b71Sopenharmony_ci  address: "192.168.xx.xx",
3521e41f4b71Sopenharmony_ci  port: 8080,
3522e41f4b71Sopenharmony_ci  family: 1
3523e41f4b71Sopenharmony_ci}
3524e41f4b71Sopenharmony_citcpServer.listen(listenAddr, (err: BusinessError) => {
3525e41f4b71Sopenharmony_ci  let tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
3526e41f4b71Sopenharmony_ci  let netAddress: socket.NetAddress = {
3527e41f4b71Sopenharmony_ci    address: "192.168.xx.xx",
3528e41f4b71Sopenharmony_ci    port: 8080
3529e41f4b71Sopenharmony_ci  }
3530e41f4b71Sopenharmony_ci  let options: socket.TCPConnectOptions = {
3531e41f4b71Sopenharmony_ci    address: netAddress,
3532e41f4b71Sopenharmony_ci    timeout: 6000
3533e41f4b71Sopenharmony_ci  }
3534e41f4b71Sopenharmony_ci  tcp.connect(options, (err: BusinessError) => {
3535e41f4b71Sopenharmony_ci    if (err) {
3536e41f4b71Sopenharmony_ci      console.error('connect fail');
3537e41f4b71Sopenharmony_ci      return;
3538e41f4b71Sopenharmony_ci    }
3539e41f4b71Sopenharmony_ci    console.info('connect success!');
3540e41f4b71Sopenharmony_ci  })
3541e41f4b71Sopenharmony_ci  tcpServer.on('connect', (client: socket.TCPSocketConnection) => {
3542e41f4b71Sopenharmony_ci    client.getLocalAddress().then((localAddress: socket.NetAddress) => {
3543e41f4b71Sopenharmony_ci      console.info("Family IP Port: " + JSON.stringify(localAddress));
3544e41f4b71Sopenharmony_ci    }).catch((err: BusinessError) => {
3545e41f4b71Sopenharmony_ci      console.error('Error:' + JSON.stringify(err));
3546e41f4b71Sopenharmony_ci    });
3547e41f4b71Sopenharmony_ci  })
3548e41f4b71Sopenharmony_ci})
3549e41f4b71Sopenharmony_ci```
3550e41f4b71Sopenharmony_ci
3551e41f4b71Sopenharmony_ci### on('message')<sup>10+</sup>
3552e41f4b71Sopenharmony_ci
3553e41f4b71Sopenharmony_cion(type: 'message', callback: Callback<SocketMessageInfo\>): void
3554e41f4b71Sopenharmony_ci
3555e41f4b71Sopenharmony_ciSubscribes to **message** events of a **TCPSocketConnection** object. This API uses an asynchronous callback to return the result.
3556e41f4b71Sopenharmony_ci
3557e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
3558e41f4b71Sopenharmony_ci
3559e41f4b71Sopenharmony_ci**Parameters**
3560e41f4b71Sopenharmony_ci
3561e41f4b71Sopenharmony_ci| Name  | Type                                                        | Mandatory| Description                                     |
3562e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------- |
3563e41f4b71Sopenharmony_ci| type     | string                                                       | Yes  | Event type.<br/> **message**: message receiving event.|
3564e41f4b71Sopenharmony_ci| callback | Callback\<[SocketMessageInfo](#socketmessageinfo11)\> | Yes  | Callback used to return the result. If the operation fails, an error message is returned.        |
3565e41f4b71Sopenharmony_ci
3566e41f4b71Sopenharmony_ci**Error codes**
3567e41f4b71Sopenharmony_ci
3568e41f4b71Sopenharmony_ci| ID| Error Message        |
3569e41f4b71Sopenharmony_ci| -------- | ---------------- |
3570e41f4b71Sopenharmony_ci| 401      | Parameter error. |
3571e41f4b71Sopenharmony_ci
3572e41f4b71Sopenharmony_ci**Example**
3573e41f4b71Sopenharmony_ci
3574e41f4b71Sopenharmony_ci```ts
3575e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
3576e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
3577e41f4b71Sopenharmony_ci
3578e41f4b71Sopenharmony_cilet tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance();
3579e41f4b71Sopenharmony_ci
3580e41f4b71Sopenharmony_citcpServer.on('connect', (client: socket.TCPSocketConnection) => {
3581e41f4b71Sopenharmony_ci  client.on('message', (value: socket.SocketMessageInfo) => {
3582e41f4b71Sopenharmony_ci    let messageView = '';
3583e41f4b71Sopenharmony_ci    for (let i: number = 0; i < value.message.byteLength; i++) {
3584e41f4b71Sopenharmony_ci      let uint8Array = new Uint8Array(value.message) 
3585e41f4b71Sopenharmony_ci      let messages = uint8Array[i]
3586e41f4b71Sopenharmony_ci      let message = String.fromCharCode(messages);
3587e41f4b71Sopenharmony_ci      messageView += message;
3588e41f4b71Sopenharmony_ci    }
3589e41f4b71Sopenharmony_ci    console.log('on message message: ' + JSON.stringify(messageView));
3590e41f4b71Sopenharmony_ci    console.log('remoteInfo: ' + JSON.stringify(value.remoteInfo));
3591e41f4b71Sopenharmony_ci  });
3592e41f4b71Sopenharmony_ci});
3593e41f4b71Sopenharmony_ci```
3594e41f4b71Sopenharmony_ci
3595e41f4b71Sopenharmony_ci### off('message')<sup>10+</sup>
3596e41f4b71Sopenharmony_ci
3597e41f4b71Sopenharmony_cioff(type: 'message', callback?: Callback<SocketMessageInfo\>): void
3598e41f4b71Sopenharmony_ci
3599e41f4b71Sopenharmony_ciUnsubscribes from **message** events of a **TCPSocketConnection** object. This API uses an asynchronous callback to return the result.
3600e41f4b71Sopenharmony_ci
3601e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
3602e41f4b71Sopenharmony_ci
3603e41f4b71Sopenharmony_ci**Parameters**
3604e41f4b71Sopenharmony_ci
3605e41f4b71Sopenharmony_ci| Name  | Type                                                        | Mandatory| Description                                     |
3606e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------- |
3607e41f4b71Sopenharmony_ci| type     | string                                                       | Yes  | Event type.<br/> **message**: message receiving event.|
3608e41f4b71Sopenharmony_ci| callback | Callback\<[SocketMessageInfo](#socketmessageinfo11)\> | No  | Callback used to return the result. If the operation fails, an error message is returned.       |
3609e41f4b71Sopenharmony_ci
3610e41f4b71Sopenharmony_ci**Error codes**
3611e41f4b71Sopenharmony_ci
3612e41f4b71Sopenharmony_ci| ID| Error Message        |
3613e41f4b71Sopenharmony_ci| -------- | ---------------- |
3614e41f4b71Sopenharmony_ci| 401      | Parameter error. |
3615e41f4b71Sopenharmony_ci
3616e41f4b71Sopenharmony_ci**Example**
3617e41f4b71Sopenharmony_ci
3618e41f4b71Sopenharmony_ci```ts
3619e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
3620e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
3621e41f4b71Sopenharmony_ci
3622e41f4b71Sopenharmony_cilet tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance();
3623e41f4b71Sopenharmony_cilet callback = (value: socket.SocketMessageInfo) => {
3624e41f4b71Sopenharmony_ci  let messageView = '';
3625e41f4b71Sopenharmony_ci  for (let i: number = 0; i < value.message.byteLength; i++) {
3626e41f4b71Sopenharmony_ci    let uint8Array = new Uint8Array(value.message) 
3627e41f4b71Sopenharmony_ci    let messages = uint8Array[i]
3628e41f4b71Sopenharmony_ci    let message = String.fromCharCode(messages);
3629e41f4b71Sopenharmony_ci    messageView += message;
3630e41f4b71Sopenharmony_ci  }
3631e41f4b71Sopenharmony_ci  console.log('on message message: ' + JSON.stringify(messageView));
3632e41f4b71Sopenharmony_ci  console.log('remoteInfo: ' + JSON.stringify(value.remoteInfo));
3633e41f4b71Sopenharmony_ci}
3634e41f4b71Sopenharmony_citcpServer.on('connect', (client: socket.TCPSocketConnection) => {
3635e41f4b71Sopenharmony_ci  client.on('message', callback);
3636e41f4b71Sopenharmony_ci  // You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
3637e41f4b71Sopenharmony_ci  client.off('message', callback);
3638e41f4b71Sopenharmony_ci  client.off('message');
3639e41f4b71Sopenharmony_ci});
3640e41f4b71Sopenharmony_ci```
3641e41f4b71Sopenharmony_ci
3642e41f4b71Sopenharmony_ci### on('close')<sup>10+</sup>
3643e41f4b71Sopenharmony_ci
3644e41f4b71Sopenharmony_cion(type: 'close', callback: Callback\<void\>): void
3645e41f4b71Sopenharmony_ci
3646e41f4b71Sopenharmony_ciSubscribes to **close** events of a **TCPSocketConnection** object. This API uses an asynchronous callback to return the result.
3647e41f4b71Sopenharmony_ci
3648e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
3649e41f4b71Sopenharmony_ci
3650e41f4b71Sopenharmony_ci**Parameters**
3651e41f4b71Sopenharmony_ci
3652e41f4b71Sopenharmony_ci| Name  | Type            | Mandatory| Description                               |
3653e41f4b71Sopenharmony_ci| -------- | ---------------- | ---- | ----------------------------------- |
3654e41f4b71Sopenharmony_ci| type     | string           | Yes  | Event type.<br/> **close**: close event.|
3655e41f4b71Sopenharmony_ci| callback | Callback\<void\> | Yes  | Callback used to return the result. If the operation fails, an error message is returned.       |
3656e41f4b71Sopenharmony_ci
3657e41f4b71Sopenharmony_ci**Error codes**
3658e41f4b71Sopenharmony_ci
3659e41f4b71Sopenharmony_ci| ID| Error Message        |
3660e41f4b71Sopenharmony_ci| -------- | ---------------- |
3661e41f4b71Sopenharmony_ci| 401      | Parameter error. |
3662e41f4b71Sopenharmony_ci
3663e41f4b71Sopenharmony_ci**Example**
3664e41f4b71Sopenharmony_ci
3665e41f4b71Sopenharmony_ci```ts
3666e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
3667e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
3668e41f4b71Sopenharmony_ci
3669e41f4b71Sopenharmony_cilet tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance();
3670e41f4b71Sopenharmony_citcpServer.on('connect', (client: socket.TCPSocketConnection) => {
3671e41f4b71Sopenharmony_ci  client.on('close', () => {
3672e41f4b71Sopenharmony_ci    console.log("on close success")
3673e41f4b71Sopenharmony_ci  });
3674e41f4b71Sopenharmony_ci});
3675e41f4b71Sopenharmony_ci```
3676e41f4b71Sopenharmony_ci
3677e41f4b71Sopenharmony_ci### off('close')<sup>10+</sup>
3678e41f4b71Sopenharmony_ci
3679e41f4b71Sopenharmony_cioff(type: 'close', callback?: Callback\<void\>): void
3680e41f4b71Sopenharmony_ci
3681e41f4b71Sopenharmony_ciUnsubscribes from **close** events of a **TCPSocketConnection** object. This API uses an asynchronous callback to return the result.
3682e41f4b71Sopenharmony_ci
3683e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
3684e41f4b71Sopenharmony_ci
3685e41f4b71Sopenharmony_ci**Parameters**
3686e41f4b71Sopenharmony_ci
3687e41f4b71Sopenharmony_ci| Name  | Type            | Mandatory| Description                               |
3688e41f4b71Sopenharmony_ci| -------- | ---------------- | ---- | ----------------------------------- |
3689e41f4b71Sopenharmony_ci| type     | string           | Yes  | Event type.<br/> **close**: close event.|
3690e41f4b71Sopenharmony_ci| callback | Callback\<void\> | No  | Callback used to return the result. If the operation fails, an error message is returned.   |
3691e41f4b71Sopenharmony_ci
3692e41f4b71Sopenharmony_ci**Error codes**
3693e41f4b71Sopenharmony_ci
3694e41f4b71Sopenharmony_ci| ID| Error Message        |
3695e41f4b71Sopenharmony_ci| -------- | ---------------- |
3696e41f4b71Sopenharmony_ci| 401      | Parameter error. |
3697e41f4b71Sopenharmony_ci
3698e41f4b71Sopenharmony_ci**Example**
3699e41f4b71Sopenharmony_ci
3700e41f4b71Sopenharmony_ci```ts
3701e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
3702e41f4b71Sopenharmony_ci
3703e41f4b71Sopenharmony_cilet tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance();
3704e41f4b71Sopenharmony_cilet callback = () => {
3705e41f4b71Sopenharmony_ci  console.log("on close success");
3706e41f4b71Sopenharmony_ci}
3707e41f4b71Sopenharmony_citcpServer.on('connect', (client: socket.TCPSocketConnection) => {
3708e41f4b71Sopenharmony_ci  client.on('close', callback);
3709e41f4b71Sopenharmony_ci  // You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
3710e41f4b71Sopenharmony_ci  client.off('close', callback);
3711e41f4b71Sopenharmony_ci  client.off('close');
3712e41f4b71Sopenharmony_ci});
3713e41f4b71Sopenharmony_ci```
3714e41f4b71Sopenharmony_ci
3715e41f4b71Sopenharmony_ci### on('error')<sup>10+</sup>
3716e41f4b71Sopenharmony_ci
3717e41f4b71Sopenharmony_cion(type: 'error', callback: ErrorCallback): void
3718e41f4b71Sopenharmony_ci
3719e41f4b71Sopenharmony_ciSubscribes to **error** events of a **TCPSocketConnection** object. This API uses an asynchronous callback to return the result.
3720e41f4b71Sopenharmony_ci
3721e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
3722e41f4b71Sopenharmony_ci
3723e41f4b71Sopenharmony_ci**Parameters**
3724e41f4b71Sopenharmony_ci
3725e41f4b71Sopenharmony_ci| Name  | Type         | Mandatory| Description                                |
3726e41f4b71Sopenharmony_ci| -------- | ------------- | ---- | ------------------------------------ |
3727e41f4b71Sopenharmony_ci| type     | string        | Yes  | Event type.<br/> **error**: error event.|
3728e41f4b71Sopenharmony_ci| callback | ErrorCallback | Yes  | Callback used to return the result. If the operation fails, an error message is returned.   |
3729e41f4b71Sopenharmony_ci
3730e41f4b71Sopenharmony_ci**Error codes**
3731e41f4b71Sopenharmony_ci
3732e41f4b71Sopenharmony_ci| ID| Error Message        |
3733e41f4b71Sopenharmony_ci| -------- | ---------------- |
3734e41f4b71Sopenharmony_ci| 401      | Parameter error. |
3735e41f4b71Sopenharmony_ci
3736e41f4b71Sopenharmony_ci**Example**
3737e41f4b71Sopenharmony_ci
3738e41f4b71Sopenharmony_ci```ts
3739e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
3740e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
3741e41f4b71Sopenharmony_ci
3742e41f4b71Sopenharmony_cilet tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance();
3743e41f4b71Sopenharmony_citcpServer.on('connect', (client: socket.TCPSocketConnection) => {
3744e41f4b71Sopenharmony_ci  client.on('error', (err: BusinessError) => {
3745e41f4b71Sopenharmony_ci    console.log("on error, err:" + JSON.stringify(err))
3746e41f4b71Sopenharmony_ci  });
3747e41f4b71Sopenharmony_ci});
3748e41f4b71Sopenharmony_ci```
3749e41f4b71Sopenharmony_ci
3750e41f4b71Sopenharmony_ci### off('error')<sup>10+</sup>
3751e41f4b71Sopenharmony_ci
3752e41f4b71Sopenharmony_cioff(type: 'error', callback?: ErrorCallback): void
3753e41f4b71Sopenharmony_ci
3754e41f4b71Sopenharmony_ciUnsubscribes from **error** events of a **TCPSocketConnection** object. This API uses an asynchronous callback to return the result.
3755e41f4b71Sopenharmony_ci
3756e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
3757e41f4b71Sopenharmony_ci
3758e41f4b71Sopenharmony_ci**Parameters**
3759e41f4b71Sopenharmony_ci
3760e41f4b71Sopenharmony_ci| Name  | Type         | Mandatory| Description                                |
3761e41f4b71Sopenharmony_ci| -------- | ------------- | ---- | ------------------------------------ |
3762e41f4b71Sopenharmony_ci| type     | string        | Yes  | Event type.<br/> **error**: error event.|
3763e41f4b71Sopenharmony_ci| callback | ErrorCallback | No  | Callback used to return the result. If the operation fails, an error message is returned. |
3764e41f4b71Sopenharmony_ci
3765e41f4b71Sopenharmony_ci**Error codes**
3766e41f4b71Sopenharmony_ci
3767e41f4b71Sopenharmony_ci| ID| Error Message        |
3768e41f4b71Sopenharmony_ci| -------- | ---------------- |
3769e41f4b71Sopenharmony_ci| 401      | Parameter error. |
3770e41f4b71Sopenharmony_ci
3771e41f4b71Sopenharmony_ci**Example**
3772e41f4b71Sopenharmony_ci
3773e41f4b71Sopenharmony_ci```ts
3774e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
3775e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
3776e41f4b71Sopenharmony_ci
3777e41f4b71Sopenharmony_cilet callback = (err: BusinessError) => {
3778e41f4b71Sopenharmony_ci  console.log("on error, err:" + JSON.stringify(err));
3779e41f4b71Sopenharmony_ci}
3780e41f4b71Sopenharmony_cilet tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance();
3781e41f4b71Sopenharmony_citcpServer.on('connect', (client: socket.TCPSocketConnection) => {
3782e41f4b71Sopenharmony_ci  client.on('error', callback);
3783e41f4b71Sopenharmony_ci  // You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
3784e41f4b71Sopenharmony_ci  client.off('error', callback);
3785e41f4b71Sopenharmony_ci  client.off('error');
3786e41f4b71Sopenharmony_ci});
3787e41f4b71Sopenharmony_ci```
3788e41f4b71Sopenharmony_ci
3789e41f4b71Sopenharmony_ci## Description of TCP Error Codes
3790e41f4b71Sopenharmony_ci
3791e41f4b71Sopenharmony_ciThe TCP error code mapping is in the format of 2301000 + Linux kernel error code.
3792e41f4b71Sopenharmony_ci
3793e41f4b71Sopenharmony_ciFor details about error codes, see [Socket Error Codes](errorcode-net-socket.md).
3794e41f4b71Sopenharmony_ci
3795e41f4b71Sopenharmony_ci## socket.constructLocalSocketInstance<sup>11+</sup>
3796e41f4b71Sopenharmony_ci
3797e41f4b71Sopenharmony_ciconstructLocalSocketInstance(): LocalSocket
3798e41f4b71Sopenharmony_ci
3799e41f4b71Sopenharmony_ciCreates a **LocalSocket** object.
3800e41f4b71Sopenharmony_ci
3801e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
3802e41f4b71Sopenharmony_ci
3803e41f4b71Sopenharmony_ci**Return value**
3804e41f4b71Sopenharmony_ci
3805e41f4b71Sopenharmony_ci| Type                              | Description                   |
3806e41f4b71Sopenharmony_ci  | :--------------------------------- | :---------------------- |
3807e41f4b71Sopenharmony_ci| [LocalSocket](#localsocket11) | **LocalSocket** object.|
3808e41f4b71Sopenharmony_ci
3809e41f4b71Sopenharmony_ci**Example**
3810e41f4b71Sopenharmony_ci
3811e41f4b71Sopenharmony_ci```ts
3812e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
3813e41f4b71Sopenharmony_cilet client: socket.LocalSocket = socket.constructLocalSocketInstance();
3814e41f4b71Sopenharmony_ci```
3815e41f4b71Sopenharmony_ci
3816e41f4b71Sopenharmony_ci## LocalSocket<sup>11+</sup>
3817e41f4b71Sopenharmony_ci
3818e41f4b71Sopenharmony_ciDefines a **LocalSocket** object. Before calling LocalSocket APIs, you need to call [socket.constructLocalSocketInstance](#socketconstructlocalsocketinstance11) to create a **LocalSocket** object.
3819e41f4b71Sopenharmony_ci
3820e41f4b71Sopenharmony_ci### bind<sup>11+</sup>
3821e41f4b71Sopenharmony_ci
3822e41f4b71Sopenharmony_cibind(address: LocalAddress): Promise\<void\>;
3823e41f4b71Sopenharmony_ci
3824e41f4b71Sopenharmony_ciBinds the address of a local socket file. This API uses a promise to return the result.
3825e41f4b71Sopenharmony_ci
3826e41f4b71Sopenharmony_ci> **NOTE**
3827e41f4b71Sopenharmony_ci> This API explicitly binds the client to a local socket file based on the specified address.
3828e41f4b71Sopenharmony_ci> It is not mandatory in local socket communication.
3829e41f4b71Sopenharmony_ci
3830e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
3831e41f4b71Sopenharmony_ci
3832e41f4b71Sopenharmony_ci**Parameters**
3833e41f4b71Sopenharmony_ci
3834e41f4b71Sopenharmony_ci| Name  | Type                              | Mandatory| Description                                                  |
3835e41f4b71Sopenharmony_ci| -------- | ---------------------------------- | ---- | ------------------------------------------------------ |
3836e41f4b71Sopenharmony_ci| address  | [LocalAddress](#localaddress11) | Yes  | Destination address. For details, see [LocalAddress](#localaddress11).|
3837e41f4b71Sopenharmony_ci
3838e41f4b71Sopenharmony_ci**Error codes**
3839e41f4b71Sopenharmony_ci
3840e41f4b71Sopenharmony_ci| ID| Error Message                   |
3841e41f4b71Sopenharmony_ci| ------- | -------------------------- |
3842e41f4b71Sopenharmony_ci| 401     | Parameter error.           |
3843e41f4b71Sopenharmony_ci| 2301013 | Insufficient permissions.  |
3844e41f4b71Sopenharmony_ci| 2301022 | Invalid argument.          |
3845e41f4b71Sopenharmony_ci| 2301098 | Address already in use.    |
3846e41f4b71Sopenharmony_ci
3847e41f4b71Sopenharmony_ci**Example**
3848e41f4b71Sopenharmony_ci
3849e41f4b71Sopenharmony_ci```ts
3850e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
3851e41f4b71Sopenharmony_ci
3852e41f4b71Sopenharmony_cilet client: socket.LocalSocket = socket.constructLocalSocketInstance()
3853e41f4b71Sopenharmony_cilet sandboxPath: string = getContext().filesDir + '/testSocket'
3854e41f4b71Sopenharmony_cilet address : socket.LocalAddress = {
3855e41f4b71Sopenharmony_ci  address: sandboxPath
3856e41f4b71Sopenharmony_ci}
3857e41f4b71Sopenharmony_ciclient.bind(address).then(() => {
3858e41f4b71Sopenharmony_ci  console.log('bind success')
3859e41f4b71Sopenharmony_ci}).catch((err: Object) => {
3860e41f4b71Sopenharmony_ci  console.error('failed to bind: ' + JSON.stringify(err))
3861e41f4b71Sopenharmony_ci})
3862e41f4b71Sopenharmony_ci```
3863e41f4b71Sopenharmony_ci
3864e41f4b71Sopenharmony_ci### connect<sup>11+</sup>
3865e41f4b71Sopenharmony_ci
3866e41f4b71Sopenharmony_ciconnect(options: LocalConnectOptions): Promise\<void\>
3867e41f4b71Sopenharmony_ci
3868e41f4b71Sopenharmony_ciConnects to the specified socket file. This API uses a promise to return the result.
3869e41f4b71Sopenharmony_ci
3870e41f4b71Sopenharmony_ci> **NOTE**
3871e41f4b71Sopenharmony_ci> This API allows you to connect to the TCP server without first executing **localsocket.bind**.
3872e41f4b71Sopenharmony_ci
3873e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
3874e41f4b71Sopenharmony_ci
3875e41f4b71Sopenharmony_ci**Parameters**
3876e41f4b71Sopenharmony_ci
3877e41f4b71Sopenharmony_ci| Name | Type                                    | Mandatory| Description                                                        |
3878e41f4b71Sopenharmony_ci| ------- | ---------------------------------------- | ---- | ------------------------------------------------------------ |
3879e41f4b71Sopenharmony_ci| options | [LocalConnectOptions](#localconnectoptions11) | Yes  | Local socket connection parameters. For details, see [LocalConnectOptions](#localconnectoptions11).|
3880e41f4b71Sopenharmony_ci
3881e41f4b71Sopenharmony_ci**Return value**
3882e41f4b71Sopenharmony_ci
3883e41f4b71Sopenharmony_ci| Type           | Description                                      |
3884e41f4b71Sopenharmony_ci| :-------------- | :---------------------------------------- |
3885e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result.|
3886e41f4b71Sopenharmony_ci
3887e41f4b71Sopenharmony_ci**Error codes**
3888e41f4b71Sopenharmony_ci
3889e41f4b71Sopenharmony_ci| ID| Error Message                |
3890e41f4b71Sopenharmony_ci| ------- | ----------------------- |
3891e41f4b71Sopenharmony_ci| 401     | Parameter error.                 |
3892e41f4b71Sopenharmony_ci| 2301013     | Insufficient permissions.        |
3893e41f4b71Sopenharmony_ci| 2301022     | Invalid argument.                |
3894e41f4b71Sopenharmony_ci| 2301111     | Connection refused.              |
3895e41f4b71Sopenharmony_ci| 2301099     | Cannot assign requested address. |
3896e41f4b71Sopenharmony_ci
3897e41f4b71Sopenharmony_ci**Example**
3898e41f4b71Sopenharmony_ci
3899e41f4b71Sopenharmony_ci```ts
3900e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
3901e41f4b71Sopenharmony_ci
3902e41f4b71Sopenharmony_cilet client: socket.LocalSocket = socket.constructLocalSocketInstance();
3903e41f4b71Sopenharmony_cilet sandboxPath: string = getContext().filesDir + '/testSocket'
3904e41f4b71Sopenharmony_cilet localAddress : socket.LocalAddress = {
3905e41f4b71Sopenharmony_ci  address: sandboxPath
3906e41f4b71Sopenharmony_ci}
3907e41f4b71Sopenharmony_cilet connectOpt: socket.LocalConnectOptions = {
3908e41f4b71Sopenharmony_ci  address: localAddress,
3909e41f4b71Sopenharmony_ci  timeout: 6000
3910e41f4b71Sopenharmony_ci}
3911e41f4b71Sopenharmony_ciclient.connect(connectOpt).then(() => {
3912e41f4b71Sopenharmony_ci  console.log('connect success')
3913e41f4b71Sopenharmony_ci}).catch((err: Object) => {
3914e41f4b71Sopenharmony_ci  console.error('connect fail: ' + JSON.stringify(err));
3915e41f4b71Sopenharmony_ci});
3916e41f4b71Sopenharmony_ci```
3917e41f4b71Sopenharmony_ci
3918e41f4b71Sopenharmony_ci### send<sup>11+</sup>
3919e41f4b71Sopenharmony_ci
3920e41f4b71Sopenharmony_cisend(options: LocalSendOptions): Promise\<void\>
3921e41f4b71Sopenharmony_ci
3922e41f4b71Sopenharmony_ciSends data over a local socket connection. This API uses a promise to return the result.
3923e41f4b71Sopenharmony_ci
3924e41f4b71Sopenharmony_ci> **NOTE**
3925e41f4b71Sopenharmony_ci> This API can be called only after **connect** is successfully called.
3926e41f4b71Sopenharmony_ci
3927e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
3928e41f4b71Sopenharmony_ci
3929e41f4b71Sopenharmony_ci**Parameters**
3930e41f4b71Sopenharmony_ci
3931e41f4b71Sopenharmony_ci| Name | Type                                   | Mandatory| Description                                                        |
3932e41f4b71Sopenharmony_ci| ------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
3933e41f4b71Sopenharmony_ci| options | [LocalSendOptions](#localsendoptions11) | Yes  | Parameters for sending data over a local socket connection. For details, see [LocalSendOptions](#localsendoptions11).|
3934e41f4b71Sopenharmony_ci
3935e41f4b71Sopenharmony_ci**Return value**
3936e41f4b71Sopenharmony_ci
3937e41f4b71Sopenharmony_ci| Type           | Description                                        |
3938e41f4b71Sopenharmony_ci| :-------------- | :------------------------------------------ |
3939e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result.|
3940e41f4b71Sopenharmony_ci
3941e41f4b71Sopenharmony_ci**Error codes**
3942e41f4b71Sopenharmony_ci
3943e41f4b71Sopenharmony_ci| ID| Error Message                |
3944e41f4b71Sopenharmony_ci| ------- | ----------------------- |
3945e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
3946e41f4b71Sopenharmony_ci| 2301011 | Operation would block.  |
3947e41f4b71Sopenharmony_ci
3948e41f4b71Sopenharmony_ci**Example**
3949e41f4b71Sopenharmony_ci
3950e41f4b71Sopenharmony_ci```ts
3951e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
3952e41f4b71Sopenharmony_ci
3953e41f4b71Sopenharmony_cilet client: socket.LocalSocket = socket.constructLocalSocketInstance()
3954e41f4b71Sopenharmony_cilet sandboxPath: string = getContext().filesDir + '/testSocket'
3955e41f4b71Sopenharmony_cilet localAddress : socket.LocalAddress = {
3956e41f4b71Sopenharmony_ci  address: sandboxPath
3957e41f4b71Sopenharmony_ci}
3958e41f4b71Sopenharmony_cilet connectOpt: socket.LocalConnectOptions = {
3959e41f4b71Sopenharmony_ci  address: localAddress,
3960e41f4b71Sopenharmony_ci  timeout: 6000
3961e41f4b71Sopenharmony_ci}
3962e41f4b71Sopenharmony_ciclient.connect(connectOpt).then(() => {
3963e41f4b71Sopenharmony_ci  console.log('connect success')
3964e41f4b71Sopenharmony_ci}).catch((err: Object) => {
3965e41f4b71Sopenharmony_ci  console.error('connect failed: ' + JSON.stringify(err))
3966e41f4b71Sopenharmony_ci})
3967e41f4b71Sopenharmony_cilet sendOpt: socket.LocalSendOptions = {
3968e41f4b71Sopenharmony_ci  data: 'Hello world!'
3969e41f4b71Sopenharmony_ci}
3970e41f4b71Sopenharmony_ciclient.send(sendOpt).then(() => {
3971e41f4b71Sopenharmony_ci  console.log('send success')
3972e41f4b71Sopenharmony_ci}).catch((err: Object) => {
3973e41f4b71Sopenharmony_ci  console.error('send fail: ' + JSON.stringify(err))
3974e41f4b71Sopenharmony_ci})
3975e41f4b71Sopenharmony_ci```
3976e41f4b71Sopenharmony_ci
3977e41f4b71Sopenharmony_ci### close<sup>11+</sup>
3978e41f4b71Sopenharmony_ci
3979e41f4b71Sopenharmony_ciclose(): Promise\<void\>
3980e41f4b71Sopenharmony_ci
3981e41f4b71Sopenharmony_ciCloses a local socket connection. This API uses a promise to return the result.
3982e41f4b71Sopenharmony_ci
3983e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
3984e41f4b71Sopenharmony_ci
3985e41f4b71Sopenharmony_ci**Return value**
3986e41f4b71Sopenharmony_ci
3987e41f4b71Sopenharmony_ci| Type           | Description                                      |
3988e41f4b71Sopenharmony_ci| :-------------- | :----------------------------------------- |
3989e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result.|
3990e41f4b71Sopenharmony_ci
3991e41f4b71Sopenharmony_ci**Error codes**
3992e41f4b71Sopenharmony_ci
3993e41f4b71Sopenharmony_ci| ID| Error Message                |
3994e41f4b71Sopenharmony_ci| ------- | ----------------------- |
3995e41f4b71Sopenharmony_ci| 2301009 | Bad file descriptor.    |
3996e41f4b71Sopenharmony_ci
3997e41f4b71Sopenharmony_ci**Example**
3998e41f4b71Sopenharmony_ci
3999e41f4b71Sopenharmony_ci```ts
4000e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
4001e41f4b71Sopenharmony_ci
4002e41f4b71Sopenharmony_cilet client: socket.LocalSocket = socket.constructLocalSocketInstance();
4003e41f4b71Sopenharmony_ci
4004e41f4b71Sopenharmony_ciclient.close().then(() => {
4005e41f4b71Sopenharmony_ci  console.log('close success');
4006e41f4b71Sopenharmony_ci}).catch((err: Object) => {
4007e41f4b71Sopenharmony_ci  console.error('close fail: ' + JSON.stringify(err));
4008e41f4b71Sopenharmony_ci});
4009e41f4b71Sopenharmony_ci```
4010e41f4b71Sopenharmony_ci
4011e41f4b71Sopenharmony_ci### getState<sup>11+</sup>
4012e41f4b71Sopenharmony_ci
4013e41f4b71Sopenharmony_cigetState(): Promise\<SocketStateBase\>
4014e41f4b71Sopenharmony_ci
4015e41f4b71Sopenharmony_ciObtains the local socket connection status. This API uses a promise to return the result.
4016e41f4b71Sopenharmony_ci
4017e41f4b71Sopenharmony_ci> **NOTE**
4018e41f4b71Sopenharmony_ci> This API can be called only after **bind** or **connect** is successfully called.
4019e41f4b71Sopenharmony_ci
4020e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
4021e41f4b71Sopenharmony_ci
4022e41f4b71Sopenharmony_ci**Return value**
4023e41f4b71Sopenharmony_ci
4024e41f4b71Sopenharmony_ci| Type                                         | Description                                    |
4025e41f4b71Sopenharmony_ci| :------------------------------------------- | :--------------------------------------- |
4026e41f4b71Sopenharmony_ci| Promise\<[SocketStateBase](#socketstatebase)\> | Promise used to return the result.|
4027e41f4b71Sopenharmony_ci
4028e41f4b71Sopenharmony_ci**Example**
4029e41f4b71Sopenharmony_ci
4030e41f4b71Sopenharmony_ci```ts
4031e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
4032e41f4b71Sopenharmony_ci
4033e41f4b71Sopenharmony_cilet client: socket.LocalSocket = socket.constructLocalSocketInstance();
4034e41f4b71Sopenharmony_cilet sandboxPath: string = getContext().filesDir + '/testSocket'
4035e41f4b71Sopenharmony_cilet localAddress : socket.LocalAddress = {
4036e41f4b71Sopenharmony_ci  address: sandboxPath
4037e41f4b71Sopenharmony_ci}
4038e41f4b71Sopenharmony_cilet connectOpt: socket.LocalConnectOptions = {
4039e41f4b71Sopenharmony_ci  address: localAddress,
4040e41f4b71Sopenharmony_ci  timeout: 6000
4041e41f4b71Sopenharmony_ci}
4042e41f4b71Sopenharmony_ciclient.connect(connectOpt).then(() => {
4043e41f4b71Sopenharmony_ci  console.log('connect success');
4044e41f4b71Sopenharmony_ci  client.getState().then(() => {
4045e41f4b71Sopenharmony_ci    console.log('getState success');
4046e41f4b71Sopenharmony_ci  }).catch((err: Object) => {
4047e41f4b71Sopenharmony_ci    console.error('getState fail: ' + JSON.stringify(err))
4048e41f4b71Sopenharmony_ci  });
4049e41f4b71Sopenharmony_ci}).catch((err: Object) => {
4050e41f4b71Sopenharmony_ci  console.error('connect fail: ' + JSON.stringify(err));
4051e41f4b71Sopenharmony_ci});
4052e41f4b71Sopenharmony_ci```
4053e41f4b71Sopenharmony_ci
4054e41f4b71Sopenharmony_ci### getSocketFd<sup>11+</sup>
4055e41f4b71Sopenharmony_ci
4056e41f4b71Sopenharmony_cigetSocketFd(): Promise\<number\>
4057e41f4b71Sopenharmony_ci
4058e41f4b71Sopenharmony_ciObtains the file descriptor of the **LocalSocket** object. This API uses a promise to return the result.
4059e41f4b71Sopenharmony_ci
4060e41f4b71Sopenharmony_ci> **NOTE**
4061e41f4b71Sopenharmony_ci> This API can be called only after **bind** or **connect** is successfully called.
4062e41f4b71Sopenharmony_ci> The file descriptor is allocated by the system kernel to uniquely identify the local socket in use.
4063e41f4b71Sopenharmony_ci
4064e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
4065e41f4b71Sopenharmony_ci
4066e41f4b71Sopenharmony_ci**Return value**
4067e41f4b71Sopenharmony_ci
4068e41f4b71Sopenharmony_ci| Type              | Description                             |
4069e41f4b71Sopenharmony_ci| :---------------- | :-------------------------------- |
4070e41f4b71Sopenharmony_ci| Promise\<number\> | Promise used to return the result.|
4071e41f4b71Sopenharmony_ci
4072e41f4b71Sopenharmony_ci**Example**
4073e41f4b71Sopenharmony_ci
4074e41f4b71Sopenharmony_ci```ts
4075e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
4076e41f4b71Sopenharmony_ci
4077e41f4b71Sopenharmony_cilet client: socket.LocalSocket = socket.constructLocalSocketInstance();
4078e41f4b71Sopenharmony_cilet sandboxPath: string = getContext().filesDir + '/testSocket'
4079e41f4b71Sopenharmony_cilet localAddress : socket.LocalAddress = {
4080e41f4b71Sopenharmony_ci  address: sandboxPath
4081e41f4b71Sopenharmony_ci}
4082e41f4b71Sopenharmony_cilet connectOpt: socket.LocalConnectOptions = {
4083e41f4b71Sopenharmony_ci  address: localAddress,
4084e41f4b71Sopenharmony_ci  timeout: 6000
4085e41f4b71Sopenharmony_ci}
4086e41f4b71Sopenharmony_ciclient.connect(connectOpt).then(() => {
4087e41f4b71Sopenharmony_ci  console.log('connect ok')
4088e41f4b71Sopenharmony_ci}).catch((err: Object) => {
4089e41f4b71Sopenharmony_ci  console.error('connect fail: ' + JSON.stringify(err))
4090e41f4b71Sopenharmony_ci})
4091e41f4b71Sopenharmony_ciclient.getSocketFd().then((data: number) => {
4092e41f4b71Sopenharmony_ci  console.info("fd: " + data);
4093e41f4b71Sopenharmony_ci}).catch((err: Object) => {
4094e41f4b71Sopenharmony_ci  console.error("getSocketFd faile: " + JSON.stringify(err));
4095e41f4b71Sopenharmony_ci})
4096e41f4b71Sopenharmony_ci```
4097e41f4b71Sopenharmony_ci
4098e41f4b71Sopenharmony_ci### setExtraOptions<sup>11+</sup>
4099e41f4b71Sopenharmony_ci
4100e41f4b71Sopenharmony_cisetExtraOptions(options: ExtraOptionsBase): Promise\<void\>
4101e41f4b71Sopenharmony_ci
4102e41f4b71Sopenharmony_ciSets other properties of the local socket connection. This API uses a promise to return the result.
4103e41f4b71Sopenharmony_ci
4104e41f4b71Sopenharmony_ci> **NOTE**
4105e41f4b71Sopenharmony_ci> This API can be called only after **bind** or **connect** is successfully called.
4106e41f4b71Sopenharmony_ci
4107e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
4108e41f4b71Sopenharmony_ci
4109e41f4b71Sopenharmony_ci**Parameters**
4110e41f4b71Sopenharmony_ci
4111e41f4b71Sopenharmony_ci| Name | Type                                     | Mandatory| Description                                                        |
4112e41f4b71Sopenharmony_ci| ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
4113e41f4b71Sopenharmony_ci| options | [ExtraOptionsBase](#extraoptionsbase7) | Yes  | Other properties of the local socket connection. For details, see [ExtraOptionsBase](#extraoptionsbase7).|
4114e41f4b71Sopenharmony_ci
4115e41f4b71Sopenharmony_ci**Return value**
4116e41f4b71Sopenharmony_ci
4117e41f4b71Sopenharmony_ci| Type           | Description                                          |
4118e41f4b71Sopenharmony_ci| :-------------- | :-------------------------------------------- |
4119e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result.|
4120e41f4b71Sopenharmony_ci
4121e41f4b71Sopenharmony_ci**Error codes**
4122e41f4b71Sopenharmony_ci
4123e41f4b71Sopenharmony_ci| ID| Error Message                |
4124e41f4b71Sopenharmony_ci| ------- | ----------------------- |
4125e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
4126e41f4b71Sopenharmony_ci| 2301009 | Bad file descriptor.    |
4127e41f4b71Sopenharmony_ci
4128e41f4b71Sopenharmony_ci**Example**
4129e41f4b71Sopenharmony_ci
4130e41f4b71Sopenharmony_ci```ts
4131e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
4132e41f4b71Sopenharmony_ci
4133e41f4b71Sopenharmony_cilet client: socket.LocalSocket = socket.constructLocalSocketInstance();
4134e41f4b71Sopenharmony_cilet sandboxPath: string = getContext().filesDir + '/testSocket'
4135e41f4b71Sopenharmony_cilet localAddress : socket.LocalAddress = {
4136e41f4b71Sopenharmony_ci  address: sandboxPath
4137e41f4b71Sopenharmony_ci}
4138e41f4b71Sopenharmony_cilet connectOpt: socket.LocalConnectOptions = {
4139e41f4b71Sopenharmony_ci  address: localAddress,
4140e41f4b71Sopenharmony_ci  timeout: 6000
4141e41f4b71Sopenharmony_ci}
4142e41f4b71Sopenharmony_ciclient.connect(connectOpt).then(() => {
4143e41f4b71Sopenharmony_ci  console.log('connect success');
4144e41f4b71Sopenharmony_ci  let options: socket.ExtraOptionsBase = {
4145e41f4b71Sopenharmony_ci    receiveBufferSize: 8000,
4146e41f4b71Sopenharmony_ci    sendBufferSize: 8000,
4147e41f4b71Sopenharmony_ci    socketTimeout: 3000
4148e41f4b71Sopenharmony_ci  }
4149e41f4b71Sopenharmony_ci  client.setExtraOptions(options).then(() => {
4150e41f4b71Sopenharmony_ci    console.log('setExtraOptions success');
4151e41f4b71Sopenharmony_ci  }).catch((err: Object) => {
4152e41f4b71Sopenharmony_ci    console.error('setExtraOptions fail: ' + JSON.stringify(err));
4153e41f4b71Sopenharmony_ci  });
4154e41f4b71Sopenharmony_ci}).catch((err: Object) => {
4155e41f4b71Sopenharmony_ci  console.error('connect fail: ' + JSON.stringify(err));
4156e41f4b71Sopenharmony_ci});
4157e41f4b71Sopenharmony_ci```
4158e41f4b71Sopenharmony_ci
4159e41f4b71Sopenharmony_ci### getExtraOptions<sup>11+</sup>
4160e41f4b71Sopenharmony_ci
4161e41f4b71Sopenharmony_cigetExtraOptions(): Promise\<ExtraOptionsBase\>;
4162e41f4b71Sopenharmony_ci
4163e41f4b71Sopenharmony_ciObtains other properties of the local socket connection. This API uses a promise to return the result.
4164e41f4b71Sopenharmony_ci
4165e41f4b71Sopenharmony_ci> **NOTE**
4166e41f4b71Sopenharmony_ci> This API can be called only after **bind** or **connect** is successfully called.
4167e41f4b71Sopenharmony_ci
4168e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
4169e41f4b71Sopenharmony_ci
4170e41f4b71Sopenharmony_ci**Return value**
4171e41f4b71Sopenharmony_ci
4172e41f4b71Sopenharmony_ci| Type                        | Description                                     |
4173e41f4b71Sopenharmony_ci| :-------------------------- | :---------------------------------------- |
4174e41f4b71Sopenharmony_ci| Promise\<[ExtraOptionsBase](#extraoptionsbase7)\> | Promise used to return the result.|
4175e41f4b71Sopenharmony_ci
4176e41f4b71Sopenharmony_ci**Error codes**
4177e41f4b71Sopenharmony_ci
4178e41f4b71Sopenharmony_ci| ID| Error Message                |
4179e41f4b71Sopenharmony_ci| ------- | ----------------------- |
4180e41f4b71Sopenharmony_ci| 2301009 | Bad file descriptor.    |
4181e41f4b71Sopenharmony_ci
4182e41f4b71Sopenharmony_ci**Example**
4183e41f4b71Sopenharmony_ci
4184e41f4b71Sopenharmony_ci```ts
4185e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
4186e41f4b71Sopenharmony_ci
4187e41f4b71Sopenharmony_cilet client: socket.LocalSocket = socket.constructLocalSocketInstance();
4188e41f4b71Sopenharmony_cilet sandboxPath: string = getContext().filesDir + '/testSocket'
4189e41f4b71Sopenharmony_cilet localAddress : socket.LocalAddress = {
4190e41f4b71Sopenharmony_ci  address: sandboxPath
4191e41f4b71Sopenharmony_ci}
4192e41f4b71Sopenharmony_cilet connectOpt: socket.LocalConnectOptions = {
4193e41f4b71Sopenharmony_ci  address: localAddress,
4194e41f4b71Sopenharmony_ci  timeout: 6000
4195e41f4b71Sopenharmony_ci}
4196e41f4b71Sopenharmony_ciclient.connect(connectOpt).then(() => {
4197e41f4b71Sopenharmony_ci  console.log('connect success');
4198e41f4b71Sopenharmony_ci  client.getExtraOptions().then((options : socket.ExtraOptionsBase) => {
4199e41f4b71Sopenharmony_ci    console.log('options: ' + JSON.stringify(options));
4200e41f4b71Sopenharmony_ci  }).catch((err: Object) => {
4201e41f4b71Sopenharmony_ci    console.error('setExtraOptions fail: ' + JSON.stringify(err));
4202e41f4b71Sopenharmony_ci  });
4203e41f4b71Sopenharmony_ci}).catch((err: Object) => {
4204e41f4b71Sopenharmony_ci  console.error('connect fail: ' + JSON.stringify(err));
4205e41f4b71Sopenharmony_ci});
4206e41f4b71Sopenharmony_ci```
4207e41f4b71Sopenharmony_ci
4208e41f4b71Sopenharmony_ci### getLocalAddress<sup>12+</sup>
4209e41f4b71Sopenharmony_ci
4210e41f4b71Sopenharmony_cigetLocalAddress(): Promise\<string\>
4211e41f4b71Sopenharmony_ci
4212e41f4b71Sopenharmony_ciObtains the local socket address of a **LocalSocket** connection. This API uses a promise to return the result.
4213e41f4b71Sopenharmony_ci
4214e41f4b71Sopenharmony_ci> **NOTE**
4215e41f4b71Sopenharmony_ci> This API can be called only after **bind** is successfully called.
4216e41f4b71Sopenharmony_ci
4217e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
4218e41f4b71Sopenharmony_ci
4219e41f4b71Sopenharmony_ci**Return value**
4220e41f4b71Sopenharmony_ci
4221e41f4b71Sopenharmony_ci| Type           | Description                                                |
4222e41f4b71Sopenharmony_ci|  -------------- |  --------------------------------------------------- |
4223e41f4b71Sopenharmony_ci| Promise\<string\> | Promise used to return the result.|
4224e41f4b71Sopenharmony_ci
4225e41f4b71Sopenharmony_ci**Error codes**
4226e41f4b71Sopenharmony_ci
4227e41f4b71Sopenharmony_ci| ID| Error Message                                   |
4228e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- |
4229e41f4b71Sopenharmony_ci| 2300002  | System internal error.                      |
4230e41f4b71Sopenharmony_ci| 2301009  | Bad file descriptor.                            |
4231e41f4b71Sopenharmony_ci| 2303188  | Socket operation on non-socket. |
4232e41f4b71Sopenharmony_ci
4233e41f4b71Sopenharmony_ci**Example**
4234e41f4b71Sopenharmony_ci
4235e41f4b71Sopenharmony_ci```ts
4236e41f4b71Sopenharmony_cilet client: socket.LocalSocket = socket.constructLocalSocketInstance();
4237e41f4b71Sopenharmony_cilet sandboxPath: string = getContext().filesDir + '/testSocket';
4238e41f4b71Sopenharmony_cilet address : socket.LocalAddress = {
4239e41f4b71Sopenharmony_ci  address: sandboxPath
4240e41f4b71Sopenharmony_ci}
4241e41f4b71Sopenharmony_ciclient.bind(address).then(() => {
4242e41f4b71Sopenharmony_ci  console.error('bind success');
4243e41f4b71Sopenharmony_ci  client.getLocalAddress().then((localPath: string) => {
4244e41f4b71Sopenharmony_ci    console.info("SUCCESS " + JSON.stringify(localPath));
4245e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
4246e41f4b71Sopenharmony_ci    console.error("FAIL " + JSON.stringify(err));
4247e41f4b71Sopenharmony_ci  })
4248e41f4b71Sopenharmony_ci}).catch((err: Object) => {
4249e41f4b71Sopenharmony_ci  console.info('failed to bind: ' + JSON.stringify(err));
4250e41f4b71Sopenharmony_ci})
4251e41f4b71Sopenharmony_ci```
4252e41f4b71Sopenharmony_ci
4253e41f4b71Sopenharmony_ci### on('message')<sup>11+</sup>
4254e41f4b71Sopenharmony_ci
4255e41f4b71Sopenharmony_cion(type: 'message', callback: Callback\<LocalSocketMessageInfo\>): void
4256e41f4b71Sopenharmony_ci
4257e41f4b71Sopenharmony_ciSubscribes to **message** events of a **LocalSocket** object. This API uses an asynchronous callback to return the result.
4258e41f4b71Sopenharmony_ci
4259e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
4260e41f4b71Sopenharmony_ci
4261e41f4b71Sopenharmony_ci**Parameters**
4262e41f4b71Sopenharmony_ci
4263e41f4b71Sopenharmony_ci| Name  | Type                                             | Mandatory| Description                                     |
4264e41f4b71Sopenharmony_ci| -------- | ----------------------------------------------- | ---- | ----------------------------------- |
4265e41f4b71Sopenharmony_ci| type     | string                                          | Yes  | Event type.<br/> **message**: message receiving event.|
4266e41f4b71Sopenharmony_ci| callback | Callback\<[LocalSocketMessageInfo](#localsocketmessageinfo11)\> | Yes  | Callback used to return the result.|
4267e41f4b71Sopenharmony_ci
4268e41f4b71Sopenharmony_ci**Error codes**
4269e41f4b71Sopenharmony_ci
4270e41f4b71Sopenharmony_ci| ID| Error Message                |
4271e41f4b71Sopenharmony_ci| ------- | ----------------------- |
4272e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
4273e41f4b71Sopenharmony_ci
4274e41f4b71Sopenharmony_ci**Example**
4275e41f4b71Sopenharmony_ci
4276e41f4b71Sopenharmony_ci```ts
4277e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
4278e41f4b71Sopenharmony_ci
4279e41f4b71Sopenharmony_cilet client: socket.LocalSocket = socket.constructLocalSocketInstance();
4280e41f4b71Sopenharmony_ciclient.on('message', (value: socket.LocalSocketMessageInfo) => {
4281e41f4b71Sopenharmony_ci  const uintArray = new Uint8Array(value.message)
4282e41f4b71Sopenharmony_ci  let messageView = '';
4283e41f4b71Sopenharmony_ci  for (let i = 0; i < uintArray.length; i++) {
4284e41f4b71Sopenharmony_ci    messageView += String.fromCharCode(uintArray[i]);
4285e41f4b71Sopenharmony_ci  }
4286e41f4b71Sopenharmony_ci  console.log('total: ' + JSON.stringify(value));
4287e41f4b71Sopenharmony_ci  console.log('message infomation: ' + messageView);
4288e41f4b71Sopenharmony_ci});
4289e41f4b71Sopenharmony_ci```
4290e41f4b71Sopenharmony_ci
4291e41f4b71Sopenharmony_ci### off('message')<sup>11+</sup>
4292e41f4b71Sopenharmony_ci
4293e41f4b71Sopenharmony_cioff(type: 'message', callback?: Callback\<LocalSocketMessageInfo\>): void
4294e41f4b71Sopenharmony_ci
4295e41f4b71Sopenharmony_ciUnsubscribes from **message** events of a **LocalSocket** object. This API uses an asynchronous callback to return the result.
4296e41f4b71Sopenharmony_ci
4297e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
4298e41f4b71Sopenharmony_ci
4299e41f4b71Sopenharmony_ci**Parameters**
4300e41f4b71Sopenharmony_ci
4301e41f4b71Sopenharmony_ci| Name  | Type                                              | Mandatory| Description                                |
4302e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------ | ---- | ----------------------------------- |
4303e41f4b71Sopenharmony_ci| type     | string                                           | Yes  | Event type.<br/> **message**: message receiving event.|
4304e41f4b71Sopenharmony_ci| callback | Callback\<[LocalSocketMessageInfo](#localsocketmessageinfo11)\> | No  | Callback passed to the **on** function.|
4305e41f4b71Sopenharmony_ci
4306e41f4b71Sopenharmony_ci**Error codes**
4307e41f4b71Sopenharmony_ci
4308e41f4b71Sopenharmony_ci| ID| Error Message                |
4309e41f4b71Sopenharmony_ci| ------- | ----------------------- |
4310e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
4311e41f4b71Sopenharmony_ci
4312e41f4b71Sopenharmony_ci**Example**
4313e41f4b71Sopenharmony_ci
4314e41f4b71Sopenharmony_ci```ts
4315e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
4316e41f4b71Sopenharmony_ci
4317e41f4b71Sopenharmony_cilet client: socket.LocalSocket = socket.constructLocalSocketInstance();
4318e41f4b71Sopenharmony_cilet messageView = '';
4319e41f4b71Sopenharmony_cilet callback = (value: socket.LocalSocketMessageInfo) => {
4320e41f4b71Sopenharmony_ci  const uintArray = new Uint8Array(value.message)
4321e41f4b71Sopenharmony_ci  let messageView = '';
4322e41f4b71Sopenharmony_ci  for (let i = 0; i < uintArray.length; i++) {
4323e41f4b71Sopenharmony_ci    messageView += String.fromCharCode(uintArray[i]);
4324e41f4b71Sopenharmony_ci  }
4325e41f4b71Sopenharmony_ci  console.log('total: ' + JSON.stringify(value));
4326e41f4b71Sopenharmony_ci  console.log('message infomation: ' + messageView);
4327e41f4b71Sopenharmony_ci}
4328e41f4b71Sopenharmony_ciclient.on('message', callback);
4329e41f4b71Sopenharmony_ciclient.off('message');
4330e41f4b71Sopenharmony_ci```
4331e41f4b71Sopenharmony_ci
4332e41f4b71Sopenharmony_ci### on('connect')<sup>11+</sup>
4333e41f4b71Sopenharmony_ci
4334e41f4b71Sopenharmony_cion(type: 'connect', callback: Callback\<void\>): void;
4335e41f4b71Sopenharmony_ci
4336e41f4b71Sopenharmony_ciSubscribes to **connect** events of a **LocalSocket** object. This API uses an asynchronous callback to return the result.
4337e41f4b71Sopenharmony_ci
4338e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
4339e41f4b71Sopenharmony_ci
4340e41f4b71Sopenharmony_ci**Parameters**
4341e41f4b71Sopenharmony_ci
4342e41f4b71Sopenharmony_ci| Name  | Type            | Mandatory| Description                                                        |
4343e41f4b71Sopenharmony_ci| -------- | ---------------- | ---- | --------------------------------------------------------- |
4344e41f4b71Sopenharmony_ci| type     | string           | Yes  | Event type.<br/>                                            |
4345e41f4b71Sopenharmony_ci| callback | Callback\<void\> | Yes  | Callback used to return the result.                    |
4346e41f4b71Sopenharmony_ci
4347e41f4b71Sopenharmony_ci**Error codes**
4348e41f4b71Sopenharmony_ci
4349e41f4b71Sopenharmony_ci| ID| Error Message                |
4350e41f4b71Sopenharmony_ci| ------- | ----------------------- |
4351e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
4352e41f4b71Sopenharmony_ci
4353e41f4b71Sopenharmony_ci**Example**
4354e41f4b71Sopenharmony_ci
4355e41f4b71Sopenharmony_ci```ts
4356e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
4357e41f4b71Sopenharmony_ci
4358e41f4b71Sopenharmony_cilet client: socket.LocalSocket = socket.constructLocalSocketInstance();
4359e41f4b71Sopenharmony_ciclient.on('connect', () => {
4360e41f4b71Sopenharmony_ci  console.log("on connect success")
4361e41f4b71Sopenharmony_ci});
4362e41f4b71Sopenharmony_ci```
4363e41f4b71Sopenharmony_ci
4364e41f4b71Sopenharmony_ci### off('connect')<sup>11+</sup>
4365e41f4b71Sopenharmony_ci
4366e41f4b71Sopenharmony_cioff(type: 'connect', callback?: Callback\<void\>): void;
4367e41f4b71Sopenharmony_ci
4368e41f4b71Sopenharmony_ciUnsubscribes from **connect** events of a **LocalSocket** object. This API uses an asynchronous callback to return the result.
4369e41f4b71Sopenharmony_ci
4370e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
4371e41f4b71Sopenharmony_ci
4372e41f4b71Sopenharmony_ci**Parameters**
4373e41f4b71Sopenharmony_ci
4374e41f4b71Sopenharmony_ci| Name  | Type            | Mandatory| Description                                                        |
4375e41f4b71Sopenharmony_ci| -------- | ---------------- | ---- | --------------------------------------------------------- |
4376e41f4b71Sopenharmony_ci| type     | string           | Yes  | Event type.<br/>                                            |
4377e41f4b71Sopenharmony_ci| callback | Callback\<void\> | No  | Callback passed to the **on** function.                          |
4378e41f4b71Sopenharmony_ci
4379e41f4b71Sopenharmony_ci**Error codes**
4380e41f4b71Sopenharmony_ci
4381e41f4b71Sopenharmony_ci| ID| Error Message                |
4382e41f4b71Sopenharmony_ci| ------- | ----------------------- |
4383e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
4384e41f4b71Sopenharmony_ci
4385e41f4b71Sopenharmony_ci**Example**
4386e41f4b71Sopenharmony_ci
4387e41f4b71Sopenharmony_ci```ts
4388e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
4389e41f4b71Sopenharmony_ci
4390e41f4b71Sopenharmony_cilet client: socket.LocalSocket = socket.constructLocalSocketInstance();
4391e41f4b71Sopenharmony_cilet callback = () => {
4392e41f4b71Sopenharmony_ci  console.log("on connect success");
4393e41f4b71Sopenharmony_ci}
4394e41f4b71Sopenharmony_ciclient.on('connect', callback);
4395e41f4b71Sopenharmony_ci// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
4396e41f4b71Sopenharmony_ciclient.off('connect', callback);
4397e41f4b71Sopenharmony_ciclient.off('connect');
4398e41f4b71Sopenharmony_ci```
4399e41f4b71Sopenharmony_ci
4400e41f4b71Sopenharmony_ci### on('close')<sup>11+</sup>
4401e41f4b71Sopenharmony_ci
4402e41f4b71Sopenharmony_cion(type: 'close', callback: Callback\<void\>): void;
4403e41f4b71Sopenharmony_ci
4404e41f4b71Sopenharmony_ciSubscribes to **close** events of a **LocalSocket** object. This API uses an asynchronous callback to return the result.
4405e41f4b71Sopenharmony_ci
4406e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
4407e41f4b71Sopenharmony_ci
4408e41f4b71Sopenharmony_ci**Parameters**
4409e41f4b71Sopenharmony_ci
4410e41f4b71Sopenharmony_ci| Name  | Type            | Mandatory| Description                       |
4411e41f4b71Sopenharmony_ci| -------- | ---------------- | ---- | ------------------------ |
4412e41f4b71Sopenharmony_ci| type     | string           | Yes  | Event type.|
4413e41f4b71Sopenharmony_ci| callback | Callback\<void\> | Yes  | Callback used to return the result.|
4414e41f4b71Sopenharmony_ci
4415e41f4b71Sopenharmony_ci**Error codes**
4416e41f4b71Sopenharmony_ci
4417e41f4b71Sopenharmony_ci| ID| Error Message                |
4418e41f4b71Sopenharmony_ci| ------- | ----------------------- |
4419e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
4420e41f4b71Sopenharmony_ci
4421e41f4b71Sopenharmony_ci**Example**
4422e41f4b71Sopenharmony_ci
4423e41f4b71Sopenharmony_ci```ts
4424e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
4425e41f4b71Sopenharmony_ci
4426e41f4b71Sopenharmony_cilet client: socket.LocalSocket = socket.constructLocalSocketInstance();
4427e41f4b71Sopenharmony_cilet callback = () => {
4428e41f4b71Sopenharmony_ci  console.log("on close success");
4429e41f4b71Sopenharmony_ci}
4430e41f4b71Sopenharmony_ciclient.on('close', callback);
4431e41f4b71Sopenharmony_ci```
4432e41f4b71Sopenharmony_ci
4433e41f4b71Sopenharmony_ci### off('close')<sup>11+</sup>
4434e41f4b71Sopenharmony_ci
4435e41f4b71Sopenharmony_cioff(type: 'close', callback?: Callback\<void\>): void;
4436e41f4b71Sopenharmony_ci
4437e41f4b71Sopenharmony_ciUnsubscribes from **close** events of a **LocalSocket** object. This API uses an asynchronous callback to return the result.
4438e41f4b71Sopenharmony_ci
4439e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
4440e41f4b71Sopenharmony_ci
4441e41f4b71Sopenharmony_ci**Parameters**
4442e41f4b71Sopenharmony_ci
4443e41f4b71Sopenharmony_ci| Name  | Type            | Mandatory| Description                       |
4444e41f4b71Sopenharmony_ci| -------- | ---------------- | ---- | ------------------------ |
4445e41f4b71Sopenharmony_ci| type     | string           | Yes  | Event type.|
4446e41f4b71Sopenharmony_ci| callback | Callback\<void\> | No  | Callback passed to the **on** function.|
4447e41f4b71Sopenharmony_ci
4448e41f4b71Sopenharmony_ci**Error codes**
4449e41f4b71Sopenharmony_ci
4450e41f4b71Sopenharmony_ci| ID| Error Message                |
4451e41f4b71Sopenharmony_ci| ------- | ----------------------- |
4452e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
4453e41f4b71Sopenharmony_ci
4454e41f4b71Sopenharmony_ci**Example**
4455e41f4b71Sopenharmony_ci
4456e41f4b71Sopenharmony_ci```ts
4457e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
4458e41f4b71Sopenharmony_ci
4459e41f4b71Sopenharmony_cilet client: socket.LocalSocket = socket.constructLocalSocketInstance();
4460e41f4b71Sopenharmony_cilet callback = () => {
4461e41f4b71Sopenharmony_ci  console.log("on close success");
4462e41f4b71Sopenharmony_ci}
4463e41f4b71Sopenharmony_ciclient.on('close', callback);
4464e41f4b71Sopenharmony_ci// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
4465e41f4b71Sopenharmony_ciclient.off('close', callback);
4466e41f4b71Sopenharmony_ciclient.off('close');
4467e41f4b71Sopenharmony_ci```
4468e41f4b71Sopenharmony_ci
4469e41f4b71Sopenharmony_ci### on('error')<sup>11+</sup>
4470e41f4b71Sopenharmony_ci
4471e41f4b71Sopenharmony_cion(type: 'error', callback: ErrorCallback): void
4472e41f4b71Sopenharmony_ci
4473e41f4b71Sopenharmony_ciSubscribes to **error** events of a **LocalSocket** object. This API uses an asynchronous callback to return the result.
4474e41f4b71Sopenharmony_ci
4475e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
4476e41f4b71Sopenharmony_ci
4477e41f4b71Sopenharmony_ci**Parameters**
4478e41f4b71Sopenharmony_ci
4479e41f4b71Sopenharmony_ci| Name  | Type         | Mandatory| Description                           |
4480e41f4b71Sopenharmony_ci| -------- | ------------- | ---- | ---------------------------- |
4481e41f4b71Sopenharmony_ci| type     | string        | Yes  | Event type.  |
4482e41f4b71Sopenharmony_ci| callback | ErrorCallback | Yes  | Callback used to return the result.|
4483e41f4b71Sopenharmony_ci
4484e41f4b71Sopenharmony_ci**Error codes**
4485e41f4b71Sopenharmony_ci
4486e41f4b71Sopenharmony_ci| ID| Error Message                |
4487e41f4b71Sopenharmony_ci| ------- | ----------------------- |
4488e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
4489e41f4b71Sopenharmony_ci
4490e41f4b71Sopenharmony_ci**Example**
4491e41f4b71Sopenharmony_ci
4492e41f4b71Sopenharmony_ci```ts
4493e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
4494e41f4b71Sopenharmony_ci
4495e41f4b71Sopenharmony_cilet client: socket.LocalSocket = socket.constructLocalSocketInstance();
4496e41f4b71Sopenharmony_ciclient.on('error', (err: Object) => {
4497e41f4b71Sopenharmony_ci  console.log("on error, err:" + JSON.stringify(err))
4498e41f4b71Sopenharmony_ci});
4499e41f4b71Sopenharmony_ci```
4500e41f4b71Sopenharmony_ci
4501e41f4b71Sopenharmony_ci### off('error')<sup>11+</sup>
4502e41f4b71Sopenharmony_ci
4503e41f4b71Sopenharmony_cioff(type: 'error', callback?: ErrorCallback): void;
4504e41f4b71Sopenharmony_ci
4505e41f4b71Sopenharmony_ciUnsubscribes from **error** events of a **LocalSocket** object. This API uses an asynchronous callback to return the result.
4506e41f4b71Sopenharmony_ci
4507e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
4508e41f4b71Sopenharmony_ci
4509e41f4b71Sopenharmony_ci**Parameters**
4510e41f4b71Sopenharmony_ci
4511e41f4b71Sopenharmony_ci| Name  | Type         | Mandatory| Description                            |
4512e41f4b71Sopenharmony_ci| -------- | ------------- | ---- | ----------------------------- |
4513e41f4b71Sopenharmony_ci| type     | string        | Yes  | Event type.|
4514e41f4b71Sopenharmony_ci| callback | ErrorCallback | No  | Callback passed to the **on** function.|
4515e41f4b71Sopenharmony_ci
4516e41f4b71Sopenharmony_ci**Error codes**
4517e41f4b71Sopenharmony_ci
4518e41f4b71Sopenharmony_ci| ID| Error Message                |
4519e41f4b71Sopenharmony_ci| ------- | ----------------------- |
4520e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
4521e41f4b71Sopenharmony_ci
4522e41f4b71Sopenharmony_ci**Example**
4523e41f4b71Sopenharmony_ci
4524e41f4b71Sopenharmony_ci```ts
4525e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
4526e41f4b71Sopenharmony_ci
4527e41f4b71Sopenharmony_cilet client: socket.LocalSocket = socket.constructLocalSocketInstance();
4528e41f4b71Sopenharmony_cilet callback = (err: Object) => {
4529e41f4b71Sopenharmony_ci  console.log("on error, err:" + JSON.stringify(err));
4530e41f4b71Sopenharmony_ci}
4531e41f4b71Sopenharmony_ciclient.on('error', callback);
4532e41f4b71Sopenharmony_ci// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
4533e41f4b71Sopenharmony_ciclient.off('error', callback);
4534e41f4b71Sopenharmony_ciclient.off('error');
4535e41f4b71Sopenharmony_ci```
4536e41f4b71Sopenharmony_ci
4537e41f4b71Sopenharmony_ci## LocalSocketMessageInfo<sup>11+</sup>
4538e41f4b71Sopenharmony_ci
4539e41f4b71Sopenharmony_ciDefines the data received by the client over a local socket connection.
4540e41f4b71Sopenharmony_ci
4541e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
4542e41f4b71Sopenharmony_ci
4543e41f4b71Sopenharmony_ci| Name    | Type           | Mandatory| Description              |
4544e41f4b71Sopenharmony_ci| ------- | --------------- | --- | ------------------ |
4545e41f4b71Sopenharmony_ci| message | ArrayBuffer     | Yes  | Data received.    |
4546e41f4b71Sopenharmony_ci| address | string          | Yes  | Local socket connection address.|
4547e41f4b71Sopenharmony_ci| size    | number          | Yes  | Data length.         |
4548e41f4b71Sopenharmony_ci
4549e41f4b71Sopenharmony_ci## LocalAddress<sup>11+</sup>
4550e41f4b71Sopenharmony_ci
4551e41f4b71Sopenharmony_ciDefines the address of a local socket file. When the address is passed for binding, a socket file is created at this address.
4552e41f4b71Sopenharmony_ci
4553e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
4554e41f4b71Sopenharmony_ci
4555e41f4b71Sopenharmony_ci| Name    | Type      | Mandatory| Description              |
4556e41f4b71Sopenharmony_ci| ------- | ---------- | --- | ------------------ |
4557e41f4b71Sopenharmony_ci| address | string     | Yes  | Address of the local socket file.    |
4558e41f4b71Sopenharmony_ci
4559e41f4b71Sopenharmony_ci## LocalConnectOptions<sup>11+</sup>
4560e41f4b71Sopenharmony_ci
4561e41f4b71Sopenharmony_ciDefines local socket connection parameters.
4562e41f4b71Sopenharmony_ci
4563e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
4564e41f4b71Sopenharmony_ci
4565e41f4b71Sopenharmony_ci| Name    | Type      | Mandatory| Description                           |
4566e41f4b71Sopenharmony_ci| ------- | ---------- | --- | ------------------------------ |
4567e41f4b71Sopenharmony_ci| address | [LocalAddress](#localaddress11)    | Yes  | Address of the local socket file.           |
4568e41f4b71Sopenharmony_ci| timeout | number     | No  | Timeout duration of the local socket connection, in ms. |
4569e41f4b71Sopenharmony_ci
4570e41f4b71Sopenharmony_ci## LocalSendOptions<sup>11+</sup>
4571e41f4b71Sopenharmony_ci
4572e41f4b71Sopenharmony_ciDefines the parameters for sending data over a local socket connection.
4573e41f4b71Sopenharmony_ci
4574e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
4575e41f4b71Sopenharmony_ci
4576e41f4b71Sopenharmony_ci| Name    | Type      | Mandatory| Description                |
4577e41f4b71Sopenharmony_ci| ------- | ---------- | --- | ------------------- |
4578e41f4b71Sopenharmony_ci| data    | string \| ArrayBuffer | Yes  | Data to be transmitted.|
4579e41f4b71Sopenharmony_ci| encoding | string   | No  | Encoding format of the string. |
4580e41f4b71Sopenharmony_ci
4581e41f4b71Sopenharmony_ci## ExtraOptionsBase<sup>7+</sup>
4582e41f4b71Sopenharmony_ci
4583e41f4b71Sopenharmony_ciDefines other properties of the local socket connection.
4584e41f4b71Sopenharmony_ci
4585e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
4586e41f4b71Sopenharmony_ci
4587e41f4b71Sopenharmony_ci| Name           | Type   | Mandatory| Description                             |
4588e41f4b71Sopenharmony_ci| ----------------- | ------- | ---- | ----------------------------- |
4589e41f4b71Sopenharmony_ci| receiveBufferSize | number  | No  | Size of the receive buffer, in bytes.    |
4590e41f4b71Sopenharmony_ci| sendBufferSize    | number  | No  | Size of the send buffer, in bytes.    |
4591e41f4b71Sopenharmony_ci| reuseAddress      | boolean | No  | Whether to reuse addresses.                  |
4592e41f4b71Sopenharmony_ci| socketTimeout     | number  | No  | Timeout duration of the local socket connection, in ms.   |
4593e41f4b71Sopenharmony_ci
4594e41f4b71Sopenharmony_ci## socket.constructLocalSocketServerInstance<sup>11+</sup>
4595e41f4b71Sopenharmony_ci
4596e41f4b71Sopenharmony_ciconstructLocalSocketServerInstance(): LocalSocketServer
4597e41f4b71Sopenharmony_ci
4598e41f4b71Sopenharmony_ciCreates a **LocalSocketServer** object.
4599e41f4b71Sopenharmony_ci
4600e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
4601e41f4b71Sopenharmony_ci
4602e41f4b71Sopenharmony_ci**Return value**
4603e41f4b71Sopenharmony_ci
4604e41f4b71Sopenharmony_ci| Type                               | Description                         |
4605e41f4b71Sopenharmony_ci| :---------------------------------- | :---------------------------- |
4606e41f4b71Sopenharmony_ci| [LocalSocketServer](#localsocketserver11) | **LocalSocketServer** object.|
4607e41f4b71Sopenharmony_ci
4608e41f4b71Sopenharmony_ci**Example**
4609e41f4b71Sopenharmony_ci
4610e41f4b71Sopenharmony_ci```ts
4611e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
4612e41f4b71Sopenharmony_cilet server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance();
4613e41f4b71Sopenharmony_ci```
4614e41f4b71Sopenharmony_ci
4615e41f4b71Sopenharmony_ci## LocalSocketServer<sup>11+</sup>
4616e41f4b71Sopenharmony_ci
4617e41f4b71Sopenharmony_ciDefines a local socket server connection. Before calling LocalSocketServer APIs, you need to call [socket.constructLocalSocketServerInstance](#socketconstructlocalsocketserverinstance11) to create a **LocalSocketServer** object. 
4618e41f4b71Sopenharmony_ci
4619e41f4b71Sopenharmony_ci### listen<sup>11+</sup>
4620e41f4b71Sopenharmony_ci
4621e41f4b71Sopenharmony_cilisten(address: LocalAddress): Promise\<void\>
4622e41f4b71Sopenharmony_ci
4623e41f4b71Sopenharmony_ciBinds the address of the local socket file. The server listens to and accepts local socket connections established over the socket. Multiple threads are used to process client data concurrently. This API uses a promise to return the result.
4624e41f4b71Sopenharmony_ci
4625e41f4b71Sopenharmony_ci> **NOTE**
4626e41f4b71Sopenharmony_ci> The server uses this API to complete the **bind**, **listen**, and **accept** operations. If the address of the local socket file is passed for binding, a socket file is automatically created when this API is called.
4627e41f4b71Sopenharmony_ci
4628e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
4629e41f4b71Sopenharmony_ci
4630e41f4b71Sopenharmony_ci**Parameters**
4631e41f4b71Sopenharmony_ci
4632e41f4b71Sopenharmony_ci| Name | Type                     | Mandatory| Description                                         |
4633e41f4b71Sopenharmony_ci| ------- | ------------------------- | ---- | --------------------------------------------- |
4634e41f4b71Sopenharmony_ci| address | [LocalAddress](#localaddress11) | Yes  | Destination address.|
4635e41f4b71Sopenharmony_ci
4636e41f4b71Sopenharmony_ci**Return value**
4637e41f4b71Sopenharmony_ci
4638e41f4b71Sopenharmony_ci| Type           | Description                                                  |
4639e41f4b71Sopenharmony_ci| :-------------- | :---------------------------------------------------- |
4640e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
4641e41f4b71Sopenharmony_ci
4642e41f4b71Sopenharmony_ci**Error codes**
4643e41f4b71Sopenharmony_ci
4644e41f4b71Sopenharmony_ci| ID| Error Message                     |
4645e41f4b71Sopenharmony_ci| -------- | --------------------------- |
4646e41f4b71Sopenharmony_ci| 401      | Parameter error.            |
4647e41f4b71Sopenharmony_ci| 2303109  | Bad file number.            |
4648e41f4b71Sopenharmony_ci| 2301013  | Insufficient permissions.   |
4649e41f4b71Sopenharmony_ci| 2301022  | Invalid argument.           |
4650e41f4b71Sopenharmony_ci| 2301098  | Address already in use.     |
4651e41f4b71Sopenharmony_ci
4652e41f4b71Sopenharmony_ci**Example**
4653e41f4b71Sopenharmony_ci
4654e41f4b71Sopenharmony_ci```ts
4655e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
4656e41f4b71Sopenharmony_ci
4657e41f4b71Sopenharmony_cilet server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance();
4658e41f4b71Sopenharmony_cilet sandboxPath: string = getContext().filesDir + '/testSocket'
4659e41f4b71Sopenharmony_cilet addr: socket.LocalAddress = {
4660e41f4b71Sopenharmony_ci  address: sandboxPath
4661e41f4b71Sopenharmony_ci}
4662e41f4b71Sopenharmony_ciserver.listen(addr).then(() => {
4663e41f4b71Sopenharmony_ci  console.log('listen success');
4664e41f4b71Sopenharmony_ci}).catch((err: Object) => {
4665e41f4b71Sopenharmony_ci  console.error('listen fail: ' + JSON.stringify(err));
4666e41f4b71Sopenharmony_ci});
4667e41f4b71Sopenharmony_ci```
4668e41f4b71Sopenharmony_ci
4669e41f4b71Sopenharmony_ci### getState<sup>11+</sup>
4670e41f4b71Sopenharmony_ci
4671e41f4b71Sopenharmony_cigetState(): Promise\<SocketStateBase\>
4672e41f4b71Sopenharmony_ci
4673e41f4b71Sopenharmony_ciObtains the status of a local socket server connection. This API uses a promise to return the result.
4674e41f4b71Sopenharmony_ci
4675e41f4b71Sopenharmony_ci> **NOTE**
4676e41f4b71Sopenharmony_ci> This API can be called only after **listen** is successfully called.
4677e41f4b71Sopenharmony_ci
4678e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
4679e41f4b71Sopenharmony_ci
4680e41f4b71Sopenharmony_ci**Return value**
4681e41f4b71Sopenharmony_ci
4682e41f4b71Sopenharmony_ci| Type                                        | Description                                           |
4683e41f4b71Sopenharmony_ci| :------------------------------------------- | :--------------------------------------------- |
4684e41f4b71Sopenharmony_ci| Promise\<[SocketStateBase](#socketstatebase)\> | Promise used to return the result.|
4685e41f4b71Sopenharmony_ci
4686e41f4b71Sopenharmony_ci**Example**
4687e41f4b71Sopenharmony_ci
4688e41f4b71Sopenharmony_ci```ts
4689e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
4690e41f4b71Sopenharmony_ci
4691e41f4b71Sopenharmony_cilet server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance();
4692e41f4b71Sopenharmony_cilet sandboxPath: string = getContext().filesDir + '/testSocket'
4693e41f4b71Sopenharmony_cilet listenAddr: socket.LocalAddress = {
4694e41f4b71Sopenharmony_ci  address: sandboxPath
4695e41f4b71Sopenharmony_ci}
4696e41f4b71Sopenharmony_ciserver.listen(listenAddr).then(() => {
4697e41f4b71Sopenharmony_ci  console.log("listen success");
4698e41f4b71Sopenharmony_ci}).catch((err: Object) => {
4699e41f4b71Sopenharmony_ci  console.error("listen fail: " + JSON.stringify(err));
4700e41f4b71Sopenharmony_ci})
4701e41f4b71Sopenharmony_ciserver.getState().then((data: socket.SocketStateBase) => {
4702e41f4b71Sopenharmony_ci  console.log('getState success: ' + JSON.stringify(data));
4703e41f4b71Sopenharmony_ci}).catch((err: Object) => {
4704e41f4b71Sopenharmony_ci  console.error('getState fail: ' + JSON.stringify(err));
4705e41f4b71Sopenharmony_ci});
4706e41f4b71Sopenharmony_ci```
4707e41f4b71Sopenharmony_ci
4708e41f4b71Sopenharmony_ci### setExtraOptions<sup>11+</sup>
4709e41f4b71Sopenharmony_ci
4710e41f4b71Sopenharmony_cisetExtraOptions(options: ExtraOptionsBase): Promise\<void\>
4711e41f4b71Sopenharmony_ci
4712e41f4b71Sopenharmony_ciSets other properties of the local socket server connection. This API uses a promise to return the result.
4713e41f4b71Sopenharmony_ci
4714e41f4b71Sopenharmony_ci> **NOTE**
4715e41f4b71Sopenharmony_ci> This API can be called only after **listen** is successfully called.
4716e41f4b71Sopenharmony_ci
4717e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
4718e41f4b71Sopenharmony_ci
4719e41f4b71Sopenharmony_ci**Parameters**
4720e41f4b71Sopenharmony_ci
4721e41f4b71Sopenharmony_ci| Name | Type                                     | Mandatory| Description                           |
4722e41f4b71Sopenharmony_ci| ------- | --------------------------------------- | ---- | ------------------------------ |
4723e41f4b71Sopenharmony_ci| options | [ExtraOptionsBase](#extraoptionsbase7) | Yes  | Other properties of a local socket server connection.|
4724e41f4b71Sopenharmony_ci
4725e41f4b71Sopenharmony_ci**Return value**
4726e41f4b71Sopenharmony_ci
4727e41f4b71Sopenharmony_ci| Type           | Description                                            |
4728e41f4b71Sopenharmony_ci| :-------------- | :---------------------------------------------- |
4729e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
4730e41f4b71Sopenharmony_ci
4731e41f4b71Sopenharmony_ci**Error codes**
4732e41f4b71Sopenharmony_ci
4733e41f4b71Sopenharmony_ci| ID| Error Message                       |
4734e41f4b71Sopenharmony_ci| -------- | ------------------------------- |
4735e41f4b71Sopenharmony_ci| 401      | Parameter error.                |
4736e41f4b71Sopenharmony_ci| 2301009  | Bad file descriptor.            |
4737e41f4b71Sopenharmony_ci
4738e41f4b71Sopenharmony_ci**Example**
4739e41f4b71Sopenharmony_ci
4740e41f4b71Sopenharmony_ci```ts
4741e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
4742e41f4b71Sopenharmony_ci
4743e41f4b71Sopenharmony_cilet server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance();
4744e41f4b71Sopenharmony_cilet sandboxPath: string = getContext().filesDir + '/testSocket'
4745e41f4b71Sopenharmony_cilet listenAddr: socket.NetAddress = {
4746e41f4b71Sopenharmony_ci  address: sandboxPath
4747e41f4b71Sopenharmony_ci}
4748e41f4b71Sopenharmony_ciserver.listen(listenAddr).then(() => {
4749e41f4b71Sopenharmony_ci  console.log("listen success");
4750e41f4b71Sopenharmony_ci}).catch((err: Object) => {
4751e41f4b71Sopenharmony_ci  console.error("listen fail: " + JSON.stringify(err));
4752e41f4b71Sopenharmony_ci})
4753e41f4b71Sopenharmony_ci
4754e41f4b71Sopenharmony_cilet options: socket.ExtraOptionsBase = {
4755e41f4b71Sopenharmony_ci  receiveBufferSize: 6000,
4756e41f4b71Sopenharmony_ci  sendBufferSize: 6000,
4757e41f4b71Sopenharmony_ci  socketTimeout: 3000
4758e41f4b71Sopenharmony_ci}
4759e41f4b71Sopenharmony_ciserver.setExtraOptions(options).then(() => {
4760e41f4b71Sopenharmony_ci  console.log('setExtraOptions success');
4761e41f4b71Sopenharmony_ci}).catch((err: Object) => {
4762e41f4b71Sopenharmony_ci  console.error('setExtraOptions fail: ' + JSON.stringify(err));
4763e41f4b71Sopenharmony_ci});
4764e41f4b71Sopenharmony_ci```
4765e41f4b71Sopenharmony_ci
4766e41f4b71Sopenharmony_ci### getExtraOptions<sup>11+</sup>
4767e41f4b71Sopenharmony_ci
4768e41f4b71Sopenharmony_cigetExtraOptions(): Promise\<ExtraOptionsBase\>;
4769e41f4b71Sopenharmony_ci
4770e41f4b71Sopenharmony_ciObtains other properties of a local socket server connection. This API uses a promise to return the result.
4771e41f4b71Sopenharmony_ci
4772e41f4b71Sopenharmony_ci> **NOTE**
4773e41f4b71Sopenharmony_ci> This API can be called only after **listen** is successfully called.
4774e41f4b71Sopenharmony_ci
4775e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
4776e41f4b71Sopenharmony_ci
4777e41f4b71Sopenharmony_ci**Return value**
4778e41f4b71Sopenharmony_ci
4779e41f4b71Sopenharmony_ci| Type                        | Description                       |
4780e41f4b71Sopenharmony_ci| :-------------------------- | :-------------------------- |
4781e41f4b71Sopenharmony_ci| Promise\<[ExtraOptionsBase](#extraoptionsbase7)\> | Promise used to return the result.|
4782e41f4b71Sopenharmony_ci
4783e41f4b71Sopenharmony_ci**Error codes**
4784e41f4b71Sopenharmony_ci
4785e41f4b71Sopenharmony_ci| ID| Error Message              |
4786e41f4b71Sopenharmony_ci| -------- | -------------------- |
4787e41f4b71Sopenharmony_ci| 401     | Parameter error. |
4788e41f4b71Sopenharmony_ci
4789e41f4b71Sopenharmony_ci**Example**
4790e41f4b71Sopenharmony_ci
4791e41f4b71Sopenharmony_ci```ts
4792e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
4793e41f4b71Sopenharmony_ci
4794e41f4b71Sopenharmony_cilet server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance();
4795e41f4b71Sopenharmony_cilet sandboxPath: string = getContext().filesDir + '/testSocket'
4796e41f4b71Sopenharmony_cilet listenAddr: socket.LocalAddress = {
4797e41f4b71Sopenharmony_ci  address: sandboxPath
4798e41f4b71Sopenharmony_ci}
4799e41f4b71Sopenharmony_ciserver.listen(listenAddr).then(() => {
4800e41f4b71Sopenharmony_ci  console.log("listen success");
4801e41f4b71Sopenharmony_ci}).catch((err: Object) => {
4802e41f4b71Sopenharmony_ci  console.error("listen fail: " + JSON.stringify(err));
4803e41f4b71Sopenharmony_ci})
4804e41f4b71Sopenharmony_ciserver.getExtraOptions().then((options: socket.ExtraOptionsBase) => {
4805e41f4b71Sopenharmony_ci  console.log('options: ' + JSON.stringify(options));
4806e41f4b71Sopenharmony_ci}).catch((err: Object) => {
4807e41f4b71Sopenharmony_ci  console.error('getExtraOptions fail: ' + JSON.stringify(err));
4808e41f4b71Sopenharmony_ci});
4809e41f4b71Sopenharmony_ci```
4810e41f4b71Sopenharmony_ci
4811e41f4b71Sopenharmony_ci### getLocalAddress<sup>12+</sup>
4812e41f4b71Sopenharmony_ci
4813e41f4b71Sopenharmony_cigetLocalAddress(): Promise\<string\>
4814e41f4b71Sopenharmony_ci
4815e41f4b71Sopenharmony_ciObtains the local socket address of a **LocalSocketServer** connection. This API uses a promise to return the result.
4816e41f4b71Sopenharmony_ci
4817e41f4b71Sopenharmony_ci> **NOTE**
4818e41f4b71Sopenharmony_ci> This API can be called only after **listen** is successfully called.
4819e41f4b71Sopenharmony_ci
4820e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
4821e41f4b71Sopenharmony_ci
4822e41f4b71Sopenharmony_ci**Return value**
4823e41f4b71Sopenharmony_ci
4824e41f4b71Sopenharmony_ci| Type           | Description                                                |
4825e41f4b71Sopenharmony_ci|  -------------- |  --------------------------------------------------- |
4826e41f4b71Sopenharmony_ci| Promise\<string\> | Promise used to return the result.|
4827e41f4b71Sopenharmony_ci
4828e41f4b71Sopenharmony_ci**Error codes**
4829e41f4b71Sopenharmony_ci
4830e41f4b71Sopenharmony_ci| ID| Error Message                                   |
4831e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- |
4832e41f4b71Sopenharmony_ci| 2300002  | System internal error.                      |
4833e41f4b71Sopenharmony_ci| 2301009  | Bad file descriptor.                            |
4834e41f4b71Sopenharmony_ci| 2303188  | Socket operation on non-socket. |
4835e41f4b71Sopenharmony_ci
4836e41f4b71Sopenharmony_ci**Example**
4837e41f4b71Sopenharmony_ci
4838e41f4b71Sopenharmony_ci```ts
4839e41f4b71Sopenharmony_cilet server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance();
4840e41f4b71Sopenharmony_cilet sandboxPath: string = getContext().filesDir + '/testSocket';
4841e41f4b71Sopenharmony_cilet listenAddr: socket.LocalAddress = {
4842e41f4b71Sopenharmony_ci  address: sandboxPath
4843e41f4b71Sopenharmony_ci}
4844e41f4b71Sopenharmony_ciserver.listen(listenAddr).then(() => {
4845e41f4b71Sopenharmony_ci  console.info("listen success");
4846e41f4b71Sopenharmony_ci  server.getLocalAddress().then((localPath: string) => {
4847e41f4b71Sopenharmony_ci    console.info("SUCCESS " + JSON.stringify(localPath));
4848e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
4849e41f4b71Sopenharmony_ci    console.error("FAIL " + JSON.stringify(err));
4850e41f4b71Sopenharmony_ci  })
4851e41f4b71Sopenharmony_ci}).catch((err: Object) => {
4852e41f4b71Sopenharmony_ci  console.error("listen fail: " + JSON.stringify(err));
4853e41f4b71Sopenharmony_ci})
4854e41f4b71Sopenharmony_ci
4855e41f4b71Sopenharmony_ci```
4856e41f4b71Sopenharmony_ci
4857e41f4b71Sopenharmony_ci### on('connect')<sup>11+</sup>
4858e41f4b71Sopenharmony_ci
4859e41f4b71Sopenharmony_cion(type: 'connect', callback: Callback\<LocalSocketConnection\>): void
4860e41f4b71Sopenharmony_ci
4861e41f4b71Sopenharmony_ciSubscribes to **connect** events of a **LocalSocketServer** object. This API uses an asynchronous callback to return the result.
4862e41f4b71Sopenharmony_ci
4863e41f4b71Sopenharmony_ci> **NOTE**
4864e41f4b71Sopenharmony_ci> This API can be called only after **listen** is successfully called.
4865e41f4b71Sopenharmony_ci
4866e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
4867e41f4b71Sopenharmony_ci
4868e41f4b71Sopenharmony_ci**Parameters**
4869e41f4b71Sopenharmony_ci
4870e41f4b71Sopenharmony_ci| Name  | Type                           | Mandatory| Description                                 |
4871e41f4b71Sopenharmony_ci| -------- | ------------------------------- | ---- | ------------------------------------- |
4872e41f4b71Sopenharmony_ci| type     | string                          | Yes  | Event type.<br/> **connect**: connection event.|
4873e41f4b71Sopenharmony_ci| callback | Callback\<[LocalSocketConnection](#localsocketconnection11)\> | Yes  | Callback used to return the result.|
4874e41f4b71Sopenharmony_ci
4875e41f4b71Sopenharmony_ci**Error codes**
4876e41f4b71Sopenharmony_ci
4877e41f4b71Sopenharmony_ci| ID| Error Message        |
4878e41f4b71Sopenharmony_ci| -------- | ---------------- |
4879e41f4b71Sopenharmony_ci| 401      | Parameter error. |
4880e41f4b71Sopenharmony_ci
4881e41f4b71Sopenharmony_ci**Example**
4882e41f4b71Sopenharmony_ci
4883e41f4b71Sopenharmony_ci```ts
4884e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
4885e41f4b71Sopenharmony_ci
4886e41f4b71Sopenharmony_cilet server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance();
4887e41f4b71Sopenharmony_ciserver.on('connect', (connection: socket.LocalSocketConnection) => {
4888e41f4b71Sopenharmony_ci  if (connection) {
4889e41f4b71Sopenharmony_ci    console.log('accept a client')
4890e41f4b71Sopenharmony_ci  }
4891e41f4b71Sopenharmony_ci});
4892e41f4b71Sopenharmony_ci```
4893e41f4b71Sopenharmony_ci
4894e41f4b71Sopenharmony_ci### off('connect')<sup>11+</sup>
4895e41f4b71Sopenharmony_ci
4896e41f4b71Sopenharmony_cioff(type: 'connect', callback?: Callback\<LocalSocketConnection\>): void
4897e41f4b71Sopenharmony_ci
4898e41f4b71Sopenharmony_ciUnsubscribes from **connect** events of a **LocalSocketServer** object. This API uses an asynchronous callback to return the result.
4899e41f4b71Sopenharmony_ci
4900e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
4901e41f4b71Sopenharmony_ci
4902e41f4b71Sopenharmony_ci**Parameters**
4903e41f4b71Sopenharmony_ci
4904e41f4b71Sopenharmony_ci| Name  | Type                           | Mandatory| Description                                 |
4905e41f4b71Sopenharmony_ci| -------- | ------------------------------- | ---- | ------------------------------------- |
4906e41f4b71Sopenharmony_ci| type     | string                          | Yes  | Event type.<br/> **connect**: connection event.|
4907e41f4b71Sopenharmony_ci| callback | Callback\<[LocalSocketConnection](#localsocketconnection11)\> | No  | Callback passed to the **on** function.|
4908e41f4b71Sopenharmony_ci
4909e41f4b71Sopenharmony_ci**Error codes**
4910e41f4b71Sopenharmony_ci
4911e41f4b71Sopenharmony_ci| ID| Error Message        |
4912e41f4b71Sopenharmony_ci| -------- | ---------------- |
4913e41f4b71Sopenharmony_ci| 401      | Parameter error. |
4914e41f4b71Sopenharmony_ci
4915e41f4b71Sopenharmony_ci**Example**
4916e41f4b71Sopenharmony_ci
4917e41f4b71Sopenharmony_ci```ts
4918e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
4919e41f4b71Sopenharmony_ci
4920e41f4b71Sopenharmony_cilet server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance();
4921e41f4b71Sopenharmony_cilet callback = (connection: socket.LocalSocketConnection) => {
4922e41f4b71Sopenharmony_ci  if (connection) {
4923e41f4b71Sopenharmony_ci    console.log('accept a client')
4924e41f4b71Sopenharmony_ci  }
4925e41f4b71Sopenharmony_ci}
4926e41f4b71Sopenharmony_ciserver.on('connect', callback);
4927e41f4b71Sopenharmony_ci// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
4928e41f4b71Sopenharmony_ciserver.off('connect', callback);
4929e41f4b71Sopenharmony_ciserver.off('connect');
4930e41f4b71Sopenharmony_ci```
4931e41f4b71Sopenharmony_ci
4932e41f4b71Sopenharmony_ci### on('error')<sup>11+</sup>
4933e41f4b71Sopenharmony_ci
4934e41f4b71Sopenharmony_cion(type: 'error', callback: ErrorCallback): void
4935e41f4b71Sopenharmony_ci
4936e41f4b71Sopenharmony_ciSubscribes to **error** events of a **LocalSocketServer** object. This API uses an asynchronous callback to return the result.
4937e41f4b71Sopenharmony_ci
4938e41f4b71Sopenharmony_ci> **NOTE**
4939e41f4b71Sopenharmony_ci> This API can be called only after **listen** is successfully called.
4940e41f4b71Sopenharmony_ci
4941e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
4942e41f4b71Sopenharmony_ci
4943e41f4b71Sopenharmony_ci**Parameters**
4944e41f4b71Sopenharmony_ci
4945e41f4b71Sopenharmony_ci| Name  | Type         | Mandatory| Description                                |
4946e41f4b71Sopenharmony_ci| -------- | ------------- | ---- | ------------------------------------ |
4947e41f4b71Sopenharmony_ci| type     | string        | Yes  | Event type.<br/> **error**: error event.|
4948e41f4b71Sopenharmony_ci| callback | ErrorCallback | Yes  | Callback used to return the result.|
4949e41f4b71Sopenharmony_ci
4950e41f4b71Sopenharmony_ci**Error codes**
4951e41f4b71Sopenharmony_ci
4952e41f4b71Sopenharmony_ci| ID| Error Message        |
4953e41f4b71Sopenharmony_ci| -------- | ---------------- |
4954e41f4b71Sopenharmony_ci| 401      | Parameter error. |
4955e41f4b71Sopenharmony_ci
4956e41f4b71Sopenharmony_ci**Example**
4957e41f4b71Sopenharmony_ci
4958e41f4b71Sopenharmony_ci```ts
4959e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
4960e41f4b71Sopenharmony_ci
4961e41f4b71Sopenharmony_cilet server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance();
4962e41f4b71Sopenharmony_ciserver.on('error', (err: Object) => {
4963e41f4b71Sopenharmony_ci  console.error("on error, err:" + JSON.stringify(err))
4964e41f4b71Sopenharmony_ci});
4965e41f4b71Sopenharmony_ci```
4966e41f4b71Sopenharmony_ci
4967e41f4b71Sopenharmony_ci### off('error')<sup>11+</sup>
4968e41f4b71Sopenharmony_ci
4969e41f4b71Sopenharmony_cioff(type: 'error', callback?: ErrorCallback): void
4970e41f4b71Sopenharmony_ci
4971e41f4b71Sopenharmony_ciUnsubscribes from **error** events of a **LocalSocketServer** object. This API uses an asynchronous callback to return the result.
4972e41f4b71Sopenharmony_ci
4973e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
4974e41f4b71Sopenharmony_ci
4975e41f4b71Sopenharmony_ci**Parameters**
4976e41f4b71Sopenharmony_ci
4977e41f4b71Sopenharmony_ci| Name  | Type         | Mandatory| Description                                |
4978e41f4b71Sopenharmony_ci| -------- | ------------- | ---- | ------------------------------------ |
4979e41f4b71Sopenharmony_ci| type     | string        | Yes  | Event type.<br/> **error**: error event.|
4980e41f4b71Sopenharmony_ci| callback | ErrorCallback | No  | Callback passed to the **on** function.  |
4981e41f4b71Sopenharmony_ci
4982e41f4b71Sopenharmony_ci**Error codes**
4983e41f4b71Sopenharmony_ci
4984e41f4b71Sopenharmony_ci| ID| Error Message        |
4985e41f4b71Sopenharmony_ci| -------- | ---------------- |
4986e41f4b71Sopenharmony_ci| 401      | Parameter error. |
4987e41f4b71Sopenharmony_ci
4988e41f4b71Sopenharmony_ci**Example**
4989e41f4b71Sopenharmony_ci
4990e41f4b71Sopenharmony_ci```ts
4991e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
4992e41f4b71Sopenharmony_ci
4993e41f4b71Sopenharmony_cilet server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance();
4994e41f4b71Sopenharmony_cilet callback = (err: Object) => {
4995e41f4b71Sopenharmony_ci  console.error("on error, err:" + JSON.stringify(err));
4996e41f4b71Sopenharmony_ci}
4997e41f4b71Sopenharmony_ciserver.on('error', callback);
4998e41f4b71Sopenharmony_ci// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
4999e41f4b71Sopenharmony_ciserver.off('error', callback);
5000e41f4b71Sopenharmony_ciserver.off('error');
5001e41f4b71Sopenharmony_ci```
5002e41f4b71Sopenharmony_ci
5003e41f4b71Sopenharmony_ci
5004e41f4b71Sopenharmony_ci## LocalSocketConnection<sup>11+</sup>
5005e41f4b71Sopenharmony_ci
5006e41f4b71Sopenharmony_ciDefines a local socket connection, that is, the session between the local socket client and the server. Before calling LocalSocketConnection APIs, you need to obtain a **LocalSocketConnection** object.
5007e41f4b71Sopenharmony_ci
5008e41f4b71Sopenharmony_ci> **NOTE**
5009e41f4b71Sopenharmony_ci> The LocalSocketConnection client can call related APIs through the **LocalSocketConnection** object only after a connection is successfully established between the local socket client and the server.
5010e41f4b71Sopenharmony_ci
5011e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
5012e41f4b71Sopenharmony_ci
5013e41f4b71Sopenharmony_ci### Attributes
5014e41f4b71Sopenharmony_ci
5015e41f4b71Sopenharmony_ci| Name    | Type  | Mandatory| Description                           |
5016e41f4b71Sopenharmony_ci| -------- | ------ | ---- | ---------------------------- |
5017e41f4b71Sopenharmony_ci| clientId | number | Yes  | ID of the session between the client and the server.|
5018e41f4b71Sopenharmony_ci
5019e41f4b71Sopenharmony_ci### send<sup>11+</sup>
5020e41f4b71Sopenharmony_ci
5021e41f4b71Sopenharmony_cisend(options: LocalSendOptions): Promise\<void\>
5022e41f4b71Sopenharmony_ci
5023e41f4b71Sopenharmony_ciSends data through a local socket connection. This API uses a promise to return the result.
5024e41f4b71Sopenharmony_ci
5025e41f4b71Sopenharmony_ci> **NOTE**
5026e41f4b71Sopenharmony_ci> This API can be used only after the server obtains a **LocalSocketConnection** object through the **callback** of the **connect** event.
5027e41f4b71Sopenharmony_ci
5028e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
5029e41f4b71Sopenharmony_ci
5030e41f4b71Sopenharmony_ci**Parameters**
5031e41f4b71Sopenharmony_ci
5032e41f4b71Sopenharmony_ci| Name | Type                             | Mandatory| Description                                                        |
5033e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | -------------------------------------- |
5034e41f4b71Sopenharmony_ci| options | [LocalSendOptions](#localsendoptions11) | Yes  | Defines the parameters for sending data over a local socket connection.|
5035e41f4b71Sopenharmony_ci
5036e41f4b71Sopenharmony_ci**Return value**
5037e41f4b71Sopenharmony_ci
5038e41f4b71Sopenharmony_ci| Type           | Description                                            |
5039e41f4b71Sopenharmony_ci| :-------------- | :---------------------------------------------- |
5040e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
5041e41f4b71Sopenharmony_ci
5042e41f4b71Sopenharmony_ci**Error codes**
5043e41f4b71Sopenharmony_ci
5044e41f4b71Sopenharmony_ci| ID| Error Message              |
5045e41f4b71Sopenharmony_ci| -------- | ---------------------- |
5046e41f4b71Sopenharmony_ci| 401      | Parameter error.       |
5047e41f4b71Sopenharmony_ci| 2301011  | Operation would block. |
5048e41f4b71Sopenharmony_ci
5049e41f4b71Sopenharmony_ci**Example**
5050e41f4b71Sopenharmony_ci
5051e41f4b71Sopenharmony_ci```ts
5052e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
5053e41f4b71Sopenharmony_ci
5054e41f4b71Sopenharmony_cilet server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance();
5055e41f4b71Sopenharmony_ci
5056e41f4b71Sopenharmony_ciserver.on('connect', (connection: socket.LocalSocketConnection) => {
5057e41f4b71Sopenharmony_ci  let sendOptions: socket.LocalSendOptions = {
5058e41f4b71Sopenharmony_ci    data: 'Hello, client!'
5059e41f4b71Sopenharmony_ci  }
5060e41f4b71Sopenharmony_ci  connection.send(sendOptions).then(() => {
5061e41f4b71Sopenharmony_ci    console.log('send success');
5062e41f4b71Sopenharmony_ci  }).catch((err: Object) => {
5063e41f4b71Sopenharmony_ci    console.error('send fail: ' + JSON.stringify(err));
5064e41f4b71Sopenharmony_ci  });
5065e41f4b71Sopenharmony_ci});
5066e41f4b71Sopenharmony_ci```
5067e41f4b71Sopenharmony_ci
5068e41f4b71Sopenharmony_ci### close<sup>11+</sup>
5069e41f4b71Sopenharmony_ci
5070e41f4b71Sopenharmony_ciclose(): Promise\<void\>
5071e41f4b71Sopenharmony_ci
5072e41f4b71Sopenharmony_ciCloses a local socket connection. This API uses a promise to return the result.
5073e41f4b71Sopenharmony_ci
5074e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
5075e41f4b71Sopenharmony_ci
5076e41f4b71Sopenharmony_ci**Return value**
5077e41f4b71Sopenharmony_ci
5078e41f4b71Sopenharmony_ci| Type           | Description                                        |
5079e41f4b71Sopenharmony_ci| :-------------- | :------------------------------------------- |
5080e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
5081e41f4b71Sopenharmony_ci
5082e41f4b71Sopenharmony_ci**Error codes**
5083e41f4b71Sopenharmony_ci
5084e41f4b71Sopenharmony_ci| ID| Error Message              |
5085e41f4b71Sopenharmony_ci| -------- | -------------------- |
5086e41f4b71Sopenharmony_ci| 2301009  | Bad file descriptor. |
5087e41f4b71Sopenharmony_ci
5088e41f4b71Sopenharmony_ci**Example**
5089e41f4b71Sopenharmony_ci
5090e41f4b71Sopenharmony_ci```ts
5091e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
5092e41f4b71Sopenharmony_ci
5093e41f4b71Sopenharmony_cilet server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance();
5094e41f4b71Sopenharmony_ciserver.on('connect', (connection: socket.LocalSocketConnection) => {
5095e41f4b71Sopenharmony_ci  connection.close().then(() => {
5096e41f4b71Sopenharmony_ci    console.log('close success');
5097e41f4b71Sopenharmony_ci  }).catch((err: Object) => {
5098e41f4b71Sopenharmony_ci    console.error('close fail: ' + JSON.stringify(err));
5099e41f4b71Sopenharmony_ci  });
5100e41f4b71Sopenharmony_ci});
5101e41f4b71Sopenharmony_ci```
5102e41f4b71Sopenharmony_ci
5103e41f4b71Sopenharmony_ci### getLocalAddress<sup>12+</sup>
5104e41f4b71Sopenharmony_ci
5105e41f4b71Sopenharmony_cigetLocalAddress(): Promise\<string\>
5106e41f4b71Sopenharmony_ci
5107e41f4b71Sopenharmony_ciObtains the local socket address of a **LocalSocketConnection** connection. This API uses a promise to return the result.
5108e41f4b71Sopenharmony_ci
5109e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
5110e41f4b71Sopenharmony_ci
5111e41f4b71Sopenharmony_ci**Return value**
5112e41f4b71Sopenharmony_ci
5113e41f4b71Sopenharmony_ci| Type           | Description                                                |
5114e41f4b71Sopenharmony_ci|  -------------- |  --------------------------------------------------- |
5115e41f4b71Sopenharmony_ci| Promise\<string\> | Promise used to return the result.|
5116e41f4b71Sopenharmony_ci
5117e41f4b71Sopenharmony_ci**Error codes**
5118e41f4b71Sopenharmony_ci
5119e41f4b71Sopenharmony_ci| ID| Error Message                                   |
5120e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- |
5121e41f4b71Sopenharmony_ci| 2300002  | System internal error.                      |
5122e41f4b71Sopenharmony_ci| 2301009  | Bad file descriptor.                            |
5123e41f4b71Sopenharmony_ci| 2303188  | Socket operation on non-socket. |
5124e41f4b71Sopenharmony_ci
5125e41f4b71Sopenharmony_ci**Example**
5126e41f4b71Sopenharmony_ci
5127e41f4b71Sopenharmony_ci```ts
5128e41f4b71Sopenharmony_cilet server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance();
5129e41f4b71Sopenharmony_cilet sandboxPath: string = getContext().filesDir + '/testSocket';
5130e41f4b71Sopenharmony_cilet localAddr: socket.LocalAddress = {
5131e41f4b71Sopenharmony_ci  address: sandboxPath
5132e41f4b71Sopenharmony_ci}
5133e41f4b71Sopenharmony_ciserver.listen(localAddr).then(() => {
5134e41f4b71Sopenharmony_ci  console.info('listen success');
5135e41f4b71Sopenharmony_ci  let client: socket.LocalSocket = socket.constructLocalSocketInstance();
5136e41f4b71Sopenharmony_ci  let connectOpt: socket.LocalConnectOptions = {
5137e41f4b71Sopenharmony_ci    address: localAddr,
5138e41f4b71Sopenharmony_ci    timeout: 6000
5139e41f4b71Sopenharmony_ci  }
5140e41f4b71Sopenharmony_ci  client.connect(connectOpt).then(() => {
5141e41f4b71Sopenharmony_ci    server.getLocalAddress().then((localPath: string) => {
5142e41f4b71Sopenharmony_ci      console.info("success, localPath is" + JSON.stringify(localPath));
5143e41f4b71Sopenharmony_ci    }).catch((err: BusinessError) => {
5144e41f4b71Sopenharmony_ci      console.error("FAIL " + JSON.stringify(err));
5145e41f4b71Sopenharmony_ci    })
5146e41f4b71Sopenharmony_ci  }).catch((err: Object) => {
5147e41f4b71Sopenharmony_ci    console.error('connect fail: ' + JSON.stringify(err));
5148e41f4b71Sopenharmony_ci  });
5149e41f4b71Sopenharmony_ci});
5150e41f4b71Sopenharmony_ci```
5151e41f4b71Sopenharmony_ci
5152e41f4b71Sopenharmony_ci### on('message')<sup>11+</sup>
5153e41f4b71Sopenharmony_ci
5154e41f4b71Sopenharmony_cion(type: 'message', callback: Callback\<LocalSocketMessageInfo\>): void;
5155e41f4b71Sopenharmony_ci
5156e41f4b71Sopenharmony_ciSubscribes to **message** events of a **LocalSocketConnection** object. This API uses an asynchronous callback to return the result.
5157e41f4b71Sopenharmony_ci
5158e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
5159e41f4b71Sopenharmony_ci
5160e41f4b71Sopenharmony_ci**Parameters**
5161e41f4b71Sopenharmony_ci
5162e41f4b71Sopenharmony_ci| Name  | Type                                             | Mandatory| Description                                    |
5163e41f4b71Sopenharmony_ci| -------- | ----------------------------------------------- | ---- | --------------------------------------- |
5164e41f4b71Sopenharmony_ci| type     | string                                          | Yes  | Event type.<br/> **message**: message receiving event.    |
5165e41f4b71Sopenharmony_ci| callback | Callback\<[LocalSocketMessageInfo](#localsocketmessageinfo11)\> | Yes  | Callback used to return the result.|
5166e41f4b71Sopenharmony_ci
5167e41f4b71Sopenharmony_ci**Error codes**
5168e41f4b71Sopenharmony_ci
5169e41f4b71Sopenharmony_ci| ID| Error Message        |
5170e41f4b71Sopenharmony_ci| -------- | ---------------- |
5171e41f4b71Sopenharmony_ci| 401      | Parameter error. |
5172e41f4b71Sopenharmony_ci
5173e41f4b71Sopenharmony_ci**Example**
5174e41f4b71Sopenharmony_ci
5175e41f4b71Sopenharmony_ci```ts
5176e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
5177e41f4b71Sopenharmony_ci
5178e41f4b71Sopenharmony_cilet server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance();
5179e41f4b71Sopenharmony_cilet sandboxPath: string = getContext().filesDir + '/testSocket'
5180e41f4b71Sopenharmony_cilet listenAddr: socket.LocalAddress = {
5181e41f4b71Sopenharmony_ci  address: sandboxPath
5182e41f4b71Sopenharmony_ci}
5183e41f4b71Sopenharmony_ciserver.listen(listenAddr).then(() => {
5184e41f4b71Sopenharmony_ci  console.log("listen success");
5185e41f4b71Sopenharmony_ci}).catch((err: Object) => {
5186e41f4b71Sopenharmony_ci  console.error("listen fail: " + JSON.stringify(err));
5187e41f4b71Sopenharmony_ci});
5188e41f4b71Sopenharmony_ciserver.on('connect', (connection: socket.LocalSocketConnection) => {
5189e41f4b71Sopenharmony_ci  connection.on('message', (value: socket.LocalSocketMessageInfo) => {
5190e41f4b71Sopenharmony_ci    const uintArray = new Uint8Array(value.message);
5191e41f4b71Sopenharmony_ci    let messageView = '';
5192e41f4b71Sopenharmony_ci    for (let i = 0; i < uintArray.length; i++) {
5193e41f4b71Sopenharmony_ci      messageView += String.fromCharCode(uintArray[i]);
5194e41f4b71Sopenharmony_ci    }
5195e41f4b71Sopenharmony_ci    console.log('total: ' + JSON.stringify(value));
5196e41f4b71Sopenharmony_ci    console.log('message infomation: ' + messageView);
5197e41f4b71Sopenharmony_ci  });
5198e41f4b71Sopenharmony_ci});
5199e41f4b71Sopenharmony_ci```
5200e41f4b71Sopenharmony_ci
5201e41f4b71Sopenharmony_ci### off('message')<sup>11+</sup>
5202e41f4b71Sopenharmony_ci
5203e41f4b71Sopenharmony_cioff(type: 'message', callback?: Callback\<LocalSocketMessageInfo\>): void
5204e41f4b71Sopenharmony_ci
5205e41f4b71Sopenharmony_ciUnsubscribes from **message** events of a **LocalSocketConnection** object. This API uses an asynchronous callback to return the result.
5206e41f4b71Sopenharmony_ci
5207e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
5208e41f4b71Sopenharmony_ci
5209e41f4b71Sopenharmony_ci**Parameters**
5210e41f4b71Sopenharmony_ci
5211e41f4b71Sopenharmony_ci| Name  | Type                                             | Mandatory| Description                                |
5212e41f4b71Sopenharmony_ci| -------- | ----------------------------------------------- | ---- | ----------------------------------- |
5213e41f4b71Sopenharmony_ci| type     | string                                          | Yes  | Event type.<br/> **message**: message receiving event.|
5214e41f4b71Sopenharmony_ci| callback | Callback\<[LocalSocketMessageInfo](#localsocketmessageinfo11)\> | No  | Callback passed to the **on** function.|
5215e41f4b71Sopenharmony_ci
5216e41f4b71Sopenharmony_ci**Error codes**
5217e41f4b71Sopenharmony_ci
5218e41f4b71Sopenharmony_ci| ID| Error Message        |
5219e41f4b71Sopenharmony_ci| -------- | ---------------- |
5220e41f4b71Sopenharmony_ci| 401      | Parameter error. |
5221e41f4b71Sopenharmony_ci
5222e41f4b71Sopenharmony_ci**Example**
5223e41f4b71Sopenharmony_ci
5224e41f4b71Sopenharmony_ci```ts
5225e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
5226e41f4b71Sopenharmony_ci
5227e41f4b71Sopenharmony_cilet server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance();
5228e41f4b71Sopenharmony_cilet callback = (value: socket.LocalSocketMessageInfo) => {
5229e41f4b71Sopenharmony_ci  const uintArray = new Uint8Array(value.message)
5230e41f4b71Sopenharmony_ci  let messageView = '';
5231e41f4b71Sopenharmony_ci  for (let i = 0; i < uintArray.length; i++) {
5232e41f4b71Sopenharmony_ci    messageView += String.fromCharCode(uintArray[i]);
5233e41f4b71Sopenharmony_ci  }
5234e41f4b71Sopenharmony_ci  console.log('total: ' + JSON.stringify(value));
5235e41f4b71Sopenharmony_ci  console.log('message infomation: ' + messageView);
5236e41f4b71Sopenharmony_ci}
5237e41f4b71Sopenharmony_ciserver.on('connect', (connection: socket.LocalSocketConnection) => {
5238e41f4b71Sopenharmony_ci  connection.on('message', callback);
5239e41f4b71Sopenharmony_ci  // You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
5240e41f4b71Sopenharmony_ci  connection.off('message', callback);
5241e41f4b71Sopenharmony_ci  connection.off('message');
5242e41f4b71Sopenharmony_ci});
5243e41f4b71Sopenharmony_ci```
5244e41f4b71Sopenharmony_ci
5245e41f4b71Sopenharmony_ci### on('close')<sup>11+</sup>
5246e41f4b71Sopenharmony_ci
5247e41f4b71Sopenharmony_cion(type: 'close', callback: Callback\<void\>): void
5248e41f4b71Sopenharmony_ci
5249e41f4b71Sopenharmony_ciUnsubscribes from **close** events of a **LocalSocketConnection** object. This API uses an asynchronous callback to return the result.
5250e41f4b71Sopenharmony_ci
5251e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
5252e41f4b71Sopenharmony_ci
5253e41f4b71Sopenharmony_ci**Parameters**
5254e41f4b71Sopenharmony_ci
5255e41f4b71Sopenharmony_ci| Name  | Type            | Mandatory| Description                               |
5256e41f4b71Sopenharmony_ci| -------- | ---------------- | ---- | ----------------------------------- |
5257e41f4b71Sopenharmony_ci| type     | string           | Yes  | Event type.<br/> **close**: close event.|
5258e41f4b71Sopenharmony_ci| callback | Callback\<void\> | Yes  | Callback used to return the result.|
5259e41f4b71Sopenharmony_ci
5260e41f4b71Sopenharmony_ci**Error codes**
5261e41f4b71Sopenharmony_ci
5262e41f4b71Sopenharmony_ci| ID| Error Message        |
5263e41f4b71Sopenharmony_ci| -------- | ---------------- |
5264e41f4b71Sopenharmony_ci| 401      | Parameter error. |
5265e41f4b71Sopenharmony_ci
5266e41f4b71Sopenharmony_ci**Example**
5267e41f4b71Sopenharmony_ci
5268e41f4b71Sopenharmony_ci```ts
5269e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
5270e41f4b71Sopenharmony_ci
5271e41f4b71Sopenharmony_cilet server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance();
5272e41f4b71Sopenharmony_ciserver.on('connect', (connection: socket.LocalSocketConnection) => {
5273e41f4b71Sopenharmony_ci  connection.on('close', () => {
5274e41f4b71Sopenharmony_ci    console.log("on close success")
5275e41f4b71Sopenharmony_ci  });
5276e41f4b71Sopenharmony_ci});
5277e41f4b71Sopenharmony_ci```
5278e41f4b71Sopenharmony_ci
5279e41f4b71Sopenharmony_ci### off('close')<sup>11+</sup>
5280e41f4b71Sopenharmony_ci
5281e41f4b71Sopenharmony_cioff(type: 'close', callback?: Callback\<void\>): void
5282e41f4b71Sopenharmony_ci
5283e41f4b71Sopenharmony_ciUnsubscribes from **close** events of a **LocalSocketConnection** object. This API uses an asynchronous callback to return the result.
5284e41f4b71Sopenharmony_ci
5285e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
5286e41f4b71Sopenharmony_ci
5287e41f4b71Sopenharmony_ci**Parameters**
5288e41f4b71Sopenharmony_ci
5289e41f4b71Sopenharmony_ci| Name  | Type            | Mandatory| Description                               |
5290e41f4b71Sopenharmony_ci| -------- | ---------------- | ---- | ----------------------------------- |
5291e41f4b71Sopenharmony_ci| type     | string           | Yes  | Event type.<br/> **close**: close event.|
5292e41f4b71Sopenharmony_ci| callback | Callback\<void\> | No  | Callback passed to the **on** function.|
5293e41f4b71Sopenharmony_ci
5294e41f4b71Sopenharmony_ci**Error codes**
5295e41f4b71Sopenharmony_ci
5296e41f4b71Sopenharmony_ci| ID| Error Message        |
5297e41f4b71Sopenharmony_ci| -------- | ---------------- |
5298e41f4b71Sopenharmony_ci| 401      | Parameter error. |
5299e41f4b71Sopenharmony_ci
5300e41f4b71Sopenharmony_ci**Example**
5301e41f4b71Sopenharmony_ci
5302e41f4b71Sopenharmony_ci```ts
5303e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
5304e41f4b71Sopenharmony_ci
5305e41f4b71Sopenharmony_cilet server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance();
5306e41f4b71Sopenharmony_cilet callback = () => {
5307e41f4b71Sopenharmony_ci  console.log("on close success");
5308e41f4b71Sopenharmony_ci}
5309e41f4b71Sopenharmony_ciserver.on('connect', (connection: socket.LocalSocketConnection) => {
5310e41f4b71Sopenharmony_ci  connection.on('close', callback);
5311e41f4b71Sopenharmony_ci  // You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
5312e41f4b71Sopenharmony_ci  connection.off('close', callback);
5313e41f4b71Sopenharmony_ci  connection.off('close');
5314e41f4b71Sopenharmony_ci});
5315e41f4b71Sopenharmony_ci```
5316e41f4b71Sopenharmony_ci
5317e41f4b71Sopenharmony_ci### on('error')<sup>11+</sup>
5318e41f4b71Sopenharmony_ci
5319e41f4b71Sopenharmony_cion(type: 'error', callback: ErrorCallback): void
5320e41f4b71Sopenharmony_ci
5321e41f4b71Sopenharmony_ciSubscribes to **error** events of a **LocalSocketConnection** object. This API uses an asynchronous callback to return the result.
5322e41f4b71Sopenharmony_ci
5323e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
5324e41f4b71Sopenharmony_ci
5325e41f4b71Sopenharmony_ci**Parameters**
5326e41f4b71Sopenharmony_ci
5327e41f4b71Sopenharmony_ci| Name  | Type         | Mandatory| Description                                |
5328e41f4b71Sopenharmony_ci| -------- | ------------- | ---- | ------------------------------------ |
5329e41f4b71Sopenharmony_ci| type     | string        | Yes  | Event type.<br/> **error**: error event.|
5330e41f4b71Sopenharmony_ci| callback | ErrorCallback | Yes  | Callback used to return the result.|
5331e41f4b71Sopenharmony_ci
5332e41f4b71Sopenharmony_ci**Error codes**
5333e41f4b71Sopenharmony_ci
5334e41f4b71Sopenharmony_ci| ID| Error Message        |
5335e41f4b71Sopenharmony_ci| -------- | ---------------- |
5336e41f4b71Sopenharmony_ci| 401      | Parameter error. |
5337e41f4b71Sopenharmony_ci
5338e41f4b71Sopenharmony_ci**Example**
5339e41f4b71Sopenharmony_ci
5340e41f4b71Sopenharmony_ci```ts
5341e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
5342e41f4b71Sopenharmony_ci
5343e41f4b71Sopenharmony_cilet server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance();
5344e41f4b71Sopenharmony_ciserver.on('connect', (connection: socket.LocalSocketConnection) => {
5345e41f4b71Sopenharmony_ci  connection.on('error', (err: Object) => {
5346e41f4b71Sopenharmony_ci    console.error("on error, err:" + JSON.stringify(err))
5347e41f4b71Sopenharmony_ci  });
5348e41f4b71Sopenharmony_ci});
5349e41f4b71Sopenharmony_ci```
5350e41f4b71Sopenharmony_ci
5351e41f4b71Sopenharmony_ci### off('error')<sup>11+</sup>
5352e41f4b71Sopenharmony_ci
5353e41f4b71Sopenharmony_cioff(type: 'error', callback?: ErrorCallback): void
5354e41f4b71Sopenharmony_ci
5355e41f4b71Sopenharmony_ciUnsubscribes from **error** events of a **LocalSocketConnection** object. This API uses an asynchronous callback to return the result.
5356e41f4b71Sopenharmony_ci
5357e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
5358e41f4b71Sopenharmony_ci
5359e41f4b71Sopenharmony_ci**Parameters**
5360e41f4b71Sopenharmony_ci
5361e41f4b71Sopenharmony_ci| Name  | Type         | Mandatory| Description                                |
5362e41f4b71Sopenharmony_ci| -------- | ------------- | ---- | ------------------------------------ |
5363e41f4b71Sopenharmony_ci| type     | string        | Yes  | Event type.<br/> **error**: error event.|
5364e41f4b71Sopenharmony_ci| callback | ErrorCallback | No  | Callback passed to the **on** function.  |
5365e41f4b71Sopenharmony_ci
5366e41f4b71Sopenharmony_ci**Error codes**
5367e41f4b71Sopenharmony_ci
5368e41f4b71Sopenharmony_ci| ID| Error Message        |
5369e41f4b71Sopenharmony_ci| -------- | ---------------- |
5370e41f4b71Sopenharmony_ci| 401      | Parameter error. |
5371e41f4b71Sopenharmony_ci
5372e41f4b71Sopenharmony_ci**Example**
5373e41f4b71Sopenharmony_ci
5374e41f4b71Sopenharmony_ci```ts
5375e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
5376e41f4b71Sopenharmony_ci
5377e41f4b71Sopenharmony_cilet callback = (err: Object) => {
5378e41f4b71Sopenharmony_ci  console.error("on error, err: " + JSON.stringify(err));
5379e41f4b71Sopenharmony_ci}
5380e41f4b71Sopenharmony_cilet server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance();
5381e41f4b71Sopenharmony_ciserver.on('connect', (connection: socket.LocalSocketConnection) => {
5382e41f4b71Sopenharmony_ci  connection.on('error', callback);
5383e41f4b71Sopenharmony_ci  // You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
5384e41f4b71Sopenharmony_ci  connection.off('error', callback);
5385e41f4b71Sopenharmony_ci  connection.off('error');
5386e41f4b71Sopenharmony_ci});
5387e41f4b71Sopenharmony_ci```
5388e41f4b71Sopenharmony_ci
5389e41f4b71Sopenharmony_ci## Description of LocalSocket Error Codes
5390e41f4b71Sopenharmony_ci
5391e41f4b71Sopenharmony_ciThe LocalSocket error code mapping is in the format of 2301000 + Linux kernel error code.
5392e41f4b71Sopenharmony_ci
5393e41f4b71Sopenharmony_ciFor details about error codes, see [Socket Error Codes](errorcode-net-socket.md).
5394e41f4b71Sopenharmony_ci
5395e41f4b71Sopenharmony_ci## socket.constructTLSSocketInstance<sup>9+</sup>
5396e41f4b71Sopenharmony_ci
5397e41f4b71Sopenharmony_ciconstructTLSSocketInstance(): TLSSocket
5398e41f4b71Sopenharmony_ci
5399e41f4b71Sopenharmony_ciCreates a **TLSSocket** object.
5400e41f4b71Sopenharmony_ci
5401e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
5402e41f4b71Sopenharmony_ci
5403e41f4b71Sopenharmony_ci**Return value**
5404e41f4b71Sopenharmony_ci
5405e41f4b71Sopenharmony_ci| Type                              | Description                   |
5406e41f4b71Sopenharmony_ci|  --------------------------------- |  ---------------------- |
5407e41f4b71Sopenharmony_ci| [TLSSocket](#tlssocket9) | **TLSSocket** object.|
5408e41f4b71Sopenharmony_ci
5409e41f4b71Sopenharmony_ci**Example**
5410e41f4b71Sopenharmony_ci
5411e41f4b71Sopenharmony_ci```ts
5412e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
5413e41f4b71Sopenharmony_ci
5414e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
5415e41f4b71Sopenharmony_ci```
5416e41f4b71Sopenharmony_ci
5417e41f4b71Sopenharmony_ci## socket.constructTLSSocketInstance<sup>12+</sup>
5418e41f4b71Sopenharmony_ci
5419e41f4b71Sopenharmony_ciconstructTLSSocketInstance(tcpSocket: TCPSocket): TLSSocket
5420e41f4b71Sopenharmony_ci
5421e41f4b71Sopenharmony_ciUpgrades a **TCPSocket** connection to a **TLSSocket** connection.
5422e41f4b71Sopenharmony_ci
5423e41f4b71Sopenharmony_ci> **NOTE**
5424e41f4b71Sopenharmony_ci> Before calling **constructTLSSocketInstance**, ensure that a **TCPSocket** connection has been established and no data is transmitted. After a successful upgrade, you do not need to call the **close** API for the **TCPSocket** object.
5425e41f4b71Sopenharmony_ci
5426e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
5427e41f4b71Sopenharmony_ci
5428e41f4b71Sopenharmony_ci**Parameters**
5429e41f4b71Sopenharmony_ci
5430e41f4b71Sopenharmony_ci| Name      | Type| Mandatory| Description                    |
5431e41f4b71Sopenharmony_ci|-----------|----| ---- |------------------------|
5432e41f4b71Sopenharmony_ci| tcpSocket | [TCPSocket](#tcpsocket)   | Yes  | **TCPSocket** connection to be upgraded.|
5433e41f4b71Sopenharmony_ci
5434e41f4b71Sopenharmony_ci**Return value**
5435e41f4b71Sopenharmony_ci
5436e41f4b71Sopenharmony_ci| Type                              | Description                   |
5437e41f4b71Sopenharmony_ci|  --------------------------------- |  ---------------------- |
5438e41f4b71Sopenharmony_ci| [TLSSocket](#tlssocket9) | **TLSSocket** object.|
5439e41f4b71Sopenharmony_ci
5440e41f4b71Sopenharmony_ci**Error codes**
5441e41f4b71Sopenharmony_ci
5442e41f4b71Sopenharmony_ci| ID  | Error Message                            |
5443e41f4b71Sopenharmony_ci|---------|----------------------------------|
5444e41f4b71Sopenharmony_ci| 401     | Parameter error.  |
5445e41f4b71Sopenharmony_ci| 2300002 | System internal error.  |
5446e41f4b71Sopenharmony_ci| 2303601 | Invalid socket FD.     |
5447e41f4b71Sopenharmony_ci| 2303602 | Socket is not connected.  |
5448e41f4b71Sopenharmony_ci
5449e41f4b71Sopenharmony_ci**Example**
5450e41f4b71Sopenharmony_ci
5451e41f4b71Sopenharmony_ci```ts
5452e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
5453e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
5454e41f4b71Sopenharmony_ci
5455e41f4b71Sopenharmony_cilet tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
5456e41f4b71Sopenharmony_cilet tcpconnectoptions: socket.TCPConnectOptions = {
5457e41f4b71Sopenharmony_ci  address: {
5458e41f4b71Sopenharmony_ci    address: '192.168.xx.xxx',
5459e41f4b71Sopenharmony_ci    port: 8080
5460e41f4b71Sopenharmony_ci  },
5461e41f4b71Sopenharmony_ci  timeout: 6000
5462e41f4b71Sopenharmony_ci}
5463e41f4b71Sopenharmony_citcp.connect(tcpconnectoptions, (err: BusinessError) => {
5464e41f4b71Sopenharmony_ci  if (err) {
5465e41f4b71Sopenharmony_ci    console.log('connect fail');
5466e41f4b71Sopenharmony_ci    return;
5467e41f4b71Sopenharmony_ci  }
5468e41f4b71Sopenharmony_ci  console.log('connect success');
5469e41f4b71Sopenharmony_ci
5470e41f4b71Sopenharmony_ci  // Ensure that a TCPSocket connection has been established before upgrading it to a TLSSocket connection.
5471e41f4b71Sopenharmony_ci  let tls: socket.TLSSocket = socket.constructTLSSocketInstance(tcp);
5472e41f4b71Sopenharmony_ci})
5473e41f4b71Sopenharmony_ci```
5474e41f4b71Sopenharmony_ci
5475e41f4b71Sopenharmony_ci## TLSSocket<sup>9+</sup>
5476e41f4b71Sopenharmony_ci
5477e41f4b71Sopenharmony_ciDefines a TLS socket connection. Before calling TLSSocket APIs, you need to call [socket.constructTLSSocketInstance](#socketconstructtlssocketinstance9) to create a **TLSSocket** object.
5478e41f4b71Sopenharmony_ci
5479e41f4b71Sopenharmony_ci### bind<sup>9+</sup>
5480e41f4b71Sopenharmony_ci
5481e41f4b71Sopenharmony_cibind(address: NetAddress, callback: AsyncCallback\<void\>): void
5482e41f4b71Sopenharmony_ci
5483e41f4b71Sopenharmony_ciBinds the IP address and port number. This API uses an asynchronous callback to return the result.
5484e41f4b71Sopenharmony_ci
5485e41f4b71Sopenharmony_ci> **NOTE**
5486e41f4b71Sopenharmony_ci> If the **TLSSocket** object is upgraded from a **TCPSocket** object, you do not need to execute the **bind** API.
5487e41f4b71Sopenharmony_ci
5488e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
5489e41f4b71Sopenharmony_ci
5490e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
5491e41f4b71Sopenharmony_ci
5492e41f4b71Sopenharmony_ci**Parameters**
5493e41f4b71Sopenharmony_ci
5494e41f4b71Sopenharmony_ci| Name  | Type                              | Mandatory| Description                                                  |
5495e41f4b71Sopenharmony_ci| -------- | ---------------------------------- | ---- | ------------------------------------------------------ |
5496e41f4b71Sopenharmony_ci| address  | [NetAddress](#netaddress) | Yes  | Destination address. For details, see [NetAddress](#netaddress).|
5497e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void\>              | Yes  | Callback used to return the result. If the operation is successful, the result of binding the local IP address and port number is returned. If the operation fails, an error message is returned.|
5498e41f4b71Sopenharmony_ci
5499e41f4b71Sopenharmony_ci**Error codes**
5500e41f4b71Sopenharmony_ci
5501e41f4b71Sopenharmony_ci| ID| Error Message                |
5502e41f4b71Sopenharmony_ci| ------- | ----------------------- |
5503e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
5504e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
5505e41f4b71Sopenharmony_ci| 2303198 | Address already in use. |
5506e41f4b71Sopenharmony_ci| 2300002 | System internal error.  |
5507e41f4b71Sopenharmony_ci
5508e41f4b71Sopenharmony_ci**Example**
5509e41f4b71Sopenharmony_ci
5510e41f4b71Sopenharmony_ci```ts
5511e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
5512e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
5513e41f4b71Sopenharmony_ci
5514e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
5515e41f4b71Sopenharmony_cilet bindAddr: socket.NetAddress = {
5516e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
5517e41f4b71Sopenharmony_ci  port: 8080
5518e41f4b71Sopenharmony_ci}
5519e41f4b71Sopenharmony_citls.bind(bindAddr, (err: BusinessError) => {
5520e41f4b71Sopenharmony_ci  if (err) {
5521e41f4b71Sopenharmony_ci    console.log('bind fail');
5522e41f4b71Sopenharmony_ci    return;
5523e41f4b71Sopenharmony_ci  }
5524e41f4b71Sopenharmony_ci  console.log('bind success');
5525e41f4b71Sopenharmony_ci});
5526e41f4b71Sopenharmony_ci```
5527e41f4b71Sopenharmony_ci
5528e41f4b71Sopenharmony_ci### bind<sup>9+</sup>
5529e41f4b71Sopenharmony_ci
5530e41f4b71Sopenharmony_cibind(address: NetAddress): Promise\<void\>
5531e41f4b71Sopenharmony_ci
5532e41f4b71Sopenharmony_ciBinds the IP address and port number. This API uses a promise to return the result.
5533e41f4b71Sopenharmony_ci
5534e41f4b71Sopenharmony_ci> **NOTE**
5535e41f4b71Sopenharmony_ci> If the **TLSSocket** object is upgraded from a **TCPSocket** object, you do not need to execute the **bind** API.
5536e41f4b71Sopenharmony_ci
5537e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
5538e41f4b71Sopenharmony_ci
5539e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
5540e41f4b71Sopenharmony_ci
5541e41f4b71Sopenharmony_ci**Parameters**
5542e41f4b71Sopenharmony_ci
5543e41f4b71Sopenharmony_ci| Name | Type                              | Mandatory| Description                                                  |
5544e41f4b71Sopenharmony_ci| ------- | ---------------------------------- | ---- | ------------------------------------------------------ |
5545e41f4b71Sopenharmony_ci| address | [NetAddress](#netaddress)          | Yes  | Destination address. For details, see [NetAddress](#netaddress).|
5546e41f4b71Sopenharmony_ci
5547e41f4b71Sopenharmony_ci**Return value**
5548e41f4b71Sopenharmony_ci
5549e41f4b71Sopenharmony_ci| Type           | Description                                                    |
5550e41f4b71Sopenharmony_ci|  -------------- |  ------------------------------------------------------- |
5551e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result. If the operation fails, an error message is returned.|
5552e41f4b71Sopenharmony_ci
5553e41f4b71Sopenharmony_ci**Error codes**
5554e41f4b71Sopenharmony_ci
5555e41f4b71Sopenharmony_ci| ID| Error Message                |
5556e41f4b71Sopenharmony_ci| ------- | ----------------------- |
5557e41f4b71Sopenharmony_ci| 401     | Parameter error.        |
5558e41f4b71Sopenharmony_ci| 201     | Permission denied.      |
5559e41f4b71Sopenharmony_ci| 2303198 | Address already in use. |
5560e41f4b71Sopenharmony_ci| 2300002 | System internal error.  |
5561e41f4b71Sopenharmony_ci
5562e41f4b71Sopenharmony_ci**Example**
5563e41f4b71Sopenharmony_ci
5564e41f4b71Sopenharmony_ci```ts
5565e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
5566e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
5567e41f4b71Sopenharmony_ci
5568e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
5569e41f4b71Sopenharmony_cilet bindAddr: socket.NetAddress = {
5570e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
5571e41f4b71Sopenharmony_ci  port: 8080
5572e41f4b71Sopenharmony_ci}
5573e41f4b71Sopenharmony_citls.bind(bindAddr).then(() => {
5574e41f4b71Sopenharmony_ci  console.log('bind success');
5575e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
5576e41f4b71Sopenharmony_ci  console.log('bind fail');
5577e41f4b71Sopenharmony_ci});
5578e41f4b71Sopenharmony_ci```
5579e41f4b71Sopenharmony_ci
5580e41f4b71Sopenharmony_ci### getState<sup>9+</sup>
5581e41f4b71Sopenharmony_ci
5582e41f4b71Sopenharmony_cigetState(callback: AsyncCallback\<SocketStateBase\>): void
5583e41f4b71Sopenharmony_ci
5584e41f4b71Sopenharmony_ciObtains the status of the TLS socket connection. This API uses an asynchronous callback to return the result.
5585e41f4b71Sopenharmony_ci
5586e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
5587e41f4b71Sopenharmony_ci
5588e41f4b71Sopenharmony_ci**Parameters**
5589e41f4b71Sopenharmony_ci
5590e41f4b71Sopenharmony_ci| Name  | Type                                                  | Mandatory| Description      |
5591e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------ | ---- | ---------- |
5592e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[SocketStateBase](#socketstatebase)\> | Yes  | Callback used to return the result. If the operation is successful, the status of the TLS socket connection is returned. If the operation fails, an error message is returned.|
5593e41f4b71Sopenharmony_ci
5594e41f4b71Sopenharmony_ci**Error codes**
5595e41f4b71Sopenharmony_ci
5596e41f4b71Sopenharmony_ci| ID| Error Message                       |
5597e41f4b71Sopenharmony_ci| ------- | ------------------------------ |
5598e41f4b71Sopenharmony_ci| 2303188 | Socket operation on non-socket.|
5599e41f4b71Sopenharmony_ci| 2300002 | System internal error.         |
5600e41f4b71Sopenharmony_ci
5601e41f4b71Sopenharmony_ci**Example**
5602e41f4b71Sopenharmony_ci
5603e41f4b71Sopenharmony_ci```ts
5604e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
5605e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
5606e41f4b71Sopenharmony_ci
5607e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
5608e41f4b71Sopenharmony_cilet bindAddr: socket.NetAddress = {
5609e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
5610e41f4b71Sopenharmony_ci  port: 8080
5611e41f4b71Sopenharmony_ci}
5612e41f4b71Sopenharmony_citls.bind(bindAddr, (err: BusinessError) => {
5613e41f4b71Sopenharmony_ci  if (err) {
5614e41f4b71Sopenharmony_ci    console.log('bind fail');
5615e41f4b71Sopenharmony_ci    return;
5616e41f4b71Sopenharmony_ci  }
5617e41f4b71Sopenharmony_ci  console.log('bind success');
5618e41f4b71Sopenharmony_ci});
5619e41f4b71Sopenharmony_citls.getState((err: BusinessError, data: socket.SocketStateBase) => {
5620e41f4b71Sopenharmony_ci  if (err) {
5621e41f4b71Sopenharmony_ci    console.log('getState fail');
5622e41f4b71Sopenharmony_ci    return;
5623e41f4b71Sopenharmony_ci  }
5624e41f4b71Sopenharmony_ci  console.log('getState success:' + JSON.stringify(data));
5625e41f4b71Sopenharmony_ci});
5626e41f4b71Sopenharmony_ci```
5627e41f4b71Sopenharmony_ci
5628e41f4b71Sopenharmony_ci### getState<sup>9+</sup>
5629e41f4b71Sopenharmony_ci
5630e41f4b71Sopenharmony_cigetState(): Promise\<SocketStateBase\>
5631e41f4b71Sopenharmony_ci
5632e41f4b71Sopenharmony_ciObtains the status of the TLS socket connection. This API uses a promise to return the result.
5633e41f4b71Sopenharmony_ci
5634e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
5635e41f4b71Sopenharmony_ci
5636e41f4b71Sopenharmony_ci**Return value**
5637e41f4b71Sopenharmony_ci
5638e41f4b71Sopenharmony_ci| Type                                            | Description                                      |
5639e41f4b71Sopenharmony_ci|  ----------------------------------------------- |  ----------------------------------------- |
5640e41f4b71Sopenharmony_ci| Promise\<[SocketStateBase](#socketstatebase)\> | Promise used to return the result. If the operation fails, an error message is returned.|
5641e41f4b71Sopenharmony_ci
5642e41f4b71Sopenharmony_ci**Error codes**
5643e41f4b71Sopenharmony_ci
5644e41f4b71Sopenharmony_ci| ID| Error Message                       |
5645e41f4b71Sopenharmony_ci| ------- | ------------------------------ |
5646e41f4b71Sopenharmony_ci| 2303188 | Socket operation on non-socket.|
5647e41f4b71Sopenharmony_ci| 2300002 | System internal error.         |
5648e41f4b71Sopenharmony_ci
5649e41f4b71Sopenharmony_ci**Example**
5650e41f4b71Sopenharmony_ci
5651e41f4b71Sopenharmony_ci```ts
5652e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
5653e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
5654e41f4b71Sopenharmony_ci
5655e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
5656e41f4b71Sopenharmony_cilet bindAddr: socket.NetAddress = {
5657e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
5658e41f4b71Sopenharmony_ci  port: 8080
5659e41f4b71Sopenharmony_ci}
5660e41f4b71Sopenharmony_citls.bind(bindAddr, (err: BusinessError) => {
5661e41f4b71Sopenharmony_ci  if (err) {
5662e41f4b71Sopenharmony_ci    console.log('bind fail');
5663e41f4b71Sopenharmony_ci    return;
5664e41f4b71Sopenharmony_ci  }
5665e41f4b71Sopenharmony_ci  console.log('bind success');
5666e41f4b71Sopenharmony_ci});
5667e41f4b71Sopenharmony_citls.getState().then(() => {
5668e41f4b71Sopenharmony_ci  console.log('getState success');
5669e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
5670e41f4b71Sopenharmony_ci  console.log('getState fail');
5671e41f4b71Sopenharmony_ci});
5672e41f4b71Sopenharmony_ci```
5673e41f4b71Sopenharmony_ci
5674e41f4b71Sopenharmony_ci### setExtraOptions<sup>9+</sup>
5675e41f4b71Sopenharmony_ci
5676e41f4b71Sopenharmony_cisetExtraOptions(options: TCPExtraOptions, callback: AsyncCallback\<void\>): void
5677e41f4b71Sopenharmony_ci
5678e41f4b71Sopenharmony_ciSets other properties of the TCP socket connection after **bind** is successfully called. This API uses an asynchronous callback to return the result.
5679e41f4b71Sopenharmony_ci
5680e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
5681e41f4b71Sopenharmony_ci
5682e41f4b71Sopenharmony_ci**Parameters**
5683e41f4b71Sopenharmony_ci
5684e41f4b71Sopenharmony_ci| Name  | Type                                     | Mandatory| Description                                                        |
5685e41f4b71Sopenharmony_ci| -------- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
5686e41f4b71Sopenharmony_ci| options  | [TCPExtraOptions](#tcpextraoptions) | Yes  | Other properties of the TCP socket connection. For details, see [TCPExtraOptions](#tcpextraoptions).|
5687e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void\>                     | Yes  | Callback used to return the result. If the operation is successful, the result of setting other properties of the TCP socket connection is returned. If the operation fails, an error message is returned.|
5688e41f4b71Sopenharmony_ci
5689e41f4b71Sopenharmony_ci**Error codes**
5690e41f4b71Sopenharmony_ci
5691e41f4b71Sopenharmony_ci| ID| Error Message                       |
5692e41f4b71Sopenharmony_ci| ------- | -----------------------------  |
5693e41f4b71Sopenharmony_ci| 401     | Parameter error.               |
5694e41f4b71Sopenharmony_ci| 2303188 | Socket operation on non-socket.|
5695e41f4b71Sopenharmony_ci| 2300002 | System internal error.         |
5696e41f4b71Sopenharmony_ci
5697e41f4b71Sopenharmony_ci**Example**
5698e41f4b71Sopenharmony_ci
5699e41f4b71Sopenharmony_ci```ts
5700e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
5701e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
5702e41f4b71Sopenharmony_ci
5703e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
5704e41f4b71Sopenharmony_cilet bindAddr: socket.NetAddress = {
5705e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
5706e41f4b71Sopenharmony_ci  port: 8080
5707e41f4b71Sopenharmony_ci}
5708e41f4b71Sopenharmony_citls.bind(bindAddr, (err: BusinessError) => {
5709e41f4b71Sopenharmony_ci  if (err) {
5710e41f4b71Sopenharmony_ci    console.log('bind fail');
5711e41f4b71Sopenharmony_ci    return;
5712e41f4b71Sopenharmony_ci  }
5713e41f4b71Sopenharmony_ci  console.log('bind success');
5714e41f4b71Sopenharmony_ci});
5715e41f4b71Sopenharmony_ci
5716e41f4b71Sopenharmony_ciinterface SocketLinger {
5717e41f4b71Sopenharmony_ci  on: boolean;
5718e41f4b71Sopenharmony_ci  linger: number;
5719e41f4b71Sopenharmony_ci}
5720e41f4b71Sopenharmony_ci
5721e41f4b71Sopenharmony_cilet tcpExtraOptions: socket.TCPExtraOptions = {
5722e41f4b71Sopenharmony_ci  keepAlive: true,
5723e41f4b71Sopenharmony_ci  OOBInline: true,
5724e41f4b71Sopenharmony_ci  TCPNoDelay: true,
5725e41f4b71Sopenharmony_ci  socketLinger: { on: true, linger: 10 } as SocketLinger,
5726e41f4b71Sopenharmony_ci  receiveBufferSize: 1000,
5727e41f4b71Sopenharmony_ci  sendBufferSize: 1000,
5728e41f4b71Sopenharmony_ci  reuseAddress: true,
5729e41f4b71Sopenharmony_ci  socketTimeout: 3000
5730e41f4b71Sopenharmony_ci}
5731e41f4b71Sopenharmony_citls.setExtraOptions(tcpExtraOptions, (err: BusinessError) => {
5732e41f4b71Sopenharmony_ci  if (err) {
5733e41f4b71Sopenharmony_ci    console.log('setExtraOptions fail');
5734e41f4b71Sopenharmony_ci    return;
5735e41f4b71Sopenharmony_ci  }
5736e41f4b71Sopenharmony_ci  console.log('setExtraOptions success');
5737e41f4b71Sopenharmony_ci});
5738e41f4b71Sopenharmony_ci```
5739e41f4b71Sopenharmony_ci
5740e41f4b71Sopenharmony_ci### setExtraOptions<sup>9+</sup>
5741e41f4b71Sopenharmony_ci
5742e41f4b71Sopenharmony_cisetExtraOptions(options: TCPExtraOptions): Promise\<void\>
5743e41f4b71Sopenharmony_ci
5744e41f4b71Sopenharmony_ciSets other properties of the TCP socket connection after **bind** is successfully called. This API uses a promise to return the result.
5745e41f4b71Sopenharmony_ci
5746e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
5747e41f4b71Sopenharmony_ci
5748e41f4b71Sopenharmony_ci**Parameters**
5749e41f4b71Sopenharmony_ci
5750e41f4b71Sopenharmony_ci| Name | Type                                     | Mandatory| Description                                                        |
5751e41f4b71Sopenharmony_ci| ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
5752e41f4b71Sopenharmony_ci| options | [TCPExtraOptions](#tcpextraoptions) | Yes  | Other properties of the TCP socket connection. For details, see [TCPExtraOptions](#tcpextraoptions).|
5753e41f4b71Sopenharmony_ci
5754e41f4b71Sopenharmony_ci**Return value**
5755e41f4b71Sopenharmony_ci
5756e41f4b71Sopenharmony_ci| Type           | Description                                                |
5757e41f4b71Sopenharmony_ci|  -------------- |  --------------------------------------------------- |
5758e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result. If the operation fails, an error message is returned.|
5759e41f4b71Sopenharmony_ci
5760e41f4b71Sopenharmony_ci**Error codes**
5761e41f4b71Sopenharmony_ci
5762e41f4b71Sopenharmony_ci| ID| Error Message                       |
5763e41f4b71Sopenharmony_ci| ------- | ------------------------------ |
5764e41f4b71Sopenharmony_ci| 401     | Parameter error.               |
5765e41f4b71Sopenharmony_ci| 2303188 | Socket operation on non-socket.|
5766e41f4b71Sopenharmony_ci| 2300002 | System internal error.         |
5767e41f4b71Sopenharmony_ci
5768e41f4b71Sopenharmony_ci**Example**
5769e41f4b71Sopenharmony_ci
5770e41f4b71Sopenharmony_ci```ts
5771e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
5772e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
5773e41f4b71Sopenharmony_ci
5774e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
5775e41f4b71Sopenharmony_cilet bindAddr: socket.NetAddress = {
5776e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
5777e41f4b71Sopenharmony_ci  port: 8080
5778e41f4b71Sopenharmony_ci}
5779e41f4b71Sopenharmony_citls.bind(bindAddr, (err: BusinessError) => {
5780e41f4b71Sopenharmony_ci  if (err) {
5781e41f4b71Sopenharmony_ci    console.log('bind fail');
5782e41f4b71Sopenharmony_ci    return;
5783e41f4b71Sopenharmony_ci  }
5784e41f4b71Sopenharmony_ci  console.log('bind success');
5785e41f4b71Sopenharmony_ci});
5786e41f4b71Sopenharmony_ci
5787e41f4b71Sopenharmony_ciinterface SocketLinger {
5788e41f4b71Sopenharmony_ci  on: boolean;
5789e41f4b71Sopenharmony_ci  linger: number;
5790e41f4b71Sopenharmony_ci}
5791e41f4b71Sopenharmony_ci
5792e41f4b71Sopenharmony_cilet tcpExtraOptions: socket.TCPExtraOptions = {
5793e41f4b71Sopenharmony_ci  keepAlive: true,
5794e41f4b71Sopenharmony_ci  OOBInline: true,
5795e41f4b71Sopenharmony_ci  TCPNoDelay: true,
5796e41f4b71Sopenharmony_ci  socketLinger: { on: true, linger: 10 } as SocketLinger,
5797e41f4b71Sopenharmony_ci  receiveBufferSize: 1000,
5798e41f4b71Sopenharmony_ci  sendBufferSize: 1000,
5799e41f4b71Sopenharmony_ci  reuseAddress: true,
5800e41f4b71Sopenharmony_ci  socketTimeout: 3000
5801e41f4b71Sopenharmony_ci}
5802e41f4b71Sopenharmony_citls.setExtraOptions(tcpExtraOptions).then(() => {
5803e41f4b71Sopenharmony_ci  console.log('setExtraOptions success');
5804e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
5805e41f4b71Sopenharmony_ci  console.log('setExtraOptions fail');
5806e41f4b71Sopenharmony_ci});
5807e41f4b71Sopenharmony_ci```
5808e41f4b71Sopenharmony_ci
5809e41f4b71Sopenharmony_ci### on('message')<sup>9+</sup>
5810e41f4b71Sopenharmony_ci
5811e41f4b71Sopenharmony_cion(type: 'message', callback: Callback\<SocketMessageInfo\>): void;
5812e41f4b71Sopenharmony_ci
5813e41f4b71Sopenharmony_ciSubscribes to **message** events of the TLS socket connection. This API uses an asynchronous callback to return the result.
5814e41f4b71Sopenharmony_ci
5815e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
5816e41f4b71Sopenharmony_ci
5817e41f4b71Sopenharmony_ci**Parameters**
5818e41f4b71Sopenharmony_ci
5819e41f4b71Sopenharmony_ci| Name  | Type                                                        | Mandatory| Description                                     |
5820e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------- |
5821e41f4b71Sopenharmony_ci| type     | string                                                       | Yes  | Event type.<br/> **message**: message receiving event.|
5822e41f4b71Sopenharmony_ci| callback | Callback\<[SocketMessageInfo](#socketmessageinfo11)\> | Yes  | Callback used to return the result.  |
5823e41f4b71Sopenharmony_ci
5824e41f4b71Sopenharmony_ci**Error codes**
5825e41f4b71Sopenharmony_ci
5826e41f4b71Sopenharmony_ci| ID| Error Message                       |
5827e41f4b71Sopenharmony_ci| ------- | ------------------------------ |
5828e41f4b71Sopenharmony_ci| 401     | Parameter error.               |
5829e41f4b71Sopenharmony_ci
5830e41f4b71Sopenharmony_ci**Example**
5831e41f4b71Sopenharmony_ci
5832e41f4b71Sopenharmony_ci```ts
5833e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
5834e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
5835e41f4b71Sopenharmony_ci
5836e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
5837e41f4b71Sopenharmony_cilet messageView = '';
5838e41f4b71Sopenharmony_citls.on('message', (value: socket.SocketMessageInfo) => {
5839e41f4b71Sopenharmony_ci  for (let i: number = 0; i < value.message.byteLength; i++) {
5840e41f4b71Sopenharmony_ci    let uint8Array = new Uint8Array(value.message) 
5841e41f4b71Sopenharmony_ci    let messages = uint8Array[i]
5842e41f4b71Sopenharmony_ci    let message = String.fromCharCode(messages);
5843e41f4b71Sopenharmony_ci    messageView += message;
5844e41f4b71Sopenharmony_ci  }
5845e41f4b71Sopenharmony_ci  console.log('on message message: ' + JSON.stringify(messageView));
5846e41f4b71Sopenharmony_ci  console.log('remoteInfo: ' + JSON.stringify(value.remoteInfo));
5847e41f4b71Sopenharmony_ci});
5848e41f4b71Sopenharmony_ci```
5849e41f4b71Sopenharmony_ci
5850e41f4b71Sopenharmony_ci### off('message')<sup>9+</sup>
5851e41f4b71Sopenharmony_ci
5852e41f4b71Sopenharmony_cioff(type: 'message', callback?: Callback\<SocketMessageInfo\>): void
5853e41f4b71Sopenharmony_ci
5854e41f4b71Sopenharmony_ciUnsubscribes from **message** events of a **TLSSocket** object. This API uses an asynchronous callback to return the result.
5855e41f4b71Sopenharmony_ci
5856e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
5857e41f4b71Sopenharmony_ci
5858e41f4b71Sopenharmony_ci**Parameters**
5859e41f4b71Sopenharmony_ci
5860e41f4b71Sopenharmony_ci| Name  | Type                                                        | Mandatory| Description                                     |
5861e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------- |
5862e41f4b71Sopenharmony_ci| type     | string                                                       | Yes  | Event type.<br/> **message**: message receiving event.|
5863e41f4b71Sopenharmony_ci| callback | Callback\<[SocketMessageInfo](#socketmessageinfo11)\> | No  | Callback used to return the result.  |
5864e41f4b71Sopenharmony_ci
5865e41f4b71Sopenharmony_ci**Error codes**
5866e41f4b71Sopenharmony_ci
5867e41f4b71Sopenharmony_ci| ID| Error Message                       |
5868e41f4b71Sopenharmony_ci| ------- | ------------------------------ |
5869e41f4b71Sopenharmony_ci| 401     | Parameter error.               |
5870e41f4b71Sopenharmony_ci
5871e41f4b71Sopenharmony_ci**Example**
5872e41f4b71Sopenharmony_ci
5873e41f4b71Sopenharmony_ci```ts
5874e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
5875e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
5876e41f4b71Sopenharmony_ci
5877e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
5878e41f4b71Sopenharmony_cilet messageView = '';
5879e41f4b71Sopenharmony_cilet callback = (value: socket.SocketMessageInfo) => {
5880e41f4b71Sopenharmony_ci  for (let i: number = 0; i < value.message.byteLength; i++) {
5881e41f4b71Sopenharmony_ci    let uint8Array = new Uint8Array(value.message) 
5882e41f4b71Sopenharmony_ci    let messages = uint8Array[i]
5883e41f4b71Sopenharmony_ci    let message = String.fromCharCode(messages);
5884e41f4b71Sopenharmony_ci    messageView += message;
5885e41f4b71Sopenharmony_ci  }
5886e41f4b71Sopenharmony_ci  console.log('on message message: ' + JSON.stringify(messageView));
5887e41f4b71Sopenharmony_ci  console.log('remoteInfo: ' + JSON.stringify(value.remoteInfo));
5888e41f4b71Sopenharmony_ci}
5889e41f4b71Sopenharmony_citls.on('message', callback);
5890e41f4b71Sopenharmony_ci// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
5891e41f4b71Sopenharmony_citls.off('message', callback);
5892e41f4b71Sopenharmony_ci```
5893e41f4b71Sopenharmony_ci### on('connect' | 'close')<sup>9+</sup>
5894e41f4b71Sopenharmony_ci
5895e41f4b71Sopenharmony_cion(type: 'connect' | 'close', callback: Callback\<void\>): void
5896e41f4b71Sopenharmony_ci
5897e41f4b71Sopenharmony_ciSubscribes to **connect** or **close** events of the TLS socket connection. This API uses an asynchronous callback to return the result.
5898e41f4b71Sopenharmony_ci
5899e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
5900e41f4b71Sopenharmony_ci
5901e41f4b71Sopenharmony_ci**Parameters**
5902e41f4b71Sopenharmony_ci
5903e41f4b71Sopenharmony_ci| Name  | Type            | Mandatory| Description                                                        |
5904e41f4b71Sopenharmony_ci| -------- | ---------------- | ---- | ------------------------------------------------------------ |
5905e41f4b71Sopenharmony_ci| type     | string           | Yes  | Event type.<br>- **connect**: connection event.<br>- **close**: close event.|
5906e41f4b71Sopenharmony_ci| callback | Callback\<void\> | Yes  | Callback used to return the result.                                                    |
5907e41f4b71Sopenharmony_ci
5908e41f4b71Sopenharmony_ci**Error codes**
5909e41f4b71Sopenharmony_ci
5910e41f4b71Sopenharmony_ci| ID| Error Message                       |
5911e41f4b71Sopenharmony_ci| ------- | ------------------------------ |
5912e41f4b71Sopenharmony_ci| 401     | Parameter error.               |
5913e41f4b71Sopenharmony_ci
5914e41f4b71Sopenharmony_ci**Example**
5915e41f4b71Sopenharmony_ci
5916e41f4b71Sopenharmony_ci```ts
5917e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
5918e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
5919e41f4b71Sopenharmony_ci
5920e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
5921e41f4b71Sopenharmony_citls.on('connect', () => {
5922e41f4b71Sopenharmony_ci  console.log("on connect success")
5923e41f4b71Sopenharmony_ci});
5924e41f4b71Sopenharmony_citls.on('close', () => {
5925e41f4b71Sopenharmony_ci  console.log("on close success")
5926e41f4b71Sopenharmony_ci});
5927e41f4b71Sopenharmony_ci```
5928e41f4b71Sopenharmony_ci
5929e41f4b71Sopenharmony_ci### off('connect' | 'close')<sup>9+</sup>
5930e41f4b71Sopenharmony_ci
5931e41f4b71Sopenharmony_cioff(type: 'connect' | 'close', callback?: Callback\<void\>): void
5932e41f4b71Sopenharmony_ci
5933e41f4b71Sopenharmony_ciUnsubscribes from **connect** or **close** events of a **TLSSocket** object. This API uses an asynchronous callback to return the result.
5934e41f4b71Sopenharmony_ci
5935e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
5936e41f4b71Sopenharmony_ci
5937e41f4b71Sopenharmony_ci**Parameters**
5938e41f4b71Sopenharmony_ci
5939e41f4b71Sopenharmony_ci| Name  | Type            | Mandatory| Description                                                        |
5940e41f4b71Sopenharmony_ci| -------- | ---------------- | ---- | ------------------------------------------------------------ |
5941e41f4b71Sopenharmony_ci| type     | string           | Yes  | Event type.<br>- **connect**: connection event.<br>- **close**: close event.|
5942e41f4b71Sopenharmony_ci| callback | Callback\<void\> | No  | Callback used to return the result.           |
5943e41f4b71Sopenharmony_ci
5944e41f4b71Sopenharmony_ci**Error codes**
5945e41f4b71Sopenharmony_ci
5946e41f4b71Sopenharmony_ci| ID| Error Message                       |
5947e41f4b71Sopenharmony_ci| ------- | ------------------------------ |
5948e41f4b71Sopenharmony_ci| 401     | Parameter error.               |
5949e41f4b71Sopenharmony_ci
5950e41f4b71Sopenharmony_ci**Example**
5951e41f4b71Sopenharmony_ci
5952e41f4b71Sopenharmony_ci```ts
5953e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
5954e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
5955e41f4b71Sopenharmony_ci
5956e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
5957e41f4b71Sopenharmony_cilet callback1 = () => {
5958e41f4b71Sopenharmony_ci  console.log("on connect success");
5959e41f4b71Sopenharmony_ci}
5960e41f4b71Sopenharmony_citls.on('connect', callback1);
5961e41f4b71Sopenharmony_ci// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
5962e41f4b71Sopenharmony_citls.off('connect', callback1);
5963e41f4b71Sopenharmony_citls.off('connect');
5964e41f4b71Sopenharmony_cilet callback2 = () => {
5965e41f4b71Sopenharmony_ci  console.log("on close success");
5966e41f4b71Sopenharmony_ci}
5967e41f4b71Sopenharmony_citls.on('close', callback2);
5968e41f4b71Sopenharmony_ci// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
5969e41f4b71Sopenharmony_citls.off('close', callback2);
5970e41f4b71Sopenharmony_ci```
5971e41f4b71Sopenharmony_ci
5972e41f4b71Sopenharmony_ci### on('error')<sup>9+</sup>
5973e41f4b71Sopenharmony_ci
5974e41f4b71Sopenharmony_cion(type: 'error', callback: ErrorCallback): void
5975e41f4b71Sopenharmony_ci
5976e41f4b71Sopenharmony_ciSubscribes to **error** events of the TLS socket connection. This API uses an asynchronous callback to return the result.
5977e41f4b71Sopenharmony_ci
5978e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
5979e41f4b71Sopenharmony_ci
5980e41f4b71Sopenharmony_ci**Parameters**
5981e41f4b71Sopenharmony_ci
5982e41f4b71Sopenharmony_ci| Name  | Type         | Mandatory| Description                                |
5983e41f4b71Sopenharmony_ci| -------- | ------------- | ---- | ------------------------------------ |
5984e41f4b71Sopenharmony_ci| type     | string        | Yes  | Event type.<br/> **error**: error event.|
5985e41f4b71Sopenharmony_ci| callback | ErrorCallback | Yes  | Callback used to return the result.         |
5986e41f4b71Sopenharmony_ci
5987e41f4b71Sopenharmony_ci**Error codes**
5988e41f4b71Sopenharmony_ci
5989e41f4b71Sopenharmony_ci| ID| Error Message                       |
5990e41f4b71Sopenharmony_ci| ------- | ------------------------------ |
5991e41f4b71Sopenharmony_ci| 401     | Parameter error.               |
5992e41f4b71Sopenharmony_ci
5993e41f4b71Sopenharmony_ci**Example**
5994e41f4b71Sopenharmony_ci
5995e41f4b71Sopenharmony_ci```ts
5996e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
5997e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
5998e41f4b71Sopenharmony_ci
5999e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
6000e41f4b71Sopenharmony_citls.on('error', (err: BusinessError) => {
6001e41f4b71Sopenharmony_ci  console.log("on error, err:" + JSON.stringify(err))
6002e41f4b71Sopenharmony_ci});
6003e41f4b71Sopenharmony_ci```
6004e41f4b71Sopenharmony_ci
6005e41f4b71Sopenharmony_ci### off('error')<sup>9+</sup>
6006e41f4b71Sopenharmony_ci
6007e41f4b71Sopenharmony_cioff(type: 'error', callback?: ErrorCallback): void
6008e41f4b71Sopenharmony_ci
6009e41f4b71Sopenharmony_ciUnsubscribes from **error** events of a **TLSSocket** object. This API uses an asynchronous callback to return the result.
6010e41f4b71Sopenharmony_ci
6011e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
6012e41f4b71Sopenharmony_ci
6013e41f4b71Sopenharmony_ci**Parameters**
6014e41f4b71Sopenharmony_ci
6015e41f4b71Sopenharmony_ci| Name  | Type         | Mandatory| Description                                |
6016e41f4b71Sopenharmony_ci| -------- | ------------- | ---- | ------------------------------------ |
6017e41f4b71Sopenharmony_ci| type     | string        | Yes  | Event type.<br/> **error**: error event.|
6018e41f4b71Sopenharmony_ci| callback | ErrorCallback | No  | Callback used to return the result.                            |
6019e41f4b71Sopenharmony_ci
6020e41f4b71Sopenharmony_ci**Error codes**
6021e41f4b71Sopenharmony_ci
6022e41f4b71Sopenharmony_ci| ID| Error Message                       |
6023e41f4b71Sopenharmony_ci| ------- | ------------------------------ |
6024e41f4b71Sopenharmony_ci| 401     | Parameter error.               |
6025e41f4b71Sopenharmony_ci
6026e41f4b71Sopenharmony_ci**Example**
6027e41f4b71Sopenharmony_ci
6028e41f4b71Sopenharmony_ci```ts
6029e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
6030e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
6031e41f4b71Sopenharmony_ci
6032e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
6033e41f4b71Sopenharmony_cilet callback = (err: BusinessError) => {
6034e41f4b71Sopenharmony_ci  console.log("on error, err:" + JSON.stringify(err));
6035e41f4b71Sopenharmony_ci}
6036e41f4b71Sopenharmony_citls.on('error', callback);
6037e41f4b71Sopenharmony_ci// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
6038e41f4b71Sopenharmony_citls.off('error', callback);
6039e41f4b71Sopenharmony_ci```
6040e41f4b71Sopenharmony_ci
6041e41f4b71Sopenharmony_ci### connect<sup>9+</sup>
6042e41f4b71Sopenharmony_ci
6043e41f4b71Sopenharmony_ciconnect(options: TLSConnectOptions, callback: AsyncCallback\<void\>): void
6044e41f4b71Sopenharmony_ci
6045e41f4b71Sopenharmony_ciSets up a TLS socket connection, and creates and initializes a TLS session after **bind** is successfully called. During this process, a TLS/SSL handshake is performed between the application and the server to implement data transmission. This API uses an asynchronous callback to return the result. Note that **ca** in **secureOptions** of the **options** parameter is mandatory. You need to enter the CA certificate of the server for certificate authentication. The certificate content starts with "-----BEGIN CERTIFICATE-----" and ends with "-----END CERTIFICATE-----".
6046e41f4b71Sopenharmony_ci
6047e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
6048e41f4b71Sopenharmony_ci
6049e41f4b71Sopenharmony_ci**Parameters**
6050e41f4b71Sopenharmony_ci
6051e41f4b71Sopenharmony_ci| Name  | Type                                  | Mandatory| Description|
6052e41f4b71Sopenharmony_ci| -------- | ---------------------------------------| ----| --------------- |
6053e41f4b71Sopenharmony_ci| options  | [TLSConnectOptions](#tlsconnectoptions9) | Yes  | Parameters required for the TLS socket connection.|
6054e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void\>                  | Yes  | Callback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
6055e41f4b71Sopenharmony_ci
6056e41f4b71Sopenharmony_ci**Error codes**
6057e41f4b71Sopenharmony_ci
6058e41f4b71Sopenharmony_ci| ID| Error Message                                     |
6059e41f4b71Sopenharmony_ci| ------- | -------------------------------------------- |
6060e41f4b71Sopenharmony_ci| 401     | Parameter error.                             |
6061e41f4b71Sopenharmony_ci| 2303104 | Interrupted system call.                     |
6062e41f4b71Sopenharmony_ci| 2303109 | Bad file number.                             |
6063e41f4b71Sopenharmony_ci| 2303111 | Resource temporarily unavailable. Try again. |
6064e41f4b71Sopenharmony_ci| 2303188 | Socket operation on non-socket.              |
6065e41f4b71Sopenharmony_ci| 2303191 | Incorrect socket protocol type.              |
6066e41f4b71Sopenharmony_ci| 2303198 | Address already in use.                      |
6067e41f4b71Sopenharmony_ci| 2303199 | Cannot assign requested address.             |
6068e41f4b71Sopenharmony_ci| 2303210 | Connection timed out.                        |
6069e41f4b71Sopenharmony_ci| 2303501 | SSL is null.                                 |
6070e41f4b71Sopenharmony_ci| 2303502 | An error occurred when reading data on the TLS socket.|
6071e41f4b71Sopenharmony_ci| 2303503 | An error occurred when writing data on the TLS socket.|
6072e41f4b71Sopenharmony_ci| 2303505 | An error occurred in the TLS system call.    |
6073e41f4b71Sopenharmony_ci| 2303506 | Failed to close the TLS connection.          |
6074e41f4b71Sopenharmony_ci| 2300002 | System internal error.                       |
6075e41f4b71Sopenharmony_ci
6076e41f4b71Sopenharmony_ci**Example**
6077e41f4b71Sopenharmony_ci
6078e41f4b71Sopenharmony_ci```ts
6079e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
6080e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
6081e41f4b71Sopenharmony_ci
6082e41f4b71Sopenharmony_cilet tlsTwoWay: socket.TLSSocket = socket.constructTLSSocketInstance();  // Two way authentication
6083e41f4b71Sopenharmony_cilet bindAddr: socket.NetAddress = {
6084e41f4b71Sopenharmony_ci  address: '0.0.0.0',
6085e41f4b71Sopenharmony_ci}
6086e41f4b71Sopenharmony_citlsTwoWay.bind(bindAddr, (err: BusinessError) => {
6087e41f4b71Sopenharmony_ci  if (err) {
6088e41f4b71Sopenharmony_ci    console.log('bind fail');
6089e41f4b71Sopenharmony_ci    return;
6090e41f4b71Sopenharmony_ci  }
6091e41f4b71Sopenharmony_ci  console.log('bind success');
6092e41f4b71Sopenharmony_ci});
6093e41f4b71Sopenharmony_cilet twoWayNetAddr: socket.NetAddress = {
6094e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
6095e41f4b71Sopenharmony_ci  port: 8080
6096e41f4b71Sopenharmony_ci}
6097e41f4b71Sopenharmony_cilet twoWaySecureOptions: socket.TLSSecureOptions = {
6098e41f4b71Sopenharmony_ci  key: "xxxx",
6099e41f4b71Sopenharmony_ci  cert: "xxxx",
6100e41f4b71Sopenharmony_ci  ca: ["xxxx"],
6101e41f4b71Sopenharmony_ci  password: "xxxx",
6102e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
6103e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
6104e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
6105e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
6106e41f4b71Sopenharmony_ci}
6107e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
6108e41f4b71Sopenharmony_ci  address: twoWayNetAddr,
6109e41f4b71Sopenharmony_ci  secureOptions: twoWaySecureOptions,
6110e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
6111e41f4b71Sopenharmony_ci}
6112e41f4b71Sopenharmony_ci
6113e41f4b71Sopenharmony_citlsTwoWay.connect(tlsConnectOptions, (err: BusinessError) => {
6114e41f4b71Sopenharmony_ci  console.error("connect callback error" + err);
6115e41f4b71Sopenharmony_ci});
6116e41f4b71Sopenharmony_ci
6117e41f4b71Sopenharmony_cilet tlsOneWay: socket.TLSSocket = socket.constructTLSSocketInstance(); // One way authentication
6118e41f4b71Sopenharmony_citlsOneWay.bind(bindAddr, (err: BusinessError) => {
6119e41f4b71Sopenharmony_ci  if (err) {
6120e41f4b71Sopenharmony_ci    console.log('bind fail');
6121e41f4b71Sopenharmony_ci    return;
6122e41f4b71Sopenharmony_ci  }
6123e41f4b71Sopenharmony_ci  console.log('bind success');
6124e41f4b71Sopenharmony_ci});
6125e41f4b71Sopenharmony_cilet oneWayNetAddr: socket.NetAddress = {
6126e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
6127e41f4b71Sopenharmony_ci  port: 8080
6128e41f4b71Sopenharmony_ci}
6129e41f4b71Sopenharmony_cilet oneWaySecureOptions: socket.TLSSecureOptions = {
6130e41f4b71Sopenharmony_ci  ca: ["xxxx", "xxxx"],
6131e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
6132e41f4b71Sopenharmony_ci}
6133e41f4b71Sopenharmony_cilet tlsOneWayConnectOptions: socket.TLSConnectOptions = {
6134e41f4b71Sopenharmony_ci  address: oneWayNetAddr,
6135e41f4b71Sopenharmony_ci  secureOptions: oneWaySecureOptions
6136e41f4b71Sopenharmony_ci}
6137e41f4b71Sopenharmony_citlsOneWay.connect(tlsOneWayConnectOptions, (err: BusinessError) => {
6138e41f4b71Sopenharmony_ci  console.error("connect callback error" + err);
6139e41f4b71Sopenharmony_ci});
6140e41f4b71Sopenharmony_ci```
6141e41f4b71Sopenharmony_ci
6142e41f4b71Sopenharmony_ci### connect<sup>9+</sup>
6143e41f4b71Sopenharmony_ci
6144e41f4b71Sopenharmony_ciconnect(options: TLSConnectOptions): Promise\<void\>
6145e41f4b71Sopenharmony_ci
6146e41f4b71Sopenharmony_ciSets up a TLS socket connection, and creates and initializes a TLS session after **bind** is successfully called. During this process, a TLS/SSL handshake is performed between the application and the server to implement data transmission. Both two-way and one-way authentication modes are supported. This API uses a promise to return the result. Note that **ca** in **secureOptions** of the **options** parameter is mandatory. You need to enter the CA certificate of the server for certificate authentication. The certificate content starts with "-----BEGIN CERTIFICATE-----" and ends with "-----END CERTIFICATE-----".
6147e41f4b71Sopenharmony_ci
6148e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
6149e41f4b71Sopenharmony_ci
6150e41f4b71Sopenharmony_ci**Parameters**
6151e41f4b71Sopenharmony_ci
6152e41f4b71Sopenharmony_ci| Name  | Type                                  | Mandatory| Description|
6153e41f4b71Sopenharmony_ci| -------- | --------------------------------------| ----| --------------- |
6154e41f4b71Sopenharmony_ci| options  | [TLSConnectOptions](#tlsconnectoptions9) | Yes  | Parameters required for the connection.|
6155e41f4b71Sopenharmony_ci
6156e41f4b71Sopenharmony_ci**Return value**
6157e41f4b71Sopenharmony_ci
6158e41f4b71Sopenharmony_ci| Type                                       | Description                         |
6159e41f4b71Sopenharmony_ci| ------------------------------------------- | ----------------------------- |
6160e41f4b71Sopenharmony_ci| Promise\<void\>                              | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
6161e41f4b71Sopenharmony_ci
6162e41f4b71Sopenharmony_ci**Error codes**
6163e41f4b71Sopenharmony_ci
6164e41f4b71Sopenharmony_ci| ID| Error Message                                     |
6165e41f4b71Sopenharmony_ci| ------- | -------------------------------------------- |
6166e41f4b71Sopenharmony_ci| 401     | Parameter error.                             |
6167e41f4b71Sopenharmony_ci| 2303104 | Interrupted system call.                     |
6168e41f4b71Sopenharmony_ci| 2303109 | Bad file number.                             |
6169e41f4b71Sopenharmony_ci| 2303111 | Resource temporarily unavailable. Try again. |
6170e41f4b71Sopenharmony_ci| 2303188 | Socket operation on non-socket.              |
6171e41f4b71Sopenharmony_ci| 2303191 | Incorrect socket protocol type.              |
6172e41f4b71Sopenharmony_ci| 2303198 | Address already in use.                      |
6173e41f4b71Sopenharmony_ci| 2303199 | Cannot assign requested address.             |
6174e41f4b71Sopenharmony_ci| 2303210 | Connection timed out.                        |
6175e41f4b71Sopenharmony_ci| 2303501 | SSL is null.                                 |
6176e41f4b71Sopenharmony_ci| 2303502 | An error occurred when reading data on the TLS socket.|
6177e41f4b71Sopenharmony_ci| 2303503 | An error occurred when writing data on the TLS socket.|
6178e41f4b71Sopenharmony_ci| 2303505 | An error occurred in the TLS system call.    |
6179e41f4b71Sopenharmony_ci| 2303506 | Failed to close the TLS connection.          |
6180e41f4b71Sopenharmony_ci| 2300002 | System internal error.                       |
6181e41f4b71Sopenharmony_ci
6182e41f4b71Sopenharmony_ci**Example**
6183e41f4b71Sopenharmony_ci
6184e41f4b71Sopenharmony_ci```ts
6185e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
6186e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
6187e41f4b71Sopenharmony_ci
6188e41f4b71Sopenharmony_cilet tlsTwoWay: socket.TLSSocket = socket.constructTLSSocketInstance();  // Two way authentication
6189e41f4b71Sopenharmony_cilet bindAddr: socket.NetAddress = {
6190e41f4b71Sopenharmony_ci  address: '0.0.0.0',
6191e41f4b71Sopenharmony_ci}
6192e41f4b71Sopenharmony_citlsTwoWay.bind(bindAddr, (err: BusinessError) => {
6193e41f4b71Sopenharmony_ci  if (err) {
6194e41f4b71Sopenharmony_ci    console.log('bind fail');
6195e41f4b71Sopenharmony_ci    return;
6196e41f4b71Sopenharmony_ci  }
6197e41f4b71Sopenharmony_ci  console.log('bind success');
6198e41f4b71Sopenharmony_ci});
6199e41f4b71Sopenharmony_cilet twoWayNetAddr: socket.NetAddress = {
6200e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
6201e41f4b71Sopenharmony_ci  port: 8080
6202e41f4b71Sopenharmony_ci}
6203e41f4b71Sopenharmony_cilet twoWaySecureOptions: socket.TLSSecureOptions = {
6204e41f4b71Sopenharmony_ci  key: "xxxx",
6205e41f4b71Sopenharmony_ci  cert: "xxxx",
6206e41f4b71Sopenharmony_ci  ca: ["xxxx"],
6207e41f4b71Sopenharmony_ci  password: "xxxx",
6208e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
6209e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
6210e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
6211e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
6212e41f4b71Sopenharmony_ci}
6213e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
6214e41f4b71Sopenharmony_ci  address: twoWayNetAddr,
6215e41f4b71Sopenharmony_ci  secureOptions: twoWaySecureOptions,
6216e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
6217e41f4b71Sopenharmony_ci}
6218e41f4b71Sopenharmony_ci
6219e41f4b71Sopenharmony_citlsTwoWay.connect(tlsConnectOptions).then(() => {
6220e41f4b71Sopenharmony_ci  console.log("connect successfully");
6221e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
6222e41f4b71Sopenharmony_ci  console.log("connect failed " + JSON.stringify(err));
6223e41f4b71Sopenharmony_ci});
6224e41f4b71Sopenharmony_ci
6225e41f4b71Sopenharmony_cilet tlsOneWay: socket.TLSSocket = socket.constructTLSSocketInstance(); // One way authentication
6226e41f4b71Sopenharmony_citlsOneWay.bind(bindAddr, (err: BusinessError) => {
6227e41f4b71Sopenharmony_ci  if (err) {
6228e41f4b71Sopenharmony_ci    console.log('bind fail');
6229e41f4b71Sopenharmony_ci    return;
6230e41f4b71Sopenharmony_ci  }
6231e41f4b71Sopenharmony_ci  console.log('bind success');
6232e41f4b71Sopenharmony_ci});
6233e41f4b71Sopenharmony_cilet oneWayNetAddr: socket.NetAddress = {
6234e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
6235e41f4b71Sopenharmony_ci  port: 8080
6236e41f4b71Sopenharmony_ci}
6237e41f4b71Sopenharmony_cilet oneWaySecureOptions: socket.TLSSecureOptions = {
6238e41f4b71Sopenharmony_ci  ca: ["xxxx", "xxxx"],
6239e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
6240e41f4b71Sopenharmony_ci}
6241e41f4b71Sopenharmony_cilet tlsOneWayConnectOptions: socket.TLSConnectOptions = {
6242e41f4b71Sopenharmony_ci  address: oneWayNetAddr,
6243e41f4b71Sopenharmony_ci  secureOptions: oneWaySecureOptions
6244e41f4b71Sopenharmony_ci}
6245e41f4b71Sopenharmony_citlsOneWay.connect(tlsOneWayConnectOptions).then(() => {
6246e41f4b71Sopenharmony_ci  console.log("connect successfully");
6247e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
6248e41f4b71Sopenharmony_ci  console.log("connect failed " + JSON.stringify(err));
6249e41f4b71Sopenharmony_ci});
6250e41f4b71Sopenharmony_ci```
6251e41f4b71Sopenharmony_ci
6252e41f4b71Sopenharmony_ci### getRemoteAddress<sup>9+</sup>
6253e41f4b71Sopenharmony_ci
6254e41f4b71Sopenharmony_cigetRemoteAddress(callback: AsyncCallback\<NetAddress\>): void
6255e41f4b71Sopenharmony_ci
6256e41f4b71Sopenharmony_ciObtains the remote address of a TLS socket connection. This API uses an asynchronous callback to return the result.
6257e41f4b71Sopenharmony_ci
6258e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
6259e41f4b71Sopenharmony_ci
6260e41f4b71Sopenharmony_ci**Parameters**
6261e41f4b71Sopenharmony_ci
6262e41f4b71Sopenharmony_ci| Name  | Type                                             | Mandatory| Description      |
6263e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | ---- | ---------- |
6264e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[NetAddress](#netaddress)\> | Yes  | Callback used to return the result. If the operation is successful, the remote address is returned. If the operation fails, an error message is returned.|
6265e41f4b71Sopenharmony_ci
6266e41f4b71Sopenharmony_ci**Error codes**
6267e41f4b71Sopenharmony_ci
6268e41f4b71Sopenharmony_ci| ID| Error Message                       |
6269e41f4b71Sopenharmony_ci| ------- | -----------------------------  |
6270e41f4b71Sopenharmony_ci| 2303188 | Socket operation on non-socket.|
6271e41f4b71Sopenharmony_ci| 2300002 | System internal error.         |
6272e41f4b71Sopenharmony_ci
6273e41f4b71Sopenharmony_ci**Example**
6274e41f4b71Sopenharmony_ci
6275e41f4b71Sopenharmony_ci```ts
6276e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
6277e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
6278e41f4b71Sopenharmony_ci
6279e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
6280e41f4b71Sopenharmony_citls.getRemoteAddress((err: BusinessError, data: socket.NetAddress) => {
6281e41f4b71Sopenharmony_ci  if (err) {
6282e41f4b71Sopenharmony_ci    console.log('getRemoteAddress fail');
6283e41f4b71Sopenharmony_ci    return;
6284e41f4b71Sopenharmony_ci  }
6285e41f4b71Sopenharmony_ci  console.log('getRemoteAddress success:' + JSON.stringify(data));
6286e41f4b71Sopenharmony_ci});
6287e41f4b71Sopenharmony_ci```
6288e41f4b71Sopenharmony_ci
6289e41f4b71Sopenharmony_ci### getRemoteAddress<sup>9+</sup>
6290e41f4b71Sopenharmony_ci
6291e41f4b71Sopenharmony_cigetRemoteAddress(): Promise\<NetAddress\>
6292e41f4b71Sopenharmony_ci
6293e41f4b71Sopenharmony_ciObtains the remote address of a TLS socket connection. This API uses a promise to return the result.
6294e41f4b71Sopenharmony_ci
6295e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
6296e41f4b71Sopenharmony_ci
6297e41f4b71Sopenharmony_ci**Return value**
6298e41f4b71Sopenharmony_ci
6299e41f4b71Sopenharmony_ci| Type                                       | Description                                       |
6300e41f4b71Sopenharmony_ci|  ------------------------------------------ |  ------------------------------------------ |
6301e41f4b71Sopenharmony_ci| Promise\<[NetAddress](#netaddress)\> | Promise used to return the result. If the operation fails, an error message is returned.|
6302e41f4b71Sopenharmony_ci
6303e41f4b71Sopenharmony_ci**Error codes**
6304e41f4b71Sopenharmony_ci
6305e41f4b71Sopenharmony_ci| ID| Error Message                       |
6306e41f4b71Sopenharmony_ci| ------- | ------------------------------ |
6307e41f4b71Sopenharmony_ci| 2303188 | Socket operation on non-socket.|
6308e41f4b71Sopenharmony_ci| 2300002 | System internal error.         |
6309e41f4b71Sopenharmony_ci
6310e41f4b71Sopenharmony_ci**Example**
6311e41f4b71Sopenharmony_ci
6312e41f4b71Sopenharmony_ci```ts
6313e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
6314e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
6315e41f4b71Sopenharmony_ci
6316e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
6317e41f4b71Sopenharmony_citls.getRemoteAddress().then(() => {
6318e41f4b71Sopenharmony_ci  console.log('getRemoteAddress success');
6319e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
6320e41f4b71Sopenharmony_ci  console.log('getRemoteAddress fail');
6321e41f4b71Sopenharmony_ci});
6322e41f4b71Sopenharmony_ci```
6323e41f4b71Sopenharmony_ci
6324e41f4b71Sopenharmony_ci### getCertificate<sup>9+</sup>
6325e41f4b71Sopenharmony_ci
6326e41f4b71Sopenharmony_cigetCertificate(callback: AsyncCallback\<[X509CertRawData](#x509certrawdata9)\>): void
6327e41f4b71Sopenharmony_ci
6328e41f4b71Sopenharmony_ciObtains the local digital certificate after a TLS socket connection is established. This API is applicable to two-way authentication. It uses an asynchronous callback to return the result.
6329e41f4b71Sopenharmony_ci
6330e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
6331e41f4b71Sopenharmony_ci
6332e41f4b71Sopenharmony_ci**Parameters**
6333e41f4b71Sopenharmony_ci
6334e41f4b71Sopenharmony_ci| Name  | Type                                  | Mandatory| Description|
6335e41f4b71Sopenharmony_ci| -------- | ----------------------------------------| ---- | ---------------|
6336e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[X509CertRawData](#x509certrawdata9)\>    | Yes  | Callback used to return the result. If the operation is successful, the local certificate is returned. If the operation fails, an error message is returned.|
6337e41f4b71Sopenharmony_ci
6338e41f4b71Sopenharmony_ci**Error codes**
6339e41f4b71Sopenharmony_ci
6340e41f4b71Sopenharmony_ci| ID| Error Message                       |
6341e41f4b71Sopenharmony_ci| ------- | ------------------------------ |
6342e41f4b71Sopenharmony_ci| 2303501 | SSL is null.                   |
6343e41f4b71Sopenharmony_ci| 2303504 | An error occurred when verifying the X.509 certificate.|
6344e41f4b71Sopenharmony_ci| 2300002 | System internal error.         |
6345e41f4b71Sopenharmony_ci
6346e41f4b71Sopenharmony_ci**Example**
6347e41f4b71Sopenharmony_ci
6348e41f4b71Sopenharmony_ci```ts
6349e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
6350e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
6351e41f4b71Sopenharmony_ci
6352e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
6353e41f4b71Sopenharmony_citls.getCertificate((err: BusinessError, data: socket.X509CertRawData) => {
6354e41f4b71Sopenharmony_ci  if (err) {
6355e41f4b71Sopenharmony_ci    console.log("getCertificate callback error = " + err);
6356e41f4b71Sopenharmony_ci  } else {
6357e41f4b71Sopenharmony_ci    console.log("getCertificate callback = " + data);
6358e41f4b71Sopenharmony_ci  }
6359e41f4b71Sopenharmony_ci});
6360e41f4b71Sopenharmony_ci```
6361e41f4b71Sopenharmony_ci
6362e41f4b71Sopenharmony_ci### getCertificate<sup>9+</sup>
6363e41f4b71Sopenharmony_ci
6364e41f4b71Sopenharmony_cigetCertificate():Promise\<[X509CertRawData](#x509certrawdata9)\>
6365e41f4b71Sopenharmony_ci
6366e41f4b71Sopenharmony_ciObtains the local digital certificate after a TLS socket connection is established. This API is applicable to two-way authentication. It uses a promise to return the result.
6367e41f4b71Sopenharmony_ci
6368e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
6369e41f4b71Sopenharmony_ci
6370e41f4b71Sopenharmony_ci**Return value**
6371e41f4b71Sopenharmony_ci
6372e41f4b71Sopenharmony_ci| Type           | Description                 |
6373e41f4b71Sopenharmony_ci| -------------- | -------------------- |
6374e41f4b71Sopenharmony_ci| Promise\<[X509CertRawData](#x509certrawdata9)\> | Promise used to return the result. If the operation fails, an error message is returned.|
6375e41f4b71Sopenharmony_ci
6376e41f4b71Sopenharmony_ci**Error codes**
6377e41f4b71Sopenharmony_ci
6378e41f4b71Sopenharmony_ci| ID| Error Message                       |
6379e41f4b71Sopenharmony_ci| ------- | ------------------------------ |
6380e41f4b71Sopenharmony_ci| 2303501 | SSL is null.                   |
6381e41f4b71Sopenharmony_ci| 2303504 | An error occurred when verifying the X.509 certificate.|
6382e41f4b71Sopenharmony_ci| 2300002 | System internal error.         |
6383e41f4b71Sopenharmony_ci
6384e41f4b71Sopenharmony_ci**Example**
6385e41f4b71Sopenharmony_ci
6386e41f4b71Sopenharmony_ci```ts
6387e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
6388e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
6389e41f4b71Sopenharmony_ciimport { util } from '@kit.ArkTS';
6390e41f4b71Sopenharmony_ci
6391e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
6392e41f4b71Sopenharmony_citls.getCertificate().then((data: socket.X509CertRawData) => {
6393e41f4b71Sopenharmony_ci  const decoder = util.TextDecoder.create();
6394e41f4b71Sopenharmony_ci  const str = decoder.decodeWithStream(data.data);
6395e41f4b71Sopenharmony_ci  console.log("getCertificate: " + str);
6396e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
6397e41f4b71Sopenharmony_ci  console.error("failed" + err);
6398e41f4b71Sopenharmony_ci});
6399e41f4b71Sopenharmony_ci```
6400e41f4b71Sopenharmony_ci
6401e41f4b71Sopenharmony_ci### getRemoteCertificate<sup>9+</sup>
6402e41f4b71Sopenharmony_ci
6403e41f4b71Sopenharmony_cigetRemoteCertificate(callback: AsyncCallback\<[X509CertRawData](#x509certrawdata9)\>): void
6404e41f4b71Sopenharmony_ci
6405e41f4b71Sopenharmony_ciObtains the digital certificate of the server after a TLS socket connection is established. This API uses an asynchronous callback to return the result.
6406e41f4b71Sopenharmony_ci
6407e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
6408e41f4b71Sopenharmony_ci
6409e41f4b71Sopenharmony_ci**Parameters**
6410e41f4b71Sopenharmony_ci
6411e41f4b71Sopenharmony_ci| Name   | Type                                   | Mandatory | Description          |
6412e41f4b71Sopenharmony_ci| -------- | ----------------------------------------| ---- | ---------------|
6413e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[X509CertRawData](#x509certrawdata9)\>  | Yes  | Callback used to return the result. If the operation fails, an error message is returned.|
6414e41f4b71Sopenharmony_ci
6415e41f4b71Sopenharmony_ci**Error codes**
6416e41f4b71Sopenharmony_ci
6417e41f4b71Sopenharmony_ci| ID| Error Message                       |
6418e41f4b71Sopenharmony_ci| ------- | ------------------------------ |
6419e41f4b71Sopenharmony_ci| 2303501 | SSL is null.                   |
6420e41f4b71Sopenharmony_ci| 2300002 | System internal error.         |
6421e41f4b71Sopenharmony_ci
6422e41f4b71Sopenharmony_ci**Example**
6423e41f4b71Sopenharmony_ci
6424e41f4b71Sopenharmony_ci```ts
6425e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
6426e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
6427e41f4b71Sopenharmony_ciimport { util } from '@kit.ArkTS';
6428e41f4b71Sopenharmony_ci
6429e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
6430e41f4b71Sopenharmony_citls.getRemoteCertificate((err: BusinessError, data: socket.X509CertRawData) => {
6431e41f4b71Sopenharmony_ci  if (err) {
6432e41f4b71Sopenharmony_ci    console.log("getRemoteCertificate callback error = " + err);
6433e41f4b71Sopenharmony_ci  } else {
6434e41f4b71Sopenharmony_ci    const decoder = util.TextDecoder.create();
6435e41f4b71Sopenharmony_ci    const str = decoder.decodeWithStream(data.data);
6436e41f4b71Sopenharmony_ci    console.log("getRemoteCertificate callback = " + str);
6437e41f4b71Sopenharmony_ci  }
6438e41f4b71Sopenharmony_ci});
6439e41f4b71Sopenharmony_ci```
6440e41f4b71Sopenharmony_ci
6441e41f4b71Sopenharmony_ci### getRemoteCertificate<sup>9+</sup>
6442e41f4b71Sopenharmony_ci
6443e41f4b71Sopenharmony_cigetRemoteCertificate():Promise\<[X509CertRawData](#x509certrawdata9)\>
6444e41f4b71Sopenharmony_ci
6445e41f4b71Sopenharmony_ciObtains the digital certificate of the server after a TLS socket connection is established. This API uses a promise to return the result.
6446e41f4b71Sopenharmony_ci
6447e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
6448e41f4b71Sopenharmony_ci
6449e41f4b71Sopenharmony_ci**Return value**
6450e41f4b71Sopenharmony_ci
6451e41f4b71Sopenharmony_ci| Type           | Description                 |
6452e41f4b71Sopenharmony_ci| -------------- | -------------------- |
6453e41f4b71Sopenharmony_ci| Promise\<[X509CertRawData](#x509certrawdata9)\> | Promise used to return the result. If the operation fails, an error message is returned.|
6454e41f4b71Sopenharmony_ci
6455e41f4b71Sopenharmony_ci**Error codes**
6456e41f4b71Sopenharmony_ci
6457e41f4b71Sopenharmony_ci| ID| Error Message                       |
6458e41f4b71Sopenharmony_ci| ------- | ------------------------------ |
6459e41f4b71Sopenharmony_ci| 2303501 | SSL is null.                   |
6460e41f4b71Sopenharmony_ci| 2300002 | System internal error.         |
6461e41f4b71Sopenharmony_ci
6462e41f4b71Sopenharmony_ci**Example**
6463e41f4b71Sopenharmony_ci
6464e41f4b71Sopenharmony_ci```ts
6465e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
6466e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
6467e41f4b71Sopenharmony_ciimport { util } from '@kit.ArkTS';
6468e41f4b71Sopenharmony_ci
6469e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
6470e41f4b71Sopenharmony_citls.getRemoteCertificate().then((data: socket.X509CertRawData) => {
6471e41f4b71Sopenharmony_ci  const decoder = util.TextDecoder.create();
6472e41f4b71Sopenharmony_ci  const str = decoder.decodeWithStream(data.data);
6473e41f4b71Sopenharmony_ci  console.log("getRemoteCertificate:" + str);
6474e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
6475e41f4b71Sopenharmony_ci  console.error("failed" + err);
6476e41f4b71Sopenharmony_ci});
6477e41f4b71Sopenharmony_ci```
6478e41f4b71Sopenharmony_ci
6479e41f4b71Sopenharmony_ci### getProtocol<sup>9+</sup>
6480e41f4b71Sopenharmony_ci
6481e41f4b71Sopenharmony_cigetProtocol(callback: AsyncCallback\<string\>): void
6482e41f4b71Sopenharmony_ci
6483e41f4b71Sopenharmony_ciObtains the communication protocol version after a TLS socket connection is established. This API uses an asynchronous callback to return the result.
6484e41f4b71Sopenharmony_ci
6485e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
6486e41f4b71Sopenharmony_ci
6487e41f4b71Sopenharmony_ci**Parameters**
6488e41f4b71Sopenharmony_ci
6489e41f4b71Sopenharmony_ci| Name  | Type                                      | Mandatory| Description          |
6490e41f4b71Sopenharmony_ci| -------- | ----------------------------------------| ---- | ---------------|
6491e41f4b71Sopenharmony_ci| callback | AsyncCallback\<string\>                  | Yes  | Callback used to return the result. If the operation fails, an error message is returned.|
6492e41f4b71Sopenharmony_ci
6493e41f4b71Sopenharmony_ci**Error codes**
6494e41f4b71Sopenharmony_ci
6495e41f4b71Sopenharmony_ci| ID| Error Message                       |
6496e41f4b71Sopenharmony_ci| ------- | -----------------------------  |
6497e41f4b71Sopenharmony_ci| 2303501 | SSL is null.                   |
6498e41f4b71Sopenharmony_ci| 2303505 | An error occurred in the TLS system call. |
6499e41f4b71Sopenharmony_ci| 2300002 | System internal error.         |
6500e41f4b71Sopenharmony_ci
6501e41f4b71Sopenharmony_ci**Example**
6502e41f4b71Sopenharmony_ci
6503e41f4b71Sopenharmony_ci```ts
6504e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
6505e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
6506e41f4b71Sopenharmony_ci
6507e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
6508e41f4b71Sopenharmony_citls.getProtocol((err: BusinessError, data: string) => {
6509e41f4b71Sopenharmony_ci  if (err) {
6510e41f4b71Sopenharmony_ci    console.log("getProtocol callback error = " + err);
6511e41f4b71Sopenharmony_ci  } else {
6512e41f4b71Sopenharmony_ci    console.log("getProtocol callback = " + data);
6513e41f4b71Sopenharmony_ci  }
6514e41f4b71Sopenharmony_ci});
6515e41f4b71Sopenharmony_ci```
6516e41f4b71Sopenharmony_ci
6517e41f4b71Sopenharmony_ci### getProtocol<sup>9+</sup>
6518e41f4b71Sopenharmony_ci
6519e41f4b71Sopenharmony_cigetProtocol():Promise\<string\>
6520e41f4b71Sopenharmony_ci
6521e41f4b71Sopenharmony_ciObtains the communication protocol version after a TLS socket connection is established. This API uses a promise to return the result.
6522e41f4b71Sopenharmony_ci
6523e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
6524e41f4b71Sopenharmony_ci
6525e41f4b71Sopenharmony_ci**Return value**
6526e41f4b71Sopenharmony_ci
6527e41f4b71Sopenharmony_ci| Type           | Description                 |
6528e41f4b71Sopenharmony_ci| -------------- | -------------------- |
6529e41f4b71Sopenharmony_ci| Promise\<string\> | Promise used to return the result. If the operation fails, an error message is returned.|
6530e41f4b71Sopenharmony_ci
6531e41f4b71Sopenharmony_ci**Error codes**
6532e41f4b71Sopenharmony_ci
6533e41f4b71Sopenharmony_ci| ID| Error Message                       |
6534e41f4b71Sopenharmony_ci| ------- | ------------------------------ |
6535e41f4b71Sopenharmony_ci| 2303501 | SSL is null.                   |
6536e41f4b71Sopenharmony_ci| 2303505 | An error occurred in the TLS system call. |
6537e41f4b71Sopenharmony_ci| 2300002 | System internal error.         |
6538e41f4b71Sopenharmony_ci
6539e41f4b71Sopenharmony_ci**Example**
6540e41f4b71Sopenharmony_ci
6541e41f4b71Sopenharmony_ci```ts
6542e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
6543e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
6544e41f4b71Sopenharmony_ci
6545e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
6546e41f4b71Sopenharmony_citls.getProtocol().then((data: string) => {
6547e41f4b71Sopenharmony_ci  console.log(data);
6548e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
6549e41f4b71Sopenharmony_ci  console.error("failed" + err);
6550e41f4b71Sopenharmony_ci});
6551e41f4b71Sopenharmony_ci```
6552e41f4b71Sopenharmony_ci
6553e41f4b71Sopenharmony_ci### getCipherSuite<sup>9+</sup>
6554e41f4b71Sopenharmony_ci
6555e41f4b71Sopenharmony_cigetCipherSuite(callback: AsyncCallback\<Array\<string\>\>): void
6556e41f4b71Sopenharmony_ci
6557e41f4b71Sopenharmony_ciObtains the cipher suite negotiated by both communication parties after a TLS socket connection is established. This API uses an asynchronous callback to return the result.
6558e41f4b71Sopenharmony_ci
6559e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
6560e41f4b71Sopenharmony_ci
6561e41f4b71Sopenharmony_ci**Parameters**
6562e41f4b71Sopenharmony_ci
6563e41f4b71Sopenharmony_ci| Name  | Type                                    | Mandatory| Description|
6564e41f4b71Sopenharmony_ci| -------- | ----------------------------------------| ---- | ---------------|
6565e41f4b71Sopenharmony_ci| callback | AsyncCallback\<Array\<string\>\>          | Yes  | Callback used to return the result. If the operation fails, an error message is returned.|
6566e41f4b71Sopenharmony_ci
6567e41f4b71Sopenharmony_ci**Error codes**
6568e41f4b71Sopenharmony_ci
6569e41f4b71Sopenharmony_ci| ID| Error Message                       |
6570e41f4b71Sopenharmony_ci| ------- | ------------------------------ |
6571e41f4b71Sopenharmony_ci| 2303501 | SSL is null.                   |
6572e41f4b71Sopenharmony_ci| 2303502 | An error occurred when reading data on the TLS socket.|
6573e41f4b71Sopenharmony_ci| 2303505 | An error occurred in the TLS system call. |
6574e41f4b71Sopenharmony_ci| 2300002 | System internal error.         |
6575e41f4b71Sopenharmony_ci
6576e41f4b71Sopenharmony_ci**Example**
6577e41f4b71Sopenharmony_ci
6578e41f4b71Sopenharmony_ci```ts
6579e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
6580e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
6581e41f4b71Sopenharmony_ci
6582e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
6583e41f4b71Sopenharmony_citls.getCipherSuite((err: BusinessError, data: Array<string>) => {
6584e41f4b71Sopenharmony_ci  if (err) {
6585e41f4b71Sopenharmony_ci    console.log("getCipherSuite callback error = " + err);
6586e41f4b71Sopenharmony_ci  } else {
6587e41f4b71Sopenharmony_ci    console.log("getCipherSuite callback = " + data);
6588e41f4b71Sopenharmony_ci  }
6589e41f4b71Sopenharmony_ci});
6590e41f4b71Sopenharmony_ci```
6591e41f4b71Sopenharmony_ci
6592e41f4b71Sopenharmony_ci### getCipherSuite<sup>9+</sup>
6593e41f4b71Sopenharmony_ci
6594e41f4b71Sopenharmony_cigetCipherSuite(): Promise\<Array\<string\>\>
6595e41f4b71Sopenharmony_ci
6596e41f4b71Sopenharmony_ciObtains the cipher suite negotiated by both communication parties after a TLS socket connection is established. This API uses a promise to return the result.
6597e41f4b71Sopenharmony_ci
6598e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
6599e41f4b71Sopenharmony_ci
6600e41f4b71Sopenharmony_ci**Return value**
6601e41f4b71Sopenharmony_ci
6602e41f4b71Sopenharmony_ci| Type                   | Description                 |
6603e41f4b71Sopenharmony_ci| ---------------------- | --------------------- |
6604e41f4b71Sopenharmony_ci| Promise\<Array\<string\>\> | Promise used to return the result. If the operation fails, an error message is returned.|
6605e41f4b71Sopenharmony_ci
6606e41f4b71Sopenharmony_ci**Error codes**
6607e41f4b71Sopenharmony_ci
6608e41f4b71Sopenharmony_ci| ID| Error Message                       |
6609e41f4b71Sopenharmony_ci| ------- | ------------------------------ |
6610e41f4b71Sopenharmony_ci| 2303501 | SSL is null.                   |
6611e41f4b71Sopenharmony_ci| 2303502 | An error occurred when reading data on the TLS socket.|
6612e41f4b71Sopenharmony_ci| 2303505 | An error occurred in the TLS system call. |
6613e41f4b71Sopenharmony_ci| 2300002 | System internal error.         |
6614e41f4b71Sopenharmony_ci
6615e41f4b71Sopenharmony_ci**Example**
6616e41f4b71Sopenharmony_ci
6617e41f4b71Sopenharmony_ci```ts
6618e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
6619e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
6620e41f4b71Sopenharmony_ci
6621e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
6622e41f4b71Sopenharmony_citls.getCipherSuite().then((data: Array<string>) => {
6623e41f4b71Sopenharmony_ci  console.log('getCipherSuite success:' + JSON.stringify(data));
6624e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
6625e41f4b71Sopenharmony_ci  console.error("failed" + err);
6626e41f4b71Sopenharmony_ci});
6627e41f4b71Sopenharmony_ci```
6628e41f4b71Sopenharmony_ci
6629e41f4b71Sopenharmony_ci### getSignatureAlgorithms<sup>9+</sup>
6630e41f4b71Sopenharmony_ci
6631e41f4b71Sopenharmony_cigetSignatureAlgorithms(callback: AsyncCallback\<Array\<string\>\>): void
6632e41f4b71Sopenharmony_ci
6633e41f4b71Sopenharmony_ciObtains the signing algorithm negotiated by both communication parties after a TLS socket connection is established. This API is applicable to two-way authentication. It uses an asynchronous callback to return the result.
6634e41f4b71Sopenharmony_ci
6635e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
6636e41f4b71Sopenharmony_ci
6637e41f4b71Sopenharmony_ci**Parameters**
6638e41f4b71Sopenharmony_ci
6639e41f4b71Sopenharmony_ci| Name  | Type                                  | Mandatory| Description           |
6640e41f4b71Sopenharmony_ci| -------- | -------------------------------------| ---- | ---------------|
6641e41f4b71Sopenharmony_ci| callback | AsyncCallback\<Array\<string\>\>         | Yes  | Callback used to return the result.  |
6642e41f4b71Sopenharmony_ci
6643e41f4b71Sopenharmony_ci**Error codes**
6644e41f4b71Sopenharmony_ci
6645e41f4b71Sopenharmony_ci| ID| Error Message                       |
6646e41f4b71Sopenharmony_ci| ------- | ------------------------------ |
6647e41f4b71Sopenharmony_ci| 2303501 | SSL is null.                   |
6648e41f4b71Sopenharmony_ci| 2300002 | System internal error.         |
6649e41f4b71Sopenharmony_ci
6650e41f4b71Sopenharmony_ci**Example**
6651e41f4b71Sopenharmony_ci
6652e41f4b71Sopenharmony_ci```ts
6653e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
6654e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
6655e41f4b71Sopenharmony_ci
6656e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
6657e41f4b71Sopenharmony_citls.getSignatureAlgorithms((err: BusinessError, data: Array<string>) => {
6658e41f4b71Sopenharmony_ci  if (err) {
6659e41f4b71Sopenharmony_ci    console.log("getSignatureAlgorithms callback error = " + err);
6660e41f4b71Sopenharmony_ci  } else {
6661e41f4b71Sopenharmony_ci    console.log("getSignatureAlgorithms callback = " + data);
6662e41f4b71Sopenharmony_ci  }
6663e41f4b71Sopenharmony_ci});
6664e41f4b71Sopenharmony_ci```
6665e41f4b71Sopenharmony_ci
6666e41f4b71Sopenharmony_ci### getSignatureAlgorithms<sup>9+</sup>
6667e41f4b71Sopenharmony_ci
6668e41f4b71Sopenharmony_cigetSignatureAlgorithms(): Promise\<Array\<string\>\>
6669e41f4b71Sopenharmony_ci
6670e41f4b71Sopenharmony_ciObtains the signing algorithm negotiated by both communication parties after a TLS socket connection is established. This API is applicable to two-way authentication. It uses a promise to return the result.
6671e41f4b71Sopenharmony_ci
6672e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
6673e41f4b71Sopenharmony_ci
6674e41f4b71Sopenharmony_ci**Return value**
6675e41f4b71Sopenharmony_ci
6676e41f4b71Sopenharmony_ci| Type                   | Description                 |
6677e41f4b71Sopenharmony_ci| ---------------------- | -------------------- |
6678e41f4b71Sopenharmony_ci| Promise\<Array\<string\>\> | Promise used to return the result.|
6679e41f4b71Sopenharmony_ci
6680e41f4b71Sopenharmony_ci**Error codes**
6681e41f4b71Sopenharmony_ci
6682e41f4b71Sopenharmony_ci| ID| Error Message                       |
6683e41f4b71Sopenharmony_ci| ------- | ------------------------------ |
6684e41f4b71Sopenharmony_ci| 2303501 | SSL is null.                   |
6685e41f4b71Sopenharmony_ci| 2300002 | System internal error.         |
6686e41f4b71Sopenharmony_ci
6687e41f4b71Sopenharmony_ci**Example**
6688e41f4b71Sopenharmony_ci
6689e41f4b71Sopenharmony_ci```ts
6690e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
6691e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
6692e41f4b71Sopenharmony_ci
6693e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
6694e41f4b71Sopenharmony_citls.getSignatureAlgorithms().then((data: Array<string>) => {
6695e41f4b71Sopenharmony_ci  console.log("getSignatureAlgorithms success" + data);
6696e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
6697e41f4b71Sopenharmony_ci  console.error("failed" + err);
6698e41f4b71Sopenharmony_ci});
6699e41f4b71Sopenharmony_ci```
6700e41f4b71Sopenharmony_ci
6701e41f4b71Sopenharmony_ci### getLocalAddress<sup>12+</sup>
6702e41f4b71Sopenharmony_ci
6703e41f4b71Sopenharmony_cigetLocalAddress(): Promise\<NetAddress\>
6704e41f4b71Sopenharmony_ci
6705e41f4b71Sopenharmony_ciObtains the local socket address of a **TLSSocket** connection. This API uses a promise to return the result.
6706e41f4b71Sopenharmony_ci
6707e41f4b71Sopenharmony_ci> **NOTE**
6708e41f4b71Sopenharmony_ci> Call this API only after the **TLSSocketServer** connection is successfully established.
6709e41f4b71Sopenharmony_ci
6710e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
6711e41f4b71Sopenharmony_ci
6712e41f4b71Sopenharmony_ci**Return value**
6713e41f4b71Sopenharmony_ci
6714e41f4b71Sopenharmony_ci| Type           | Description                                                |
6715e41f4b71Sopenharmony_ci|  -------------- |  --------------------------------------------------- |
6716e41f4b71Sopenharmony_ci| Promise\<[NetAddress](#netaddress)\> | Promise used to return the result.|
6717e41f4b71Sopenharmony_ci
6718e41f4b71Sopenharmony_ci**Error codes**
6719e41f4b71Sopenharmony_ci
6720e41f4b71Sopenharmony_ci| ID| Error Message                                   |
6721e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- |
6722e41f4b71Sopenharmony_ci| 2300002  | System internal error.                      |
6723e41f4b71Sopenharmony_ci| 2301009  | Bad file descriptor.                            |
6724e41f4b71Sopenharmony_ci| 2303188  | Socket operation on non-socket. |
6725e41f4b71Sopenharmony_ci
6726e41f4b71Sopenharmony_ci**Example**
6727e41f4b71Sopenharmony_ci
6728e41f4b71Sopenharmony_ci```ts
6729e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
6730e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
6731e41f4b71Sopenharmony_ci
6732e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
6733e41f4b71Sopenharmony_citls.getLocalAddress().then((localAddress: socket.NetAddress) => {
6734e41f4b71Sopenharmony_ci  console.info("Get success: " + JSON.stringify(localAddress));
6735e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
6736e41f4b71Sopenharmony_ci  console.error("Get failed, error: " + JSON.stringify(err));
6737e41f4b71Sopenharmony_ci})
6738e41f4b71Sopenharmony_ci```
6739e41f4b71Sopenharmony_ci
6740e41f4b71Sopenharmony_ci### send<sup>9+</sup>
6741e41f4b71Sopenharmony_ci
6742e41f4b71Sopenharmony_cisend(data: string \| ArrayBuffer, callback: AsyncCallback\<void\>): void
6743e41f4b71Sopenharmony_ci
6744e41f4b71Sopenharmony_ciSends a message to the server after a TLS socket connection is established. This API uses an asynchronous callback to return the result.
6745e41f4b71Sopenharmony_ci
6746e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
6747e41f4b71Sopenharmony_ci
6748e41f4b71Sopenharmony_ci**Parameters**
6749e41f4b71Sopenharmony_ci
6750e41f4b71Sopenharmony_ci| Name   | Type                         | Mandatory| Description           |
6751e41f4b71Sopenharmony_ci| -------- | -----------------------------| ---- | ---------------|
6752e41f4b71Sopenharmony_ci|   data   | string \| ArrayBuffer                      | Yes  | Data content of the message to send.  |
6753e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void\>         | Yes  | Callback used to return the result. If the operation fails, an error message is returned.|
6754e41f4b71Sopenharmony_ci
6755e41f4b71Sopenharmony_ci**Error codes**
6756e41f4b71Sopenharmony_ci
6757e41f4b71Sopenharmony_ci| ID| Error Message                                     |
6758e41f4b71Sopenharmony_ci| ------- | -------------------------------------------- |
6759e41f4b71Sopenharmony_ci| 401     | Parameter error.                             |
6760e41f4b71Sopenharmony_ci| 2303501 | SSL is null.                                 |
6761e41f4b71Sopenharmony_ci| 2303503 | An error occurred when writing data on the TLS socket.|
6762e41f4b71Sopenharmony_ci| 2303505 | An error occurred in the TLS system call.    |
6763e41f4b71Sopenharmony_ci| 2303506 | Failed to close the TLS connection.          |
6764e41f4b71Sopenharmony_ci| 2300002 | System internal error.                       |
6765e41f4b71Sopenharmony_ci
6766e41f4b71Sopenharmony_ci**Example**
6767e41f4b71Sopenharmony_ci
6768e41f4b71Sopenharmony_ci```ts
6769e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
6770e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
6771e41f4b71Sopenharmony_ci
6772e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
6773e41f4b71Sopenharmony_citls.send("xxxx", (err: BusinessError) => {
6774e41f4b71Sopenharmony_ci  if (err) {
6775e41f4b71Sopenharmony_ci    console.log("send callback error = " + err);
6776e41f4b71Sopenharmony_ci  } else {
6777e41f4b71Sopenharmony_ci    console.log("send success");
6778e41f4b71Sopenharmony_ci  }
6779e41f4b71Sopenharmony_ci});
6780e41f4b71Sopenharmony_ci```
6781e41f4b71Sopenharmony_ci
6782e41f4b71Sopenharmony_ci### send<sup>9+</sup>
6783e41f4b71Sopenharmony_ci
6784e41f4b71Sopenharmony_cisend(data: string \| ArrayBuffer): Promise\<void\>
6785e41f4b71Sopenharmony_ci
6786e41f4b71Sopenharmony_ciSends a message to the server after a TLS socket connection is established. This API uses a promise to return the result.
6787e41f4b71Sopenharmony_ci
6788e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
6789e41f4b71Sopenharmony_ci
6790e41f4b71Sopenharmony_ci**Parameters**
6791e41f4b71Sopenharmony_ci
6792e41f4b71Sopenharmony_ci| Name   | Type                         | Mandatory| Description           |
6793e41f4b71Sopenharmony_ci| -------- | -----------------------------| ---- | ---------------|
6794e41f4b71Sopenharmony_ci|   data   | string \| ArrayBuffer                       | Yes  | Data content of the message to send.  |
6795e41f4b71Sopenharmony_ci
6796e41f4b71Sopenharmony_ci**Error codes**
6797e41f4b71Sopenharmony_ci
6798e41f4b71Sopenharmony_ci| ID| Error Message                                     |
6799e41f4b71Sopenharmony_ci| ------- | -------------------------------------------- |
6800e41f4b71Sopenharmony_ci| 401     | Parameter error.                             |
6801e41f4b71Sopenharmony_ci| 2303501 | SSL is null.                                 |
6802e41f4b71Sopenharmony_ci| 2303503 | An error occurred when writing data on the TLS socket.|
6803e41f4b71Sopenharmony_ci| 2303505 | An error occurred in the TLS system call.    |
6804e41f4b71Sopenharmony_ci| 2303506 | Failed to close the TLS connection.          |
6805e41f4b71Sopenharmony_ci| 2300002 | System internal error.                       |
6806e41f4b71Sopenharmony_ci
6807e41f4b71Sopenharmony_ci**Return value**
6808e41f4b71Sopenharmony_ci
6809e41f4b71Sopenharmony_ci| Type          | Description                 |
6810e41f4b71Sopenharmony_ci| -------------- | -------------------- |
6811e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result. If the operation fails, an error message is returned.|
6812e41f4b71Sopenharmony_ci
6813e41f4b71Sopenharmony_ci**Example**
6814e41f4b71Sopenharmony_ci
6815e41f4b71Sopenharmony_ci```ts
6816e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
6817e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
6818e41f4b71Sopenharmony_ci
6819e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
6820e41f4b71Sopenharmony_citls.send("xxxx").then(() => {
6821e41f4b71Sopenharmony_ci  console.log("send success");
6822e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
6823e41f4b71Sopenharmony_ci  console.error("failed" + err);
6824e41f4b71Sopenharmony_ci});
6825e41f4b71Sopenharmony_ci```
6826e41f4b71Sopenharmony_ci
6827e41f4b71Sopenharmony_ci### close<sup>9+</sup>
6828e41f4b71Sopenharmony_ci
6829e41f4b71Sopenharmony_ciclose(callback: AsyncCallback\<void\>): void
6830e41f4b71Sopenharmony_ci
6831e41f4b71Sopenharmony_ciCloses a TLS socket connection. This API uses an asynchronous callback to return the result.
6832e41f4b71Sopenharmony_ci
6833e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
6834e41f4b71Sopenharmony_ci
6835e41f4b71Sopenharmony_ci**Parameters**
6836e41f4b71Sopenharmony_ci
6837e41f4b71Sopenharmony_ci| Name   | Type                         | Mandatory| Description           |
6838e41f4b71Sopenharmony_ci| -------- | -----------------------------| ---- | ---------------|
6839e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void\>         | Yes  | Callback used to return the result. If the operation fails, an error message is returned.|
6840e41f4b71Sopenharmony_ci
6841e41f4b71Sopenharmony_ci**Error codes**
6842e41f4b71Sopenharmony_ci
6843e41f4b71Sopenharmony_ci| ID| Error Message                                     |
6844e41f4b71Sopenharmony_ci| ------- | -------------------------------------------- |
6845e41f4b71Sopenharmony_ci| 401 | Parameter error.                                 |
6846e41f4b71Sopenharmony_ci| 2303501 | SSL is null.                                 |
6847e41f4b71Sopenharmony_ci| 2303505 | An error occurred in the TLS system call.    |
6848e41f4b71Sopenharmony_ci| 2303506 | Failed to close the TLS connection.          |
6849e41f4b71Sopenharmony_ci| 2300002 | System internal error.                       |
6850e41f4b71Sopenharmony_ci
6851e41f4b71Sopenharmony_ci**Example**
6852e41f4b71Sopenharmony_ci
6853e41f4b71Sopenharmony_ci```ts
6854e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
6855e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
6856e41f4b71Sopenharmony_ci
6857e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
6858e41f4b71Sopenharmony_citls.close((err: BusinessError) => {
6859e41f4b71Sopenharmony_ci  if (err) {
6860e41f4b71Sopenharmony_ci    console.log("close callback error = " + err);
6861e41f4b71Sopenharmony_ci  } else {
6862e41f4b71Sopenharmony_ci    console.log("close success");
6863e41f4b71Sopenharmony_ci  }
6864e41f4b71Sopenharmony_ci});
6865e41f4b71Sopenharmony_ci```
6866e41f4b71Sopenharmony_ci
6867e41f4b71Sopenharmony_ci### close<sup>9+</sup>
6868e41f4b71Sopenharmony_ci
6869e41f4b71Sopenharmony_ciclose(): Promise\<void\>
6870e41f4b71Sopenharmony_ci
6871e41f4b71Sopenharmony_ciCloses a TLS socket connection. This API uses a promise to return the result.
6872e41f4b71Sopenharmony_ci
6873e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
6874e41f4b71Sopenharmony_ci
6875e41f4b71Sopenharmony_ci**Return value**
6876e41f4b71Sopenharmony_ci
6877e41f4b71Sopenharmony_ci| Type          | Description                 |
6878e41f4b71Sopenharmony_ci| -------------- | -------------------- |
6879e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result. If the operation fails, an error message is returned.|
6880e41f4b71Sopenharmony_ci
6881e41f4b71Sopenharmony_ci**Error codes**
6882e41f4b71Sopenharmony_ci
6883e41f4b71Sopenharmony_ci| ID| Error Message                                     |
6884e41f4b71Sopenharmony_ci| ------- | -------------------------------------------- |
6885e41f4b71Sopenharmony_ci| 401 | Parameter error.                                 |
6886e41f4b71Sopenharmony_ci| 2303501 | SSL is null.                                 |
6887e41f4b71Sopenharmony_ci| 2303505 | An error occurred in the TLS system call.    |
6888e41f4b71Sopenharmony_ci| 2303506 | Failed to close the TLS connection.          |
6889e41f4b71Sopenharmony_ci| 2300002 | System internal error.                       |
6890e41f4b71Sopenharmony_ci
6891e41f4b71Sopenharmony_ci**Example**
6892e41f4b71Sopenharmony_ci
6893e41f4b71Sopenharmony_ci```ts
6894e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
6895e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
6896e41f4b71Sopenharmony_ci
6897e41f4b71Sopenharmony_cilet tls: socket.TLSSocket = socket.constructTLSSocketInstance();
6898e41f4b71Sopenharmony_citls.close().then(() => {
6899e41f4b71Sopenharmony_ci  console.log("close success");
6900e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
6901e41f4b71Sopenharmony_ci  console.error("failed" + err);
6902e41f4b71Sopenharmony_ci});
6903e41f4b71Sopenharmony_ci```
6904e41f4b71Sopenharmony_ci
6905e41f4b71Sopenharmony_ci## TLSConnectOptions<sup>9+</sup>
6906e41f4b71Sopenharmony_ci
6907e41f4b71Sopenharmony_ciDefines TLS connection options.
6908e41f4b71Sopenharmony_ci
6909e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
6910e41f4b71Sopenharmony_ci
6911e41f4b71Sopenharmony_ci| Name         | Type                                    | Mandatory| Description           |
6912e41f4b71Sopenharmony_ci| -------------- | ------------------------------------- | ---  |-------------- |
6913e41f4b71Sopenharmony_ci| address        | [NetAddress](#netaddress)             | Yes |  Gateway address.      |
6914e41f4b71Sopenharmony_ci| secureOptions  | [TLSSecureOptions](#tlssecureoptions9) | Yes| TLS security options.|
6915e41f4b71Sopenharmony_ci| ALPNProtocols  | Array\<string\>                         | No| ALPN protocol. The value range is ["spdy/1", "http/1.1"]. The default value is **[]**.     |
6916e41f4b71Sopenharmony_ci| skipRemoteValidation<sup>12+</sup>  | boolean                         | No| Whether to perform certificate authentication on the server. The default value is **false**.     |
6917e41f4b71Sopenharmony_ci
6918e41f4b71Sopenharmony_ci## TLSSecureOptions<sup>9+</sup>
6919e41f4b71Sopenharmony_ci
6920e41f4b71Sopenharmony_ciTLS security options. When **cert** (local certificate) and **key** (private key) are not empty, the two-way authentication mode is enabled. If **cert** or **key** is empty, one-way authentication is enabled.
6921e41f4b71Sopenharmony_ci
6922e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
6923e41f4b71Sopenharmony_ci
6924e41f4b71Sopenharmony_ci| Name                | Type                                                   | Mandatory| Description                               |
6925e41f4b71Sopenharmony_ci| --------------------- | ------------------------------------------------------ | --- |----------------------------------- |
6926e41f4b71Sopenharmony_ci| ca                    | string \| Array\<string\> | No| CA certificate of the server, which is used to authenticate the digital certificate of the server. The default value is the preset CA certificate<sup>12+</sup>.|
6927e41f4b71Sopenharmony_ci| cert                  | string                                                  | No| Digital certificate of the local client.                |
6928e41f4b71Sopenharmony_ci| key                   | string                                                  | No| Private key of the local digital certificate.                  |
6929e41f4b71Sopenharmony_ci| password                | string                                                  | No| Password for reading the private key.                     |
6930e41f4b71Sopenharmony_ci| protocols             | [Protocol](#protocol9) \|Array\<[Protocol](#protocol9)\> | No| TLS protocol version. The default value is **TLSv1.2**.                 |
6931e41f4b71Sopenharmony_ci| useRemoteCipherPrefer | boolean                                                 | No| Whether to use the remote cipher suite preferentially.       |
6932e41f4b71Sopenharmony_ci| signatureAlgorithms   | string                                                 | No| Signing algorithm used during communication. The default value is **""**.             |
6933e41f4b71Sopenharmony_ci| cipherSuite           | string                                                 | No| Cipher suite used during communication. The default value is **""**.             |
6934e41f4b71Sopenharmony_ci
6935e41f4b71Sopenharmony_ci## Protocol<sup>9+</sup>
6936e41f4b71Sopenharmony_ci
6937e41f4b71Sopenharmony_ciEnumerates TLS protocol versions.
6938e41f4b71Sopenharmony_ci
6939e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
6940e41f4b71Sopenharmony_ci
6941e41f4b71Sopenharmony_ci| Name     |    Value   | Description               |
6942e41f4b71Sopenharmony_ci| --------- | --------- |------------------ |
6943e41f4b71Sopenharmony_ci| TLSv12    | "TLSv1.2" | TLSv1.2.|
6944e41f4b71Sopenharmony_ci| TLSv13    | "TLSv1.3" | TLSv1.3.|
6945e41f4b71Sopenharmony_ci
6946e41f4b71Sopenharmony_ci## X509CertRawData<sup>9+</sup>
6947e41f4b71Sopenharmony_ci
6948e41f4b71Sopenharmony_ciDefines the certificate raw data.
6949e41f4b71Sopenharmony_ci
6950e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
6951e41f4b71Sopenharmony_ci
6952e41f4b71Sopenharmony_ci## socket.constructTLSSocketServerInstance<sup>10+</sup>
6953e41f4b71Sopenharmony_ci
6954e41f4b71Sopenharmony_ciconstructTLSSocketServerInstance(): TLSSocketServer
6955e41f4b71Sopenharmony_ci
6956e41f4b71Sopenharmony_ciCreates a **TLSSocketServer** object.
6957e41f4b71Sopenharmony_ci
6958e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
6959e41f4b71Sopenharmony_ci
6960e41f4b71Sopenharmony_ci**Return value**
6961e41f4b71Sopenharmony_ci
6962e41f4b71Sopenharmony_ci| Type                                 | Description                         |
6963e41f4b71Sopenharmony_ci|  ------------------------------------ |  ---------------------------- |
6964e41f4b71Sopenharmony_ci| [TLSSocketServer](#tlssocketserver10) | **TLSSocketServer** object.|
6965e41f4b71Sopenharmony_ci
6966e41f4b71Sopenharmony_ci**Example**
6967e41f4b71Sopenharmony_ci
6968e41f4b71Sopenharmony_ci```ts
6969e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
6970e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
6971e41f4b71Sopenharmony_ci
6972e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
6973e41f4b71Sopenharmony_ci```
6974e41f4b71Sopenharmony_ci
6975e41f4b71Sopenharmony_ci## TLSSocketServer<sup>10+</sup>
6976e41f4b71Sopenharmony_ci
6977e41f4b71Sopenharmony_ciDefines a TLS socket server connection. Before calling TLSSocketServer APIs, you need to call [socket.constructTLSSocketServerInstance](#socketconstructtlssocketserverinstance10) to create a **TLSSocketServer** object.
6978e41f4b71Sopenharmony_ci
6979e41f4b71Sopenharmony_ci### listen<sup>10+</sup>
6980e41f4b71Sopenharmony_ci
6981e41f4b71Sopenharmony_cilisten(options: TLSConnectOptions, callback: AsyncCallback\<void\>): void
6982e41f4b71Sopenharmony_ci
6983e41f4b71Sopenharmony_ciListens to client connections after **bind** is successfully called. This API uses an asynchronous callback to return the result. After a connection is established, a TLS session will be created and initialized and a certificate key will be loaded and verified. 
6984e41f4b71Sopenharmony_ci
6985e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
6986e41f4b71Sopenharmony_ci
6987e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
6988e41f4b71Sopenharmony_ci
6989e41f4b71Sopenharmony_ci**Parameters**
6990e41f4b71Sopenharmony_ci
6991e41f4b71Sopenharmony_ci| Name  | Type                                    | Mandatory| Description                                            |
6992e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ------------------------------------------------ |
6993e41f4b71Sopenharmony_ci| options  | [TLSConnectOptions](#tlsconnectoptions9) | Yes  | Parameters required for the connection.               |
6994e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void\>                     | Yes  | Callback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
6995e41f4b71Sopenharmony_ci
6996e41f4b71Sopenharmony_ci**Error codes**
6997e41f4b71Sopenharmony_ci
6998e41f4b71Sopenharmony_ci| ID| Error Message                                   |
6999e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- |
7000e41f4b71Sopenharmony_ci| 401      | Parameter error.                            |
7001e41f4b71Sopenharmony_ci| 201      | Permission denied.                          |
7002e41f4b71Sopenharmony_ci| 2300002  | System internal error.                      |
7003e41f4b71Sopenharmony_ci| 2303109  | Bad file number.                            |
7004e41f4b71Sopenharmony_ci| 2303111  | Resource temporarily unavailable. Try again.|
7005e41f4b71Sopenharmony_ci| 2303198  | Address already in use.                     |
7006e41f4b71Sopenharmony_ci| 2303199  | Cannot assign requested address.            |
7007e41f4b71Sopenharmony_ci| 2303501  | SSL is null.                                |
7008e41f4b71Sopenharmony_ci| 2303502  | An error occurred when reading data on the TLS socket.|
7009e41f4b71Sopenharmony_ci| 2303503  | An error occurred when writing data on the TLS socket.|
7010e41f4b71Sopenharmony_ci| 2303505  | An error occurred in the TLS system call.   |
7011e41f4b71Sopenharmony_ci| 2303506  | Failed to close the TLS connection.         |
7012e41f4b71Sopenharmony_ci
7013e41f4b71Sopenharmony_ci**Example**
7014e41f4b71Sopenharmony_ci
7015e41f4b71Sopenharmony_ci```ts
7016e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
7017e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
7018e41f4b71Sopenharmony_ci
7019e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
7020e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
7021e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
7022e41f4b71Sopenharmony_ci  port: 8080
7023e41f4b71Sopenharmony_ci}
7024e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
7025e41f4b71Sopenharmony_ci  key: "xxxx",
7026e41f4b71Sopenharmony_ci  cert: "xxxx",
7027e41f4b71Sopenharmony_ci  ca: ["xxxx"],
7028e41f4b71Sopenharmony_ci  password: "xxxx",
7029e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
7030e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
7031e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
7032e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
7033e41f4b71Sopenharmony_ci}
7034e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
7035e41f4b71Sopenharmony_ci  address: netAddress,
7036e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
7037e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"],
7038e41f4b71Sopenharmony_ci  skipRemoteValidation: false
7039e41f4b71Sopenharmony_ci}
7040e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions, (err: BusinessError) => {
7041e41f4b71Sopenharmony_ci  console.log("listen callback error" + err);
7042e41f4b71Sopenharmony_ci});
7043e41f4b71Sopenharmony_ci```
7044e41f4b71Sopenharmony_ci
7045e41f4b71Sopenharmony_ci### listen<sup>10+</sup>
7046e41f4b71Sopenharmony_ci
7047e41f4b71Sopenharmony_cilisten(options: TLSConnectOptions): Promise\<void\>
7048e41f4b71Sopenharmony_ci
7049e41f4b71Sopenharmony_ciListens to client connections after **bind** is successfully called. This API uses a promise to return the result. After a connection is established, a TLS session will be created and initialized and a certificate key will be loaded and verified. 
7050e41f4b71Sopenharmony_ci
7051e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INTERNET
7052e41f4b71Sopenharmony_ci
7053e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
7054e41f4b71Sopenharmony_ci
7055e41f4b71Sopenharmony_ci**Parameters**
7056e41f4b71Sopenharmony_ci
7057e41f4b71Sopenharmony_ci| Name | Type                                    | Mandatory| Description              |
7058e41f4b71Sopenharmony_ci| ------- | ---------------------------------------- | ---- | ------------------ |
7059e41f4b71Sopenharmony_ci| options | [TLSConnectOptions](#tlsconnectoptions9) | Yes  | Parameters required for the connection.|
7060e41f4b71Sopenharmony_ci
7061e41f4b71Sopenharmony_ci**Return value**
7062e41f4b71Sopenharmony_ci
7063e41f4b71Sopenharmony_ci| Type           | Description                                                     |
7064e41f4b71Sopenharmony_ci| --------------- | --------------------------------------------------------- |
7065e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
7066e41f4b71Sopenharmony_ci
7067e41f4b71Sopenharmony_ci**Error codes**
7068e41f4b71Sopenharmony_ci
7069e41f4b71Sopenharmony_ci| ID| Error Message                                   |
7070e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- |
7071e41f4b71Sopenharmony_ci| 401      | Parameter error.                            |
7072e41f4b71Sopenharmony_ci| 201      | Permission denied.                          |
7073e41f4b71Sopenharmony_ci| 2300002  | System internal error.                      |
7074e41f4b71Sopenharmony_ci| 2303109  | Bad file number.                            |
7075e41f4b71Sopenharmony_ci| 2303111  | Resource temporarily unavailable. Try again.|
7076e41f4b71Sopenharmony_ci| 2303198  | Address already in use.                     |
7077e41f4b71Sopenharmony_ci| 2303199  | Cannot assign requested address.            |
7078e41f4b71Sopenharmony_ci| 2303501  | SSL is null.                                |
7079e41f4b71Sopenharmony_ci| 2303502  | An error occurred when reading data on the TLS socket.|
7080e41f4b71Sopenharmony_ci| 2303503  | An error occurred when writing data on the TLS socket.|
7081e41f4b71Sopenharmony_ci| 2303505  | An error occurred in the TLS system call.   |
7082e41f4b71Sopenharmony_ci| 2303506  | Failed to close the TLS connection.         |
7083e41f4b71Sopenharmony_ci
7084e41f4b71Sopenharmony_ci**Example**
7085e41f4b71Sopenharmony_ci
7086e41f4b71Sopenharmony_ci```ts
7087e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
7088e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
7089e41f4b71Sopenharmony_ci
7090e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
7091e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
7092e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
7093e41f4b71Sopenharmony_ci  port: 8080
7094e41f4b71Sopenharmony_ci}
7095e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
7096e41f4b71Sopenharmony_ci  key: "xxxx",
7097e41f4b71Sopenharmony_ci  cert: "xxxx",
7098e41f4b71Sopenharmony_ci  ca: ["xxxx"],
7099e41f4b71Sopenharmony_ci  password: "xxxx",
7100e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
7101e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
7102e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
7103e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
7104e41f4b71Sopenharmony_ci}
7105e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
7106e41f4b71Sopenharmony_ci  address: netAddress,
7107e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
7108e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"],
7109e41f4b71Sopenharmony_ci  skipRemoteValidation: false
7110e41f4b71Sopenharmony_ci}
7111e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
7112e41f4b71Sopenharmony_ci  console.log("listen callback success");
7113e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
7114e41f4b71Sopenharmony_ci  console.log("failed: " + JSON.stringify(err));
7115e41f4b71Sopenharmony_ci});
7116e41f4b71Sopenharmony_ci```
7117e41f4b71Sopenharmony_ci
7118e41f4b71Sopenharmony_ci### getState<sup>10+</sup>
7119e41f4b71Sopenharmony_ci
7120e41f4b71Sopenharmony_cigetState(callback: AsyncCallback\<SocketStateBase\>): void
7121e41f4b71Sopenharmony_ci
7122e41f4b71Sopenharmony_ciObtains the status of the TLS socket server connection upon successful listening. This API uses an asynchronous callback to return the result.
7123e41f4b71Sopenharmony_ci
7124e41f4b71Sopenharmony_ci> **NOTE**
7125e41f4b71Sopenharmony_ci> This API can be called only after **listen** is successfully called.
7126e41f4b71Sopenharmony_ci
7127e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
7128e41f4b71Sopenharmony_ci
7129e41f4b71Sopenharmony_ci**Parameters**
7130e41f4b71Sopenharmony_ci
7131e41f4b71Sopenharmony_ci| Name  | Type                                                | Mandatory| Description                                                        |
7132e41f4b71Sopenharmony_ci| -------- | ---------------------------------------------------- | ---- | ------------------------------------------------------------ |
7133e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[SocketStateBase](#socketstatebase)\> | Yes  | Callback used to return the result. If the operation is successful, the status of the TLS socket server connection is returned. If the operation fails, an error message is returned.|
7134e41f4b71Sopenharmony_ci
7135e41f4b71Sopenharmony_ci**Error codes**
7136e41f4b71Sopenharmony_ci
7137e41f4b71Sopenharmony_ci| ID| Error Message                       |
7138e41f4b71Sopenharmony_ci| -------- | ------------------------------- |
7139e41f4b71Sopenharmony_ci| 401      | Parameter error.                |
7140e41f4b71Sopenharmony_ci| 2303188  | Socket operation on non-socket. |
7141e41f4b71Sopenharmony_ci| 2300002  | System internal error.          |
7142e41f4b71Sopenharmony_ci
7143e41f4b71Sopenharmony_ci**Example**
7144e41f4b71Sopenharmony_ci
7145e41f4b71Sopenharmony_ci```ts
7146e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
7147e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
7148e41f4b71Sopenharmony_ci
7149e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
7150e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
7151e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
7152e41f4b71Sopenharmony_ci  port: 8080
7153e41f4b71Sopenharmony_ci}
7154e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
7155e41f4b71Sopenharmony_ci  key: "xxxx",
7156e41f4b71Sopenharmony_ci  cert: "xxxx",
7157e41f4b71Sopenharmony_ci  ca: ["xxxx"],
7158e41f4b71Sopenharmony_ci  password: "xxxx",
7159e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
7160e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
7161e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
7162e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
7163e41f4b71Sopenharmony_ci}
7164e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
7165e41f4b71Sopenharmony_ci  address: netAddress,
7166e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
7167e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
7168e41f4b71Sopenharmony_ci}
7169e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
7170e41f4b71Sopenharmony_ci  console.log("listen callback success");
7171e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
7172e41f4b71Sopenharmony_ci  console.log("failed: " + JSON.stringify(err));
7173e41f4b71Sopenharmony_ci});
7174e41f4b71Sopenharmony_citlsServer.getState((err: BusinessError, data: socket.SocketStateBase) => {
7175e41f4b71Sopenharmony_ci  if (err) {
7176e41f4b71Sopenharmony_ci    console.log('getState fail');
7177e41f4b71Sopenharmony_ci    return;
7178e41f4b71Sopenharmony_ci  }
7179e41f4b71Sopenharmony_ci  console.log('getState success:' + JSON.stringify(data));
7180e41f4b71Sopenharmony_ci});
7181e41f4b71Sopenharmony_ci```
7182e41f4b71Sopenharmony_ci
7183e41f4b71Sopenharmony_ci### getState<sup>10+</sup>
7184e41f4b71Sopenharmony_ci
7185e41f4b71Sopenharmony_cigetState(): Promise\<SocketStateBase\>
7186e41f4b71Sopenharmony_ci
7187e41f4b71Sopenharmony_ciObtains the status of the TLS socket server connection upon successful listening. This API uses a promise to return the result.
7188e41f4b71Sopenharmony_ci
7189e41f4b71Sopenharmony_ci> **NOTE**
7190e41f4b71Sopenharmony_ci> This API can be called only after **listen** is successfully called.
7191e41f4b71Sopenharmony_ci
7192e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
7193e41f4b71Sopenharmony_ci
7194e41f4b71Sopenharmony_ci**Return value**
7195e41f4b71Sopenharmony_ci
7196e41f4b71Sopenharmony_ci| Type                                          | Description                                                        |
7197e41f4b71Sopenharmony_ci|  --------------------------------------------- |  ----------------------------------------------------------- |
7198e41f4b71Sopenharmony_ci| Promise\<[SocketStateBase](#socketstatebase)\> | Promise used to return the result. If the operation fails, an error message is returned.|
7199e41f4b71Sopenharmony_ci
7200e41f4b71Sopenharmony_ci**Error codes**
7201e41f4b71Sopenharmony_ci
7202e41f4b71Sopenharmony_ci| ID| Error Message                       |
7203e41f4b71Sopenharmony_ci| -------- | ------------------------------- |
7204e41f4b71Sopenharmony_ci| 2303188  | Socket operation on non-socket. |
7205e41f4b71Sopenharmony_ci| 2300002  | System internal error.          |
7206e41f4b71Sopenharmony_ci
7207e41f4b71Sopenharmony_ci**Example**
7208e41f4b71Sopenharmony_ci
7209e41f4b71Sopenharmony_ci```ts
7210e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
7211e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
7212e41f4b71Sopenharmony_ci
7213e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
7214e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
7215e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
7216e41f4b71Sopenharmony_ci  port: 8080
7217e41f4b71Sopenharmony_ci}
7218e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
7219e41f4b71Sopenharmony_ci  key: "xxxx",
7220e41f4b71Sopenharmony_ci  cert: "xxxx",
7221e41f4b71Sopenharmony_ci  ca: ["xxxx"],
7222e41f4b71Sopenharmony_ci  password: "xxxx",
7223e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
7224e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
7225e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
7226e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
7227e41f4b71Sopenharmony_ci}
7228e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
7229e41f4b71Sopenharmony_ci  address: netAddress,
7230e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
7231e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
7232e41f4b71Sopenharmony_ci}
7233e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
7234e41f4b71Sopenharmony_ci  console.log("listen callback success");
7235e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
7236e41f4b71Sopenharmony_ci  console.log("failed: " + JSON.stringify(err));
7237e41f4b71Sopenharmony_ci});
7238e41f4b71Sopenharmony_citlsServer.getState().then(() => {
7239e41f4b71Sopenharmony_ci  console.log('getState success');
7240e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
7241e41f4b71Sopenharmony_ci  console.log('getState fail');
7242e41f4b71Sopenharmony_ci});
7243e41f4b71Sopenharmony_ci```
7244e41f4b71Sopenharmony_ci
7245e41f4b71Sopenharmony_ci### setExtraOptions<sup>10+</sup>
7246e41f4b71Sopenharmony_ci
7247e41f4b71Sopenharmony_cisetExtraOptions(options: TCPExtraOptions, callback: AsyncCallback\<void\>): void
7248e41f4b71Sopenharmony_ci
7249e41f4b71Sopenharmony_ciSets other properties of the TLS socket server connection upon successful listening. This API uses an asynchronous callback to return the result.
7250e41f4b71Sopenharmony_ci
7251e41f4b71Sopenharmony_ci> **NOTE**
7252e41f4b71Sopenharmony_ci> This API can be called only after **listen** is successfully called.
7253e41f4b71Sopenharmony_ci
7254e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
7255e41f4b71Sopenharmony_ci
7256e41f4b71Sopenharmony_ci**Parameters**
7257e41f4b71Sopenharmony_ci
7258e41f4b71Sopenharmony_ci| Name  | Type                                | Mandatory| Description                                            |
7259e41f4b71Sopenharmony_ci| -------- | ------------------------------------ | ---- | ------------------------------------------------ |
7260e41f4b71Sopenharmony_ci| options  | [TCPExtraOptions](#tcpextraoptions) | Yes  | Other properties of the TLS socket server connection.                 |
7261e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void\>                | Yes  | Callback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
7262e41f4b71Sopenharmony_ci
7263e41f4b71Sopenharmony_ci**Error codes**
7264e41f4b71Sopenharmony_ci
7265e41f4b71Sopenharmony_ci| ID| Error Message                       |
7266e41f4b71Sopenharmony_ci| -------- | ------------------------------- |
7267e41f4b71Sopenharmony_ci| 401      | Parameter error.                |
7268e41f4b71Sopenharmony_ci| 2303188  | Socket operation on non-socket. |
7269e41f4b71Sopenharmony_ci| 2300002  | System internal error.          |
7270e41f4b71Sopenharmony_ci
7271e41f4b71Sopenharmony_ci**Example**
7272e41f4b71Sopenharmony_ci
7273e41f4b71Sopenharmony_ci```ts
7274e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
7275e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
7276e41f4b71Sopenharmony_ci
7277e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
7278e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
7279e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
7280e41f4b71Sopenharmony_ci  port: 8080
7281e41f4b71Sopenharmony_ci}
7282e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
7283e41f4b71Sopenharmony_ci  key: "xxxx",
7284e41f4b71Sopenharmony_ci  cert: "xxxx",
7285e41f4b71Sopenharmony_ci  ca: ["xxxx"],
7286e41f4b71Sopenharmony_ci  password: "xxxx",
7287e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
7288e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
7289e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
7290e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
7291e41f4b71Sopenharmony_ci}
7292e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
7293e41f4b71Sopenharmony_ci  address: netAddress,
7294e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
7295e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
7296e41f4b71Sopenharmony_ci}
7297e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
7298e41f4b71Sopenharmony_ci  console.log("listen callback success");
7299e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
7300e41f4b71Sopenharmony_ci  console.log("failed: " + JSON.stringify(err));
7301e41f4b71Sopenharmony_ci});
7302e41f4b71Sopenharmony_ci
7303e41f4b71Sopenharmony_ciinterface SocketLinger {
7304e41f4b71Sopenharmony_ci  on: boolean;
7305e41f4b71Sopenharmony_ci  linger: number;
7306e41f4b71Sopenharmony_ci}
7307e41f4b71Sopenharmony_ci
7308e41f4b71Sopenharmony_cilet tcpExtraOptions: socket.TCPExtraOptions = {
7309e41f4b71Sopenharmony_ci  keepAlive: true,
7310e41f4b71Sopenharmony_ci  OOBInline: true,
7311e41f4b71Sopenharmony_ci  TCPNoDelay: true,
7312e41f4b71Sopenharmony_ci  socketLinger: { on: true, linger: 10 } as SocketLinger,
7313e41f4b71Sopenharmony_ci  receiveBufferSize: 1000,
7314e41f4b71Sopenharmony_ci  sendBufferSize: 1000,
7315e41f4b71Sopenharmony_ci  reuseAddress: true,
7316e41f4b71Sopenharmony_ci  socketTimeout: 3000
7317e41f4b71Sopenharmony_ci}
7318e41f4b71Sopenharmony_citlsServer.setExtraOptions(tcpExtraOptions, (err: BusinessError) => {
7319e41f4b71Sopenharmony_ci  if (err) {
7320e41f4b71Sopenharmony_ci    console.log('setExtraOptions fail');
7321e41f4b71Sopenharmony_ci    return;
7322e41f4b71Sopenharmony_ci  }
7323e41f4b71Sopenharmony_ci  console.log('setExtraOptions success');
7324e41f4b71Sopenharmony_ci});
7325e41f4b71Sopenharmony_ci```
7326e41f4b71Sopenharmony_ci
7327e41f4b71Sopenharmony_ci### setExtraOptions<sup>10+</sup>
7328e41f4b71Sopenharmony_ci
7329e41f4b71Sopenharmony_cisetExtraOptions(options: TCPExtraOptions): Promise\<void\>
7330e41f4b71Sopenharmony_ci
7331e41f4b71Sopenharmony_ciSets other properties of the TLS socket server connection upon successful listening. This API uses a promise to return the result.
7332e41f4b71Sopenharmony_ci
7333e41f4b71Sopenharmony_ci> **NOTE**
7334e41f4b71Sopenharmony_ci> This API can be called only after **listen** is successfully called.
7335e41f4b71Sopenharmony_ci
7336e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
7337e41f4b71Sopenharmony_ci
7338e41f4b71Sopenharmony_ci**Parameters**
7339e41f4b71Sopenharmony_ci
7340e41f4b71Sopenharmony_ci| Name | Type                                | Mandatory| Description                           |
7341e41f4b71Sopenharmony_ci| ------- | ------------------------------------ | ---- | ------------------------------- |
7342e41f4b71Sopenharmony_ci| options | [TCPExtraOptions](#tcpextraoptions) | Yes  | Other properties of the TLS socket server connection.|
7343e41f4b71Sopenharmony_ci
7344e41f4b71Sopenharmony_ci**Return value**
7345e41f4b71Sopenharmony_ci
7346e41f4b71Sopenharmony_ci| Type           | Description                                                     |
7347e41f4b71Sopenharmony_ci|  -------------- |  -------------------------------------------------------- |
7348e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
7349e41f4b71Sopenharmony_ci
7350e41f4b71Sopenharmony_ci**Error codes**
7351e41f4b71Sopenharmony_ci
7352e41f4b71Sopenharmony_ci| ID| Error Message                       |
7353e41f4b71Sopenharmony_ci| -------- | ------------------------------- |
7354e41f4b71Sopenharmony_ci| 401      | Parameter error.                |
7355e41f4b71Sopenharmony_ci| 2303188  | Socket operation on non-socket. |
7356e41f4b71Sopenharmony_ci| 2300002  | System internal error.          |
7357e41f4b71Sopenharmony_ci
7358e41f4b71Sopenharmony_ci**Example**
7359e41f4b71Sopenharmony_ci
7360e41f4b71Sopenharmony_ci```ts
7361e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
7362e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
7363e41f4b71Sopenharmony_ci
7364e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
7365e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
7366e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
7367e41f4b71Sopenharmony_ci  port: 8080
7368e41f4b71Sopenharmony_ci}
7369e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
7370e41f4b71Sopenharmony_ci  key: "xxxx",
7371e41f4b71Sopenharmony_ci  cert: "xxxx",
7372e41f4b71Sopenharmony_ci  ca: ["xxxx"],
7373e41f4b71Sopenharmony_ci  password: "xxxx",
7374e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
7375e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
7376e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
7377e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
7378e41f4b71Sopenharmony_ci}
7379e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
7380e41f4b71Sopenharmony_ci  address: netAddress,
7381e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
7382e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
7383e41f4b71Sopenharmony_ci}
7384e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
7385e41f4b71Sopenharmony_ci  console.log("listen callback success");
7386e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
7387e41f4b71Sopenharmony_ci  console.log("failed: " + JSON.stringify(err));
7388e41f4b71Sopenharmony_ci});
7389e41f4b71Sopenharmony_ci
7390e41f4b71Sopenharmony_ciinterface SocketLinger {
7391e41f4b71Sopenharmony_ci  on: boolean;
7392e41f4b71Sopenharmony_ci  linger: number;
7393e41f4b71Sopenharmony_ci}
7394e41f4b71Sopenharmony_ci
7395e41f4b71Sopenharmony_cilet tcpExtraOptions: socket.TCPExtraOptions = {
7396e41f4b71Sopenharmony_ci  keepAlive: true,
7397e41f4b71Sopenharmony_ci  OOBInline: true,
7398e41f4b71Sopenharmony_ci  TCPNoDelay: true,
7399e41f4b71Sopenharmony_ci  socketLinger: { on: true, linger: 10 } as SocketLinger,
7400e41f4b71Sopenharmony_ci  receiveBufferSize: 1000,
7401e41f4b71Sopenharmony_ci  sendBufferSize: 1000,
7402e41f4b71Sopenharmony_ci  reuseAddress: true,
7403e41f4b71Sopenharmony_ci  socketTimeout: 3000
7404e41f4b71Sopenharmony_ci}
7405e41f4b71Sopenharmony_citlsServer.setExtraOptions(tcpExtraOptions).then(() => {
7406e41f4b71Sopenharmony_ci  console.log('setExtraOptions success');
7407e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
7408e41f4b71Sopenharmony_ci  console.log('setExtraOptions fail');
7409e41f4b71Sopenharmony_ci});
7410e41f4b71Sopenharmony_ci```
7411e41f4b71Sopenharmony_ci
7412e41f4b71Sopenharmony_ci### getCertificate<sup>10+</sup>
7413e41f4b71Sopenharmony_ci
7414e41f4b71Sopenharmony_cigetCertificate(callback: AsyncCallback\<[X509CertRawData](#x509certrawdata9)\>): void
7415e41f4b71Sopenharmony_ci
7416e41f4b71Sopenharmony_ciObtains the local digital certificate after a TLS socket server connection is established. This API uses an asynchronous callback to return the result.
7417e41f4b71Sopenharmony_ci
7418e41f4b71Sopenharmony_ci> **NOTE**
7419e41f4b71Sopenharmony_ci> This API can be called only after **listen** is successfully called.
7420e41f4b71Sopenharmony_ci
7421e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
7422e41f4b71Sopenharmony_ci
7423e41f4b71Sopenharmony_ci**Parameters**
7424e41f4b71Sopenharmony_ci
7425e41f4b71Sopenharmony_ci| Name  | Type                                                 | Mandatory| Description                                                    |
7426e41f4b71Sopenharmony_ci| -------- | ----------------------------------------------------- | ---- | -------------------------------------------------------- |
7427e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[X509CertRawData](#x509certrawdata9)\> | Yes  | Callback used to return the result. If the operation is successful, the local certificate is returned. If the operation fails, an error message is returned.|
7428e41f4b71Sopenharmony_ci
7429e41f4b71Sopenharmony_ci**Error codes**
7430e41f4b71Sopenharmony_ci
7431e41f4b71Sopenharmony_ci| ID| Error Message              |
7432e41f4b71Sopenharmony_ci| -------- | ---------------------- |
7433e41f4b71Sopenharmony_ci| 401      | Parameter error.       |
7434e41f4b71Sopenharmony_ci| 2303501  | SSL is null.           |
7435e41f4b71Sopenharmony_ci| 2303504  | An error occurred when verifying the X.509 certificate. |
7436e41f4b71Sopenharmony_ci| 2300002  | System internal error. |
7437e41f4b71Sopenharmony_ci
7438e41f4b71Sopenharmony_ci**Example**
7439e41f4b71Sopenharmony_ci
7440e41f4b71Sopenharmony_ci```ts
7441e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
7442e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
7443e41f4b71Sopenharmony_ciimport { util } from '@kit.ArkTS';
7444e41f4b71Sopenharmony_ci
7445e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
7446e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
7447e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
7448e41f4b71Sopenharmony_ci  port: 8080
7449e41f4b71Sopenharmony_ci}
7450e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
7451e41f4b71Sopenharmony_ci  key: "xxxx",
7452e41f4b71Sopenharmony_ci  cert: "xxxx",
7453e41f4b71Sopenharmony_ci  ca: ["xxxx"],
7454e41f4b71Sopenharmony_ci  password: "xxxx",
7455e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
7456e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
7457e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
7458e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
7459e41f4b71Sopenharmony_ci}
7460e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
7461e41f4b71Sopenharmony_ci  address: netAddress,
7462e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
7463e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
7464e41f4b71Sopenharmony_ci}
7465e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
7466e41f4b71Sopenharmony_ci  console.log("listen callback success");
7467e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
7468e41f4b71Sopenharmony_ci  console.log("failed: " + JSON.stringify(err));
7469e41f4b71Sopenharmony_ci});
7470e41f4b71Sopenharmony_citlsServer.getCertificate((err: BusinessError, data: socket.X509CertRawData) => {
7471e41f4b71Sopenharmony_ci  if (err) {
7472e41f4b71Sopenharmony_ci    console.log("getCertificate callback error = " + err);
7473e41f4b71Sopenharmony_ci  } else {
7474e41f4b71Sopenharmony_ci    const decoder = util.TextDecoder.create();
7475e41f4b71Sopenharmony_ci    const str = decoder.decodeWithStream(data.data);
7476e41f4b71Sopenharmony_ci    console.log("getCertificate callback: " + str);
7477e41f4b71Sopenharmony_ci  }
7478e41f4b71Sopenharmony_ci});
7479e41f4b71Sopenharmony_ci```
7480e41f4b71Sopenharmony_ci
7481e41f4b71Sopenharmony_ci### getCertificate<sup>10+</sup>
7482e41f4b71Sopenharmony_ci
7483e41f4b71Sopenharmony_cigetCertificate():Promise\<[X509CertRawData](#x509certrawdata9)\>
7484e41f4b71Sopenharmony_ci
7485e41f4b71Sopenharmony_ciObtains the local digital certificate after a TLS socket server connection is established. This API uses a promise to return the result.
7486e41f4b71Sopenharmony_ci
7487e41f4b71Sopenharmony_ci> **NOTE**
7488e41f4b71Sopenharmony_ci> This API can be called only after **listen** is successfully called.
7489e41f4b71Sopenharmony_ci
7490e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
7491e41f4b71Sopenharmony_ci
7492e41f4b71Sopenharmony_ci**Return value**
7493e41f4b71Sopenharmony_ci
7494e41f4b71Sopenharmony_ci| Type                                           | Description                                                        |
7495e41f4b71Sopenharmony_ci| ----------------------------------------------- | ------------------------------------------------------------ |
7496e41f4b71Sopenharmony_ci| Promise\<[X509CertRawData](#x509certrawdata9)\> | Promise used to return the result. If the operation fails, an error message is returned.|
7497e41f4b71Sopenharmony_ci
7498e41f4b71Sopenharmony_ci**Error codes**
7499e41f4b71Sopenharmony_ci
7500e41f4b71Sopenharmony_ci| ID| Error Message              |
7501e41f4b71Sopenharmony_ci| -------- | ---------------------- |
7502e41f4b71Sopenharmony_ci| 2303501  | SSL is null.           |
7503e41f4b71Sopenharmony_ci| 2303504  | An error occurred when verifying the X.509 certificate. |
7504e41f4b71Sopenharmony_ci| 2300002  | System internal error. |
7505e41f4b71Sopenharmony_ci
7506e41f4b71Sopenharmony_ci**Example**
7507e41f4b71Sopenharmony_ci
7508e41f4b71Sopenharmony_ci```ts
7509e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
7510e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
7511e41f4b71Sopenharmony_ciimport { util } from '@kit.ArkTS';
7512e41f4b71Sopenharmony_ci
7513e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
7514e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
7515e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
7516e41f4b71Sopenharmony_ci  port: 8080
7517e41f4b71Sopenharmony_ci}
7518e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
7519e41f4b71Sopenharmony_ci  key: "xxxx",
7520e41f4b71Sopenharmony_ci  cert: "xxxx",
7521e41f4b71Sopenharmony_ci  ca: ["xxxx"],
7522e41f4b71Sopenharmony_ci  password: "xxxx",
7523e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
7524e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
7525e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
7526e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
7527e41f4b71Sopenharmony_ci}
7528e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
7529e41f4b71Sopenharmony_ci  address: netAddress,
7530e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
7531e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
7532e41f4b71Sopenharmony_ci}
7533e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
7534e41f4b71Sopenharmony_ci  console.log("listen callback success");
7535e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
7536e41f4b71Sopenharmony_ci  console.log("failed: " + JSON.stringify(err));
7537e41f4b71Sopenharmony_ci});
7538e41f4b71Sopenharmony_citlsServer.getCertificate().then((data: socket.X509CertRawData) => {
7539e41f4b71Sopenharmony_ci  const decoder = util.TextDecoder.create();
7540e41f4b71Sopenharmony_ci  const str = decoder.decodeWithStream(data.data);
7541e41f4b71Sopenharmony_ci  console.log("getCertificate: " + str);
7542e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
7543e41f4b71Sopenharmony_ci  console.error("failed" + err);
7544e41f4b71Sopenharmony_ci});
7545e41f4b71Sopenharmony_ci```
7546e41f4b71Sopenharmony_ci
7547e41f4b71Sopenharmony_ci### getProtocol<sup>10+</sup>
7548e41f4b71Sopenharmony_ci
7549e41f4b71Sopenharmony_cigetProtocol(callback: AsyncCallback\<string\>): void
7550e41f4b71Sopenharmony_ci
7551e41f4b71Sopenharmony_ciObtains the communication protocol version after a TLS socket server connection is established. This API uses an asynchronous callback to return the result.
7552e41f4b71Sopenharmony_ci
7553e41f4b71Sopenharmony_ci> **NOTE**
7554e41f4b71Sopenharmony_ci> This API can be called only after **listen** is successfully called.
7555e41f4b71Sopenharmony_ci
7556e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
7557e41f4b71Sopenharmony_ci
7558e41f4b71Sopenharmony_ci**Parameters**
7559e41f4b71Sopenharmony_ci
7560e41f4b71Sopenharmony_ci| Name  | Type                   | Mandatory| Description                                                |
7561e41f4b71Sopenharmony_ci| -------- | ----------------------- | ---- | ---------------------------------------------------- |
7562e41f4b71Sopenharmony_ci| callback | AsyncCallback\<string\> | Yes  | Callback used to return the result. If the operation fails, an error message is returned.|
7563e41f4b71Sopenharmony_ci
7564e41f4b71Sopenharmony_ci**Error codes**
7565e41f4b71Sopenharmony_ci
7566e41f4b71Sopenharmony_ci| ID| Error Message                              |
7567e41f4b71Sopenharmony_ci| -------- | -------------------------------------- |
7568e41f4b71Sopenharmony_ci| 401      | Parameter error.                       |
7569e41f4b71Sopenharmony_ci| 2303501  | SSL is null.                           |
7570e41f4b71Sopenharmony_ci| 2303505  | An error occurred in the TLS system call. |
7571e41f4b71Sopenharmony_ci| 2300002  | System internal error.                 |
7572e41f4b71Sopenharmony_ci
7573e41f4b71Sopenharmony_ci**Example**
7574e41f4b71Sopenharmony_ci
7575e41f4b71Sopenharmony_ci```ts
7576e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
7577e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
7578e41f4b71Sopenharmony_ci
7579e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
7580e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
7581e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
7582e41f4b71Sopenharmony_ci  port: 8080
7583e41f4b71Sopenharmony_ci}
7584e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
7585e41f4b71Sopenharmony_ci  key: "xxxx",
7586e41f4b71Sopenharmony_ci  cert: "xxxx",
7587e41f4b71Sopenharmony_ci  ca: ["xxxx"],
7588e41f4b71Sopenharmony_ci  password: "xxxx",
7589e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
7590e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
7591e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
7592e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
7593e41f4b71Sopenharmony_ci}
7594e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
7595e41f4b71Sopenharmony_ci  address: netAddress,
7596e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
7597e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
7598e41f4b71Sopenharmony_ci}
7599e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
7600e41f4b71Sopenharmony_ci  console.log("listen callback success");
7601e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
7602e41f4b71Sopenharmony_ci  console.log("failed: " + JSON.stringify(err));
7603e41f4b71Sopenharmony_ci});
7604e41f4b71Sopenharmony_citlsServer.getProtocol((err: BusinessError, data: string) => {
7605e41f4b71Sopenharmony_ci  if (err) {
7606e41f4b71Sopenharmony_ci    console.log("getProtocol callback error = " + err);
7607e41f4b71Sopenharmony_ci  } else {
7608e41f4b71Sopenharmony_ci    console.log("getProtocol callback = " + data);
7609e41f4b71Sopenharmony_ci  }
7610e41f4b71Sopenharmony_ci});
7611e41f4b71Sopenharmony_ci```
7612e41f4b71Sopenharmony_ci
7613e41f4b71Sopenharmony_ci### getProtocol<sup>10+</sup>
7614e41f4b71Sopenharmony_ci
7615e41f4b71Sopenharmony_cigetProtocol():Promise\<string\>
7616e41f4b71Sopenharmony_ci
7617e41f4b71Sopenharmony_ciObtains the communication protocol version after a TLS socket server connection is established. This API uses a promise to return the result.
7618e41f4b71Sopenharmony_ci
7619e41f4b71Sopenharmony_ci> **NOTE**
7620e41f4b71Sopenharmony_ci> This API can be called only after **listen** is successfully called.
7621e41f4b71Sopenharmony_ci
7622e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
7623e41f4b71Sopenharmony_ci
7624e41f4b71Sopenharmony_ci**Return value**
7625e41f4b71Sopenharmony_ci
7626e41f4b71Sopenharmony_ci| Type             | Description                                                   |
7627e41f4b71Sopenharmony_ci| ----------------- | ------------------------------------------------------- |
7628e41f4b71Sopenharmony_ci| Promise\<string\> | Promise used to return the result. If the operation fails, an error message is returned.|
7629e41f4b71Sopenharmony_ci
7630e41f4b71Sopenharmony_ci**Error codes**
7631e41f4b71Sopenharmony_ci
7632e41f4b71Sopenharmony_ci| ID| Error Message                              |
7633e41f4b71Sopenharmony_ci| -------- | -------------------------------------- |
7634e41f4b71Sopenharmony_ci| 2303501  | SSL is null.                           |
7635e41f4b71Sopenharmony_ci| 2303505  | An error occurred in the TLS system call. |
7636e41f4b71Sopenharmony_ci| 2300002  | System internal error.                 |
7637e41f4b71Sopenharmony_ci
7638e41f4b71Sopenharmony_ci**Example**
7639e41f4b71Sopenharmony_ci
7640e41f4b71Sopenharmony_ci```ts
7641e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
7642e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
7643e41f4b71Sopenharmony_ci
7644e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
7645e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
7646e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
7647e41f4b71Sopenharmony_ci  port: 8080
7648e41f4b71Sopenharmony_ci}
7649e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
7650e41f4b71Sopenharmony_ci  key: "xxxx",
7651e41f4b71Sopenharmony_ci  cert: "xxxx",
7652e41f4b71Sopenharmony_ci  ca: ["xxxx"],
7653e41f4b71Sopenharmony_ci  password: "xxxx",
7654e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
7655e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
7656e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
7657e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
7658e41f4b71Sopenharmony_ci}
7659e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
7660e41f4b71Sopenharmony_ci  address: netAddress,
7661e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
7662e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
7663e41f4b71Sopenharmony_ci}
7664e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
7665e41f4b71Sopenharmony_ci  console.log("listen callback success");
7666e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
7667e41f4b71Sopenharmony_ci  console.log("failed: " + JSON.stringify(err));
7668e41f4b71Sopenharmony_ci});
7669e41f4b71Sopenharmony_citlsServer.getProtocol().then((data: string) => {
7670e41f4b71Sopenharmony_ci  console.log(data);
7671e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
7672e41f4b71Sopenharmony_ci  console.error("failed" + err);
7673e41f4b71Sopenharmony_ci});
7674e41f4b71Sopenharmony_ci```
7675e41f4b71Sopenharmony_ci
7676e41f4b71Sopenharmony_ci### getLocalAddress<sup>12+</sup>
7677e41f4b71Sopenharmony_ci
7678e41f4b71Sopenharmony_cigetLocalAddress(): Promise\<NetAddress\>
7679e41f4b71Sopenharmony_ci
7680e41f4b71Sopenharmony_ciObtains the local socket address of a **TLSSocketServer** connection. This API uses a promise to return the result.
7681e41f4b71Sopenharmony_ci
7682e41f4b71Sopenharmony_ci> **NOTE**
7683e41f4b71Sopenharmony_ci> Call this API only after the **TLSSocketServer** connection is successfully established.
7684e41f4b71Sopenharmony_ci
7685e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
7686e41f4b71Sopenharmony_ci
7687e41f4b71Sopenharmony_ci**Return value**
7688e41f4b71Sopenharmony_ci
7689e41f4b71Sopenharmony_ci| Type           | Description                                                |
7690e41f4b71Sopenharmony_ci|  -------------- |  --------------------------------------------------- |
7691e41f4b71Sopenharmony_ci| Promise\<[NetAddress](#netaddress)\> | Promise used to return the result.|
7692e41f4b71Sopenharmony_ci
7693e41f4b71Sopenharmony_ci**Error codes**
7694e41f4b71Sopenharmony_ci
7695e41f4b71Sopenharmony_ci| ID| Error Message                                   |
7696e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- |
7697e41f4b71Sopenharmony_ci| 2300002  | System internal error.                      |
7698e41f4b71Sopenharmony_ci| 2301009  | Bad file descriptor.                            |
7699e41f4b71Sopenharmony_ci| 2303188  | Socket operation on non-socket. |
7700e41f4b71Sopenharmony_ci
7701e41f4b71Sopenharmony_ci**Example**
7702e41f4b71Sopenharmony_ci
7703e41f4b71Sopenharmony_ci```ts
7704e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
7705e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
7706e41f4b71Sopenharmony_ci
7707e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocket = socket.constructTLSSocketServerInstance();
7708e41f4b71Sopenharmony_citlsServer.getLocalAddress().then((localAddress: socket.NetAddress) => {
7709e41f4b71Sopenharmony_ci  console.info("Get success: " + JSON.stringify(localAddress));
7710e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
7711e41f4b71Sopenharmony_ci  console.error("Get failed, error: " + JSON.stringify(err));
7712e41f4b71Sopenharmony_ci})
7713e41f4b71Sopenharmony_ci```
7714e41f4b71Sopenharmony_ci
7715e41f4b71Sopenharmony_ci### on('connect')<sup>10+</sup>
7716e41f4b71Sopenharmony_ci
7717e41f4b71Sopenharmony_cion(type: 'connect', callback: Callback\<TLSSocketConnection\>): void
7718e41f4b71Sopenharmony_ci
7719e41f4b71Sopenharmony_ciSubscribes to TLS socket server connection events. This API uses an asynchronous callback to return the result.
7720e41f4b71Sopenharmony_ci
7721e41f4b71Sopenharmony_ci> **NOTE**
7722e41f4b71Sopenharmony_ci> This API can be called only after **listen** is successfully called.
7723e41f4b71Sopenharmony_ci
7724e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
7725e41f4b71Sopenharmony_ci
7726e41f4b71Sopenharmony_ci**Parameters**
7727e41f4b71Sopenharmony_ci
7728e41f4b71Sopenharmony_ci| Name  | Type                                                   | Mandatory| Description                                 |
7729e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------- | ---- | ------------------------------------- |
7730e41f4b71Sopenharmony_ci| type     | string                                                  | Yes  | Event type.<br/> **connect**: connection event.|
7731e41f4b71Sopenharmony_ci| callback | Callback\<[TLSSocketConnection](#tlssocketconnection10)\> | Yes  | Callback used to return the result. If the operation fails, an error message is returned.   |
7732e41f4b71Sopenharmony_ci
7733e41f4b71Sopenharmony_ci**Error codes**
7734e41f4b71Sopenharmony_ci
7735e41f4b71Sopenharmony_ci| ID| Error Message        |
7736e41f4b71Sopenharmony_ci| -------- | ---------------- |
7737e41f4b71Sopenharmony_ci| 401      | Parameter error. |
7738e41f4b71Sopenharmony_ci
7739e41f4b71Sopenharmony_ci**Example**
7740e41f4b71Sopenharmony_ci
7741e41f4b71Sopenharmony_ci```ts
7742e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
7743e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
7744e41f4b71Sopenharmony_ci
7745e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
7746e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
7747e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
7748e41f4b71Sopenharmony_ci  port: 8080
7749e41f4b71Sopenharmony_ci}
7750e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
7751e41f4b71Sopenharmony_ci  key: "xxxx",
7752e41f4b71Sopenharmony_ci  cert: "xxxx",
7753e41f4b71Sopenharmony_ci  ca: ["xxxx"],
7754e41f4b71Sopenharmony_ci  password: "xxxx",
7755e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
7756e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
7757e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
7758e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
7759e41f4b71Sopenharmony_ci}
7760e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
7761e41f4b71Sopenharmony_ci  address: netAddress,
7762e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
7763e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
7764e41f4b71Sopenharmony_ci}
7765e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
7766e41f4b71Sopenharmony_ci  console.log("listen callback success");
7767e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
7768e41f4b71Sopenharmony_ci  console.log("failed: " + JSON.stringify(err));
7769e41f4b71Sopenharmony_ci});
7770e41f4b71Sopenharmony_citlsServer.on('connect', (data: socket.TLSSocketConnection) => {
7771e41f4b71Sopenharmony_ci  console.log(JSON.stringify(data))
7772e41f4b71Sopenharmony_ci});
7773e41f4b71Sopenharmony_ci```
7774e41f4b71Sopenharmony_ci
7775e41f4b71Sopenharmony_ci### off('connect')<sup>10+</sup>
7776e41f4b71Sopenharmony_ci
7777e41f4b71Sopenharmony_cioff(type: 'connect', callback?: Callback\<TLSSocketConnection\>): void
7778e41f4b71Sopenharmony_ci
7779e41f4b71Sopenharmony_ciUnsubscribes from **connect** events of a **TLSSocketServer** object. This API uses an asynchronous callback to return the result.
7780e41f4b71Sopenharmony_ci
7781e41f4b71Sopenharmony_ci> **NOTE**
7782e41f4b71Sopenharmony_ci> This API can be called only after **listen** is successfully called.
7783e41f4b71Sopenharmony_ci> You can pass the callback of the **on** function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
7784e41f4b71Sopenharmony_ci
7785e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
7786e41f4b71Sopenharmony_ci
7787e41f4b71Sopenharmony_ci**Parameters**
7788e41f4b71Sopenharmony_ci
7789e41f4b71Sopenharmony_ci| Name  | Type                                                   | Mandatory| Description                                 |
7790e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------- | ---- | ------------------------------------- |
7791e41f4b71Sopenharmony_ci| type     | string                                                  | Yes  | Event type.<br/> **connect**: connection event.|
7792e41f4b71Sopenharmony_ci| callback | Callback\<[TLSSocketConnection](#tlssocketconnection10)\> | No  | Callback used to return the result. If the operation fails, an error message is returned.     |
7793e41f4b71Sopenharmony_ci
7794e41f4b71Sopenharmony_ci**Error codes**
7795e41f4b71Sopenharmony_ci
7796e41f4b71Sopenharmony_ci| ID| Error Message        |
7797e41f4b71Sopenharmony_ci| -------- | ---------------- |
7798e41f4b71Sopenharmony_ci| 401      | Parameter error. |
7799e41f4b71Sopenharmony_ci
7800e41f4b71Sopenharmony_ci**Example**
7801e41f4b71Sopenharmony_ci
7802e41f4b71Sopenharmony_ci```ts
7803e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
7804e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
7805e41f4b71Sopenharmony_ci
7806e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
7807e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
7808e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
7809e41f4b71Sopenharmony_ci  port: 8080
7810e41f4b71Sopenharmony_ci}
7811e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
7812e41f4b71Sopenharmony_ci  key: "xxxx",
7813e41f4b71Sopenharmony_ci  cert: "xxxx",
7814e41f4b71Sopenharmony_ci  ca: ["xxxx"],
7815e41f4b71Sopenharmony_ci  password: "xxxx",
7816e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
7817e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
7818e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
7819e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
7820e41f4b71Sopenharmony_ci}
7821e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
7822e41f4b71Sopenharmony_ci  address: netAddress,
7823e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
7824e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
7825e41f4b71Sopenharmony_ci}
7826e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
7827e41f4b71Sopenharmony_ci  console.log("listen callback success");
7828e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
7829e41f4b71Sopenharmony_ci  console.log("failed: " + JSON.stringify(err));
7830e41f4b71Sopenharmony_ci});
7831e41f4b71Sopenharmony_ci
7832e41f4b71Sopenharmony_cilet callback = (data: socket.TLSSocketConnection) => {
7833e41f4b71Sopenharmony_ci  console.log('on connect message: ' + JSON.stringify(data));
7834e41f4b71Sopenharmony_ci}
7835e41f4b71Sopenharmony_citlsServer.on('connect', callback);
7836e41f4b71Sopenharmony_ci// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
7837e41f4b71Sopenharmony_citlsServer.off('connect', callback);
7838e41f4b71Sopenharmony_citlsServer.off('connect');
7839e41f4b71Sopenharmony_ci```
7840e41f4b71Sopenharmony_ci
7841e41f4b71Sopenharmony_ci### on('error')<sup>10+</sup>
7842e41f4b71Sopenharmony_ci
7843e41f4b71Sopenharmony_cion(type: 'error', callback: ErrorCallback): void
7844e41f4b71Sopenharmony_ci
7845e41f4b71Sopenharmony_ciSubscribes to **error** events of a **TLSSocketServer** object. This API uses an asynchronous callback to return the result.
7846e41f4b71Sopenharmony_ci
7847e41f4b71Sopenharmony_ci> **NOTE**
7848e41f4b71Sopenharmony_ci> This API can be called only after **listen** is successfully called.
7849e41f4b71Sopenharmony_ci
7850e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
7851e41f4b71Sopenharmony_ci
7852e41f4b71Sopenharmony_ci**Parameters**
7853e41f4b71Sopenharmony_ci
7854e41f4b71Sopenharmony_ci| Name  | Type         | Mandatory| Description                                |
7855e41f4b71Sopenharmony_ci| -------- | ------------- | ---- | ------------------------------------ |
7856e41f4b71Sopenharmony_ci| type     | string        | Yes  | Event type.<br/> **error**: error event.|
7857e41f4b71Sopenharmony_ci| callback | ErrorCallback | Yes  | Callback used to return the result. If the operation fails, an error message is returned.    |
7858e41f4b71Sopenharmony_ci
7859e41f4b71Sopenharmony_ci**Error codes**
7860e41f4b71Sopenharmony_ci
7861e41f4b71Sopenharmony_ci| ID| Error Message        |
7862e41f4b71Sopenharmony_ci| -------- | ---------------- |
7863e41f4b71Sopenharmony_ci| 401      | Parameter error. |
7864e41f4b71Sopenharmony_ci
7865e41f4b71Sopenharmony_ci**Example**
7866e41f4b71Sopenharmony_ci
7867e41f4b71Sopenharmony_ci```ts
7868e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
7869e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
7870e41f4b71Sopenharmony_ci
7871e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
7872e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
7873e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
7874e41f4b71Sopenharmony_ci  port: 8080
7875e41f4b71Sopenharmony_ci}
7876e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
7877e41f4b71Sopenharmony_ci  key: "xxxx",
7878e41f4b71Sopenharmony_ci  cert: "xxxx",
7879e41f4b71Sopenharmony_ci  ca: ["xxxx"],
7880e41f4b71Sopenharmony_ci  password: "xxxx",
7881e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
7882e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
7883e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
7884e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
7885e41f4b71Sopenharmony_ci}
7886e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
7887e41f4b71Sopenharmony_ci  address: netAddress,
7888e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
7889e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
7890e41f4b71Sopenharmony_ci}
7891e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
7892e41f4b71Sopenharmony_ci  console.log("listen callback success");
7893e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
7894e41f4b71Sopenharmony_ci  console.log("failed: " + JSON.stringify(err));
7895e41f4b71Sopenharmony_ci});
7896e41f4b71Sopenharmony_citlsServer.on('error', (err: BusinessError) => {
7897e41f4b71Sopenharmony_ci  console.log("on error, err:" + JSON.stringify(err))
7898e41f4b71Sopenharmony_ci});
7899e41f4b71Sopenharmony_ci```
7900e41f4b71Sopenharmony_ci
7901e41f4b71Sopenharmony_ci### off('error')<sup>10+</sup>
7902e41f4b71Sopenharmony_ci
7903e41f4b71Sopenharmony_cioff(type: 'error', callback?: ErrorCallback): void
7904e41f4b71Sopenharmony_ci
7905e41f4b71Sopenharmony_ciUnsubscribes from **error** events of a **TLSSocketServer** object. This API uses an asynchronous callback to return the result.
7906e41f4b71Sopenharmony_ci
7907e41f4b71Sopenharmony_ci> **NOTE**
7908e41f4b71Sopenharmony_ci> This API can be called only after **listen** is successfully called.
7909e41f4b71Sopenharmony_ci> You can pass the callback of the **on** function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
7910e41f4b71Sopenharmony_ci
7911e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
7912e41f4b71Sopenharmony_ci
7913e41f4b71Sopenharmony_ci**Parameters**
7914e41f4b71Sopenharmony_ci
7915e41f4b71Sopenharmony_ci| Name  | Type         | Mandatory| Description                                |
7916e41f4b71Sopenharmony_ci| -------- | ------------- | ---- | ------------------------------------ |
7917e41f4b71Sopenharmony_ci| type     | string        | Yes  | Event type.<br/> **error**: error event.|
7918e41f4b71Sopenharmony_ci| callback | ErrorCallback | No  | Callback used to return the result. If the operation fails, an error message is returned.    |
7919e41f4b71Sopenharmony_ci
7920e41f4b71Sopenharmony_ci**Error codes**
7921e41f4b71Sopenharmony_ci
7922e41f4b71Sopenharmony_ci| ID| Error Message        |
7923e41f4b71Sopenharmony_ci| -------- | ---------------- |
7924e41f4b71Sopenharmony_ci| 401      | Parameter error. |
7925e41f4b71Sopenharmony_ci
7926e41f4b71Sopenharmony_ci**Example**
7927e41f4b71Sopenharmony_ci
7928e41f4b71Sopenharmony_ci```ts
7929e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
7930e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
7931e41f4b71Sopenharmony_ci
7932e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
7933e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
7934e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
7935e41f4b71Sopenharmony_ci  port: 8080
7936e41f4b71Sopenharmony_ci}
7937e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
7938e41f4b71Sopenharmony_ci  key: "xxxx",
7939e41f4b71Sopenharmony_ci  cert: "xxxx",
7940e41f4b71Sopenharmony_ci  ca: ["xxxx"],
7941e41f4b71Sopenharmony_ci  password: "xxxx",
7942e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
7943e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
7944e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
7945e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
7946e41f4b71Sopenharmony_ci}
7947e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
7948e41f4b71Sopenharmony_ci  address: netAddress,
7949e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
7950e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
7951e41f4b71Sopenharmony_ci}
7952e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
7953e41f4b71Sopenharmony_ci  console.log("listen callback success");
7954e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
7955e41f4b71Sopenharmony_ci  console.log("failed: " + JSON.stringify(err));
7956e41f4b71Sopenharmony_ci});
7957e41f4b71Sopenharmony_ci
7958e41f4b71Sopenharmony_cilet callback = (err: BusinessError) => {
7959e41f4b71Sopenharmony_ci  console.log("on error, err:" + JSON.stringify(err));
7960e41f4b71Sopenharmony_ci}
7961e41f4b71Sopenharmony_citlsServer.on('error', callback);
7962e41f4b71Sopenharmony_ci// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
7963e41f4b71Sopenharmony_citlsServer.off('error', callback);
7964e41f4b71Sopenharmony_citlsServer.off('error');
7965e41f4b71Sopenharmony_ci```
7966e41f4b71Sopenharmony_ci
7967e41f4b71Sopenharmony_ci## TLSSocketConnection<sup>10+</sup>
7968e41f4b71Sopenharmony_ci
7969e41f4b71Sopenharmony_ciDefines a **TLSSocketConnection** object, that is, the connection between the TLSSocket client and the server. Before calling TLSSocketConnection APIs, you need to obtain a **TLSSocketConnection** object.
7970e41f4b71Sopenharmony_ci
7971e41f4b71Sopenharmony_ci> **NOTE**
7972e41f4b71Sopenharmony_ci> The TLSSocket client can call related APIs through the **TLSSocketConnection** object only after a connection is successfully established between the TLSSocket client and the server.
7973e41f4b71Sopenharmony_ci
7974e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
7975e41f4b71Sopenharmony_ci
7976e41f4b71Sopenharmony_ci### Attributes
7977e41f4b71Sopenharmony_ci
7978e41f4b71Sopenharmony_ci| Name    | Type  | Mandatory| Description                                 |
7979e41f4b71Sopenharmony_ci| -------- | ------ | ---- | ------------------------------------- |
7980e41f4b71Sopenharmony_ci| clientId | number | Yes  | ID of the connection between the client and TLSSocketServer.|
7981e41f4b71Sopenharmony_ci
7982e41f4b71Sopenharmony_ci### send<sup>10+</sup>
7983e41f4b71Sopenharmony_ci
7984e41f4b71Sopenharmony_cisend(data: string \| ArrayBuffer, callback: AsyncCallback\<void\>): void
7985e41f4b71Sopenharmony_ci
7986e41f4b71Sopenharmony_ciSends a message to the client after a TLS socket server connection is established. This API uses an asynchronous callback to return the result.
7987e41f4b71Sopenharmony_ci
7988e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
7989e41f4b71Sopenharmony_ci
7990e41f4b71Sopenharmony_ci**Parameters**
7991e41f4b71Sopenharmony_ci
7992e41f4b71Sopenharmony_ci| Name  | Type                 | Mandatory| Description                                            |
7993e41f4b71Sopenharmony_ci| -------- | --------------------- | ---- | ------------------------------------------------ |
7994e41f4b71Sopenharmony_ci| data     | string \| ArrayBuffer                | Yes  | Parameters for sending data over a TLS socket server connection.           |
7995e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void\> | Yes  | Callback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
7996e41f4b71Sopenharmony_ci
7997e41f4b71Sopenharmony_ci**Error codes**
7998e41f4b71Sopenharmony_ci
7999e41f4b71Sopenharmony_ci| ID| Error Message                              |
8000e41f4b71Sopenharmony_ci| -------- | -------------------------------------- |
8001e41f4b71Sopenharmony_ci| 401      | Parameter error.                       |
8002e41f4b71Sopenharmony_ci| 2303501  | SSL is null.                           |
8003e41f4b71Sopenharmony_ci| 2303503  | An error occurred when writing data on the TLS socket.|
8004e41f4b71Sopenharmony_ci| 2303505  | An error occurred in the TLS system call.|
8005e41f4b71Sopenharmony_ci| 2303506  | Failed to close the TLS connection.    |
8006e41f4b71Sopenharmony_ci| 2300002  | System internal error.                 |
8007e41f4b71Sopenharmony_ci
8008e41f4b71Sopenharmony_ci**Example**
8009e41f4b71Sopenharmony_ci
8010e41f4b71Sopenharmony_ci```ts
8011e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
8012e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
8013e41f4b71Sopenharmony_ci
8014e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
8015e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
8016e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
8017e41f4b71Sopenharmony_ci  port: 8080
8018e41f4b71Sopenharmony_ci}
8019e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
8020e41f4b71Sopenharmony_ci  key: "xxxx",
8021e41f4b71Sopenharmony_ci  cert: "xxxx",
8022e41f4b71Sopenharmony_ci  ca: ["xxxx"],
8023e41f4b71Sopenharmony_ci  password: "xxxx",
8024e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
8025e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
8026e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
8027e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
8028e41f4b71Sopenharmony_ci}
8029e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
8030e41f4b71Sopenharmony_ci  address: netAddress,
8031e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
8032e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
8033e41f4b71Sopenharmony_ci}
8034e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
8035e41f4b71Sopenharmony_ci  console.log("listen callback success");
8036e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
8037e41f4b71Sopenharmony_ci  console.log("failed" + err);
8038e41f4b71Sopenharmony_ci});
8039e41f4b71Sopenharmony_ci
8040e41f4b71Sopenharmony_citlsServer.on('connect', (client: socket.TLSSocketConnection) => {
8041e41f4b71Sopenharmony_ci  client.send('Hello, client!', (err: BusinessError) => {
8042e41f4b71Sopenharmony_ci    if (err) {
8043e41f4b71Sopenharmony_ci      console.log('send fail');
8044e41f4b71Sopenharmony_ci      return;
8045e41f4b71Sopenharmony_ci    }
8046e41f4b71Sopenharmony_ci    console.log('send success');
8047e41f4b71Sopenharmony_ci  });
8048e41f4b71Sopenharmony_ci});
8049e41f4b71Sopenharmony_ci```
8050e41f4b71Sopenharmony_ci
8051e41f4b71Sopenharmony_ci### send<sup>10+</sup>
8052e41f4b71Sopenharmony_ci
8053e41f4b71Sopenharmony_cisend(data: string \| ArrayBuffer): Promise\<void\>
8054e41f4b71Sopenharmony_ci
8055e41f4b71Sopenharmony_ciSends a message to the server after a TLS socket server connection is established. This API uses a promise to return the result.
8056e41f4b71Sopenharmony_ci
8057e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
8058e41f4b71Sopenharmony_ci
8059e41f4b71Sopenharmony_ci**Parameters**
8060e41f4b71Sopenharmony_ci
8061e41f4b71Sopenharmony_ci| Name| Type  | Mandatory| Description                                 |
8062e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------- |
8063e41f4b71Sopenharmony_ci| data   | string \| ArrayBuffer | Yes  | Parameters for sending data over a TLS socket server connection.|
8064e41f4b71Sopenharmony_ci
8065e41f4b71Sopenharmony_ci**Return value**
8066e41f4b71Sopenharmony_ci
8067e41f4b71Sopenharmony_ci| Type           | Description                                                     |
8068e41f4b71Sopenharmony_ci| --------------- | --------------------------------------------------------- |
8069e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
8070e41f4b71Sopenharmony_ci
8071e41f4b71Sopenharmony_ci**Error codes**
8072e41f4b71Sopenharmony_ci
8073e41f4b71Sopenharmony_ci| ID| Error Message                              |
8074e41f4b71Sopenharmony_ci| -------- | -------------------------------------- |
8075e41f4b71Sopenharmony_ci| 401      | Parameter error.                       |
8076e41f4b71Sopenharmony_ci| 2303501  | SSL is null.                           |
8077e41f4b71Sopenharmony_ci| 2303503  | An error occurred when writing data on the TLS socket.|
8078e41f4b71Sopenharmony_ci| 2303505  | An error occurred in the TLS system call.|
8079e41f4b71Sopenharmony_ci| 2303506  | Failed to close the TLS connection.    |
8080e41f4b71Sopenharmony_ci| 2300002  | System internal error.                 |
8081e41f4b71Sopenharmony_ci
8082e41f4b71Sopenharmony_ci**Example**
8083e41f4b71Sopenharmony_ci
8084e41f4b71Sopenharmony_ci```ts
8085e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
8086e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
8087e41f4b71Sopenharmony_ci
8088e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
8089e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
8090e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
8091e41f4b71Sopenharmony_ci  port: 8080
8092e41f4b71Sopenharmony_ci}
8093e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
8094e41f4b71Sopenharmony_ci  key: "xxxx",
8095e41f4b71Sopenharmony_ci  cert: "xxxx",
8096e41f4b71Sopenharmony_ci  ca: ["xxxx"],
8097e41f4b71Sopenharmony_ci  password: "xxxx",
8098e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
8099e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
8100e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
8101e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
8102e41f4b71Sopenharmony_ci}
8103e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
8104e41f4b71Sopenharmony_ci  address: netAddress,
8105e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
8106e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
8107e41f4b71Sopenharmony_ci}
8108e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
8109e41f4b71Sopenharmony_ci  console.log("listen callback success");
8110e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
8111e41f4b71Sopenharmony_ci  console.log("failed" + err);
8112e41f4b71Sopenharmony_ci});
8113e41f4b71Sopenharmony_ci
8114e41f4b71Sopenharmony_citlsServer.on('connect', (client: socket.TLSSocketConnection) => {
8115e41f4b71Sopenharmony_ci  client.send('Hello, client!').then(() => {
8116e41f4b71Sopenharmony_ci    console.log('send success');
8117e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
8118e41f4b71Sopenharmony_ci    console.log('send fail');
8119e41f4b71Sopenharmony_ci  });
8120e41f4b71Sopenharmony_ci});
8121e41f4b71Sopenharmony_ci```
8122e41f4b71Sopenharmony_ci
8123e41f4b71Sopenharmony_ci### close<sup>10+</sup>
8124e41f4b71Sopenharmony_ci
8125e41f4b71Sopenharmony_ciclose(callback: AsyncCallback\<void\>): void
8126e41f4b71Sopenharmony_ci
8127e41f4b71Sopenharmony_ciCloses a TLS socket server connection. This API uses an asynchronous callback to return the result.
8128e41f4b71Sopenharmony_ci
8129e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
8130e41f4b71Sopenharmony_ci
8131e41f4b71Sopenharmony_ci**Parameters**
8132e41f4b71Sopenharmony_ci
8133e41f4b71Sopenharmony_ci| Name  | Type                 | Mandatory| Description                                            |
8134e41f4b71Sopenharmony_ci| -------- | --------------------- | ---- | ------------------------------------------------ |
8135e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void\> | Yes  | Callback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
8136e41f4b71Sopenharmony_ci
8137e41f4b71Sopenharmony_ci**Error codes**
8138e41f4b71Sopenharmony_ci
8139e41f4b71Sopenharmony_ci| ID| Error Message                              |
8140e41f4b71Sopenharmony_ci| -------- | -------------------------------------- |
8141e41f4b71Sopenharmony_ci| 401      | Parameter error.                       |
8142e41f4b71Sopenharmony_ci| 2303501  | SSL is null.                           |
8143e41f4b71Sopenharmony_ci| 2303505  | An error occurred in the TLS system call. |
8144e41f4b71Sopenharmony_ci| 2303506  | Failed to close the TLS connection.    |
8145e41f4b71Sopenharmony_ci| 2300002  | System internal error.                 |
8146e41f4b71Sopenharmony_ci
8147e41f4b71Sopenharmony_ci**Example**
8148e41f4b71Sopenharmony_ci
8149e41f4b71Sopenharmony_ci```ts
8150e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
8151e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
8152e41f4b71Sopenharmony_ci
8153e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
8154e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
8155e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
8156e41f4b71Sopenharmony_ci  port: 8080
8157e41f4b71Sopenharmony_ci}
8158e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
8159e41f4b71Sopenharmony_ci  key: "xxxx",
8160e41f4b71Sopenharmony_ci  cert: "xxxx",
8161e41f4b71Sopenharmony_ci  ca: ["xxxx"],
8162e41f4b71Sopenharmony_ci  password: "xxxx",
8163e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
8164e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
8165e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
8166e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
8167e41f4b71Sopenharmony_ci}
8168e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
8169e41f4b71Sopenharmony_ci  address: netAddress,
8170e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
8171e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
8172e41f4b71Sopenharmony_ci}
8173e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
8174e41f4b71Sopenharmony_ci  console.log("listen callback success");
8175e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
8176e41f4b71Sopenharmony_ci  console.log("failed" + err);
8177e41f4b71Sopenharmony_ci});
8178e41f4b71Sopenharmony_ci
8179e41f4b71Sopenharmony_citlsServer.on('connect', (client: socket.TLSSocketConnection) => {
8180e41f4b71Sopenharmony_ci  client.close((err: BusinessError) => {
8181e41f4b71Sopenharmony_ci    if (err) {
8182e41f4b71Sopenharmony_ci      console.log('close fail');
8183e41f4b71Sopenharmony_ci      return;
8184e41f4b71Sopenharmony_ci    }
8185e41f4b71Sopenharmony_ci    console.log('close success');
8186e41f4b71Sopenharmony_ci  });
8187e41f4b71Sopenharmony_ci});
8188e41f4b71Sopenharmony_ci```
8189e41f4b71Sopenharmony_ci
8190e41f4b71Sopenharmony_ci### close<sup>10+</sup>
8191e41f4b71Sopenharmony_ci
8192e41f4b71Sopenharmony_ciclose(): Promise\<void\>
8193e41f4b71Sopenharmony_ci
8194e41f4b71Sopenharmony_ciCloses a TLS socket server connection. This API uses a promise to return the result.
8195e41f4b71Sopenharmony_ci
8196e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
8197e41f4b71Sopenharmony_ci
8198e41f4b71Sopenharmony_ci**Return value**
8199e41f4b71Sopenharmony_ci
8200e41f4b71Sopenharmony_ci| Type           | Description                                                     |
8201e41f4b71Sopenharmony_ci| --------------- | --------------------------------------------------------- |
8202e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned. If the operation fails, an error message is returned.|
8203e41f4b71Sopenharmony_ci
8204e41f4b71Sopenharmony_ci**Error codes**
8205e41f4b71Sopenharmony_ci
8206e41f4b71Sopenharmony_ci| ID| Error Message                              |
8207e41f4b71Sopenharmony_ci| -------- | -------------------------------------- |
8208e41f4b71Sopenharmony_ci| 2303501  | SSL is null.                           |
8209e41f4b71Sopenharmony_ci| 2303505  | An error occurred in the TLS system call. |
8210e41f4b71Sopenharmony_ci| 2303506  | Failed to close the TLS connection.    |
8211e41f4b71Sopenharmony_ci| 2300002  | System internal error.                 |
8212e41f4b71Sopenharmony_ci
8213e41f4b71Sopenharmony_ci**Example**
8214e41f4b71Sopenharmony_ci
8215e41f4b71Sopenharmony_ci```ts
8216e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
8217e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
8218e41f4b71Sopenharmony_ci
8219e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
8220e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
8221e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
8222e41f4b71Sopenharmony_ci  port: 8080
8223e41f4b71Sopenharmony_ci}
8224e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
8225e41f4b71Sopenharmony_ci  key: "xxxx",
8226e41f4b71Sopenharmony_ci  cert: "xxxx",
8227e41f4b71Sopenharmony_ci  ca: ["xxxx"],
8228e41f4b71Sopenharmony_ci  password: "xxxx",
8229e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
8230e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
8231e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
8232e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
8233e41f4b71Sopenharmony_ci}
8234e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
8235e41f4b71Sopenharmony_ci  address: netAddress,
8236e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
8237e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
8238e41f4b71Sopenharmony_ci}
8239e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
8240e41f4b71Sopenharmony_ci  console.log("listen callback success");
8241e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
8242e41f4b71Sopenharmony_ci  console.log("failed" + err);
8243e41f4b71Sopenharmony_ci});
8244e41f4b71Sopenharmony_citlsServer.on('connect', (client: socket.TLSSocketConnection) => {
8245e41f4b71Sopenharmony_ci  client.close().then(() => {
8246e41f4b71Sopenharmony_ci    console.log('close success');
8247e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
8248e41f4b71Sopenharmony_ci    console.log('close fail');
8249e41f4b71Sopenharmony_ci  });
8250e41f4b71Sopenharmony_ci});
8251e41f4b71Sopenharmony_ci```
8252e41f4b71Sopenharmony_ci
8253e41f4b71Sopenharmony_ci### getRemoteAddress<sup>10+</sup>
8254e41f4b71Sopenharmony_ci
8255e41f4b71Sopenharmony_cigetRemoteAddress(callback: AsyncCallback\<NetAddress\>): void
8256e41f4b71Sopenharmony_ci
8257e41f4b71Sopenharmony_ciObtains the remote address of a TLS socket server connection. This API uses an asynchronous callback to return the result.
8258e41f4b71Sopenharmony_ci
8259e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
8260e41f4b71Sopenharmony_ci
8261e41f4b71Sopenharmony_ci**Parameters**
8262e41f4b71Sopenharmony_ci
8263e41f4b71Sopenharmony_ci| Name  | Type                                       | Mandatory| Description                                                        |
8264e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- | ---- | ------------------------------------------------------------ |
8265e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[NetAddress](#netaddress)\> | Yes  | Callback used to return the result. If the operation is successful, the remote address is returned. If the operation fails, an error message is returned.|
8266e41f4b71Sopenharmony_ci
8267e41f4b71Sopenharmony_ci**Error codes**
8268e41f4b71Sopenharmony_ci
8269e41f4b71Sopenharmony_ci| ID| Error Message                       |
8270e41f4b71Sopenharmony_ci| -------- | ------------------------------- |
8271e41f4b71Sopenharmony_ci| 401      | Parameter error.                |
8272e41f4b71Sopenharmony_ci| 2303188  | Socket operation on non-socket. |
8273e41f4b71Sopenharmony_ci| 2300002  | System internal error.          |
8274e41f4b71Sopenharmony_ci
8275e41f4b71Sopenharmony_ci**Example**
8276e41f4b71Sopenharmony_ci
8277e41f4b71Sopenharmony_ci```ts
8278e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
8279e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
8280e41f4b71Sopenharmony_ci
8281e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
8282e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
8283e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
8284e41f4b71Sopenharmony_ci  port: 8080
8285e41f4b71Sopenharmony_ci}
8286e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
8287e41f4b71Sopenharmony_ci  key: "xxxx",
8288e41f4b71Sopenharmony_ci  cert: "xxxx",
8289e41f4b71Sopenharmony_ci  ca: ["xxxx"],
8290e41f4b71Sopenharmony_ci  password: "xxxx",
8291e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
8292e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
8293e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
8294e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
8295e41f4b71Sopenharmony_ci}
8296e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
8297e41f4b71Sopenharmony_ci  address: netAddress,
8298e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
8299e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
8300e41f4b71Sopenharmony_ci}
8301e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
8302e41f4b71Sopenharmony_ci  console.log("listen callback success");
8303e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
8304e41f4b71Sopenharmony_ci  console.log("failed" + err);
8305e41f4b71Sopenharmony_ci});
8306e41f4b71Sopenharmony_citlsServer.on('connect', (client: socket.TLSSocketConnection) => {
8307e41f4b71Sopenharmony_ci  client.getRemoteAddress((err: BusinessError, data: socket.NetAddress) => {
8308e41f4b71Sopenharmony_ci    if (err) {
8309e41f4b71Sopenharmony_ci      console.log('getRemoteAddress fail');
8310e41f4b71Sopenharmony_ci      return;
8311e41f4b71Sopenharmony_ci    }
8312e41f4b71Sopenharmony_ci    console.log('getRemoteAddress success:' + JSON.stringify(data));
8313e41f4b71Sopenharmony_ci  });
8314e41f4b71Sopenharmony_ci});
8315e41f4b71Sopenharmony_ci```
8316e41f4b71Sopenharmony_ci
8317e41f4b71Sopenharmony_ci### getRemoteAddress<sup>10+</sup>
8318e41f4b71Sopenharmony_ci
8319e41f4b71Sopenharmony_cigetRemoteAddress(): Promise\<NetAddress\>
8320e41f4b71Sopenharmony_ci
8321e41f4b71Sopenharmony_ciObtains the remote address of a TLS socket server connection. This API uses a promise to return the result.
8322e41f4b71Sopenharmony_ci
8323e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
8324e41f4b71Sopenharmony_ci
8325e41f4b71Sopenharmony_ci**Return value**
8326e41f4b71Sopenharmony_ci
8327e41f4b71Sopenharmony_ci| Type                                | Description                                                        |
8328e41f4b71Sopenharmony_ci|  ----------------------------------- |  ----------------------------------------------------------- |
8329e41f4b71Sopenharmony_ci| Promise\<[NetAddress](#netaddress)\> | Promise used to return the result. If the operation fails, an error message is returned.|
8330e41f4b71Sopenharmony_ci
8331e41f4b71Sopenharmony_ci**Error codes**
8332e41f4b71Sopenharmony_ci
8333e41f4b71Sopenharmony_ci| ID| Error Message                       |
8334e41f4b71Sopenharmony_ci| -------- | ------------------------------- |
8335e41f4b71Sopenharmony_ci| 2303188  | Socket operation on non-socket. |
8336e41f4b71Sopenharmony_ci| 2300002  | System internal error.          |
8337e41f4b71Sopenharmony_ci
8338e41f4b71Sopenharmony_ci**Example**
8339e41f4b71Sopenharmony_ci
8340e41f4b71Sopenharmony_ci```ts
8341e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
8342e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
8343e41f4b71Sopenharmony_ci
8344e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
8345e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
8346e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
8347e41f4b71Sopenharmony_ci  port: 8080
8348e41f4b71Sopenharmony_ci}
8349e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
8350e41f4b71Sopenharmony_ci  key: "xxxx",
8351e41f4b71Sopenharmony_ci  cert: "xxxx",
8352e41f4b71Sopenharmony_ci  ca: ["xxxx"],
8353e41f4b71Sopenharmony_ci  password: "xxxx",
8354e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
8355e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
8356e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
8357e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
8358e41f4b71Sopenharmony_ci}
8359e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
8360e41f4b71Sopenharmony_ci  address: netAddress,
8361e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
8362e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
8363e41f4b71Sopenharmony_ci}
8364e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
8365e41f4b71Sopenharmony_ci  console.log("listen callback success");
8366e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
8367e41f4b71Sopenharmony_ci  console.log("failed" + err);
8368e41f4b71Sopenharmony_ci});
8369e41f4b71Sopenharmony_citlsServer.on('connect', (client: socket.TLSSocketConnection) => {
8370e41f4b71Sopenharmony_ci  client.getRemoteAddress().then((data: socket.NetAddress) => {
8371e41f4b71Sopenharmony_ci    console.log('getRemoteAddress success:' + JSON.stringify(data));
8372e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
8373e41f4b71Sopenharmony_ci    console.error("failed" + err);
8374e41f4b71Sopenharmony_ci  });
8375e41f4b71Sopenharmony_ci});
8376e41f4b71Sopenharmony_ci```
8377e41f4b71Sopenharmony_ci
8378e41f4b71Sopenharmony_ci### getRemoteCertificate<sup>10+</sup>
8379e41f4b71Sopenharmony_ci
8380e41f4b71Sopenharmony_cigetRemoteCertificate(callback: AsyncCallback\<[X509CertRawData](#x509certrawdata9)\>): void
8381e41f4b71Sopenharmony_ci
8382e41f4b71Sopenharmony_ciObtains the digital certificate of the peer end after a TLS socket server connection is established. This API uses an asynchronous callback to return the result. It applies only to the scenario where the client sends a certificate to the server. 
8383e41f4b71Sopenharmony_ci
8384e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
8385e41f4b71Sopenharmony_ci
8386e41f4b71Sopenharmony_ci**Parameters**
8387e41f4b71Sopenharmony_ci
8388e41f4b71Sopenharmony_ci| Name  | Type                                                 | Mandatory| Description                                                |
8389e41f4b71Sopenharmony_ci| -------- | ----------------------------------------------------- | ---- | ---------------------------------------------------- |
8390e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[X509CertRawData](#x509certrawdata9)\> | Yes  | Callback used to return the result. If the operation fails, an error message is returned.|
8391e41f4b71Sopenharmony_ci
8392e41f4b71Sopenharmony_ci**Error codes**
8393e41f4b71Sopenharmony_ci
8394e41f4b71Sopenharmony_ci| ID| Error Message              |
8395e41f4b71Sopenharmony_ci| -------- | ---------------------- |
8396e41f4b71Sopenharmony_ci| 401      | Parameter error.       |
8397e41f4b71Sopenharmony_ci| 2303501  | SSL is null.           |
8398e41f4b71Sopenharmony_ci| 2300002  | System internal error. |
8399e41f4b71Sopenharmony_ci
8400e41f4b71Sopenharmony_ci**Example**
8401e41f4b71Sopenharmony_ci
8402e41f4b71Sopenharmony_ci```ts
8403e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
8404e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
8405e41f4b71Sopenharmony_ciimport { util } from '@kit.ArkTS';
8406e41f4b71Sopenharmony_ci
8407e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
8408e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
8409e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
8410e41f4b71Sopenharmony_ci  port: 8080
8411e41f4b71Sopenharmony_ci}
8412e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
8413e41f4b71Sopenharmony_ci  key: "xxxx",
8414e41f4b71Sopenharmony_ci  cert: "xxxx",
8415e41f4b71Sopenharmony_ci  ca: ["xxxx"],
8416e41f4b71Sopenharmony_ci  password: "xxxx",
8417e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
8418e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
8419e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
8420e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
8421e41f4b71Sopenharmony_ci}
8422e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
8423e41f4b71Sopenharmony_ci  address: netAddress,
8424e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
8425e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
8426e41f4b71Sopenharmony_ci}
8427e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
8428e41f4b71Sopenharmony_ci  console.log("listen callback success");
8429e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
8430e41f4b71Sopenharmony_ci  console.log("failed" + err);
8431e41f4b71Sopenharmony_ci});
8432e41f4b71Sopenharmony_citlsServer.on('connect', (client: socket.TLSSocketConnection) => {
8433e41f4b71Sopenharmony_ci  client.getRemoteCertificate((err: BusinessError, data: socket.X509CertRawData) => {
8434e41f4b71Sopenharmony_ci    if (err) {
8435e41f4b71Sopenharmony_ci      console.log("getRemoteCertificate callback error: " + err);
8436e41f4b71Sopenharmony_ci    } else {
8437e41f4b71Sopenharmony_ci      const decoder = util.TextDecoder.create();
8438e41f4b71Sopenharmony_ci      const str = decoder.decodeWithStream(data.data);
8439e41f4b71Sopenharmony_ci      console.log("getRemoteCertificate callback: " + str);
8440e41f4b71Sopenharmony_ci    }
8441e41f4b71Sopenharmony_ci  });
8442e41f4b71Sopenharmony_ci});
8443e41f4b71Sopenharmony_ci```
8444e41f4b71Sopenharmony_ci
8445e41f4b71Sopenharmony_ci### getRemoteCertificate<sup>10+</sup>
8446e41f4b71Sopenharmony_ci
8447e41f4b71Sopenharmony_cigetRemoteCertificate():Promise\<[X509CertRawData](#x509certrawdata9)\>
8448e41f4b71Sopenharmony_ci
8449e41f4b71Sopenharmony_ciObtains the digital certificate of the peer end after a TLS socket server connection is established. This API uses a promise to return the result. It applies only to the scenario where the client sends a certificate to the server. 
8450e41f4b71Sopenharmony_ci
8451e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
8452e41f4b71Sopenharmony_ci
8453e41f4b71Sopenharmony_ci**Return value**
8454e41f4b71Sopenharmony_ci
8455e41f4b71Sopenharmony_ci| Type                                           | Description                                                        |
8456e41f4b71Sopenharmony_ci| ----------------------------------------------- | ------------------------------------------------------------ |
8457e41f4b71Sopenharmony_ci| Promise\<[X509CertRawData](#x509certrawdata9)\> | Promise used to return the result. If the operation fails, an error message is returned.|
8458e41f4b71Sopenharmony_ci
8459e41f4b71Sopenharmony_ci**Error codes**
8460e41f4b71Sopenharmony_ci
8461e41f4b71Sopenharmony_ci| ID| Error Message              |
8462e41f4b71Sopenharmony_ci| -------- | ---------------------- |
8463e41f4b71Sopenharmony_ci| 2303501  | SSL is null.           |
8464e41f4b71Sopenharmony_ci| 2300002  | System internal error. |
8465e41f4b71Sopenharmony_ci
8466e41f4b71Sopenharmony_ci**Example**
8467e41f4b71Sopenharmony_ci
8468e41f4b71Sopenharmony_ci```ts
8469e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
8470e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
8471e41f4b71Sopenharmony_ciimport { util } from '@kit.ArkTS';
8472e41f4b71Sopenharmony_ci
8473e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
8474e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
8475e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
8476e41f4b71Sopenharmony_ci  port: 8080
8477e41f4b71Sopenharmony_ci}
8478e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
8479e41f4b71Sopenharmony_ci  key: "xxxx",
8480e41f4b71Sopenharmony_ci  cert: "xxxx",
8481e41f4b71Sopenharmony_ci  ca: ["xxxx"],
8482e41f4b71Sopenharmony_ci  password: "xxxx",
8483e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
8484e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
8485e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
8486e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
8487e41f4b71Sopenharmony_ci}
8488e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
8489e41f4b71Sopenharmony_ci  address: netAddress,
8490e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
8491e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
8492e41f4b71Sopenharmony_ci}
8493e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
8494e41f4b71Sopenharmony_ci  console.log("listen callback success");
8495e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
8496e41f4b71Sopenharmony_ci  console.log("failed" + err);
8497e41f4b71Sopenharmony_ci});
8498e41f4b71Sopenharmony_citlsServer.on('connect', (client: socket.TLSSocketConnection) => {
8499e41f4b71Sopenharmony_ci  client.getRemoteCertificate().then((data: socket.X509CertRawData) => {
8500e41f4b71Sopenharmony_ci    const decoder = util.TextDecoder.create();
8501e41f4b71Sopenharmony_ci    const str = decoder.decodeWithStream(data.data);
8502e41f4b71Sopenharmony_ci    console.log("getRemoteCertificate success: " + str);
8503e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
8504e41f4b71Sopenharmony_ci    console.error("failed" + err);
8505e41f4b71Sopenharmony_ci  });
8506e41f4b71Sopenharmony_ci});
8507e41f4b71Sopenharmony_ci```
8508e41f4b71Sopenharmony_ci
8509e41f4b71Sopenharmony_ci### getCipherSuite<sup>10+</sup>
8510e41f4b71Sopenharmony_ci
8511e41f4b71Sopenharmony_cigetCipherSuite(callback: AsyncCallback\<Array\<string\>\>): void
8512e41f4b71Sopenharmony_ci
8513e41f4b71Sopenharmony_ciObtains the cipher suite negotiated by both communication parties after a TLS socket server connection is established. This API uses an asynchronous callback to return the result.
8514e41f4b71Sopenharmony_ci
8515e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
8516e41f4b71Sopenharmony_ci
8517e41f4b71Sopenharmony_ci**Parameters**
8518e41f4b71Sopenharmony_ci
8519e41f4b71Sopenharmony_ci| Name  | Type                            | Mandatory| Description                                                        |
8520e41f4b71Sopenharmony_ci| -------- | -------------------------------- | ---- | ------------------------------------------------------------ |
8521e41f4b71Sopenharmony_ci| callback | AsyncCallback\<Array\<string\>\> | Yes  | Callback used to return the result. If the operation fails, an error message is returned.|
8522e41f4b71Sopenharmony_ci
8523e41f4b71Sopenharmony_ci**Error codes**
8524e41f4b71Sopenharmony_ci
8525e41f4b71Sopenharmony_ci| ID| Error Message                              |
8526e41f4b71Sopenharmony_ci| -------- | -------------------------------------- |
8527e41f4b71Sopenharmony_ci| 401      | Parameter error.                       |
8528e41f4b71Sopenharmony_ci| 2303501  | SSL is null.                           |
8529e41f4b71Sopenharmony_ci| 2303502  | An error occurred when reading data on the TLS socket.|
8530e41f4b71Sopenharmony_ci| 2303505  | An error occurred in the TLS system call.|
8531e41f4b71Sopenharmony_ci| 2300002  | System internal error.                 |
8532e41f4b71Sopenharmony_ci
8533e41f4b71Sopenharmony_ci**Example**
8534e41f4b71Sopenharmony_ci
8535e41f4b71Sopenharmony_ci```ts
8536e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
8537e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
8538e41f4b71Sopenharmony_ci
8539e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
8540e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
8541e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
8542e41f4b71Sopenharmony_ci  port: 8080
8543e41f4b71Sopenharmony_ci}
8544e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
8545e41f4b71Sopenharmony_ci  key: "xxxx",
8546e41f4b71Sopenharmony_ci  cert: "xxxx",
8547e41f4b71Sopenharmony_ci  ca: ["xxxx"],
8548e41f4b71Sopenharmony_ci  password: "xxxx",
8549e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
8550e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
8551e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
8552e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
8553e41f4b71Sopenharmony_ci}
8554e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
8555e41f4b71Sopenharmony_ci  address: netAddress,
8556e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
8557e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
8558e41f4b71Sopenharmony_ci}
8559e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
8560e41f4b71Sopenharmony_ci  console.log("listen callback success");
8561e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
8562e41f4b71Sopenharmony_ci  console.log("failed" + err);
8563e41f4b71Sopenharmony_ci});
8564e41f4b71Sopenharmony_citlsServer.on('connect', (client: socket.TLSSocketConnection) => {
8565e41f4b71Sopenharmony_ci  client.getCipherSuite((err: BusinessError, data: Array<string>) => {
8566e41f4b71Sopenharmony_ci    if (err) {
8567e41f4b71Sopenharmony_ci      console.log("getCipherSuite callback error = " + err);
8568e41f4b71Sopenharmony_ci    } else {
8569e41f4b71Sopenharmony_ci      console.log("getCipherSuite callback = " + data);
8570e41f4b71Sopenharmony_ci    }
8571e41f4b71Sopenharmony_ci  });
8572e41f4b71Sopenharmony_ci});
8573e41f4b71Sopenharmony_ci```
8574e41f4b71Sopenharmony_ci
8575e41f4b71Sopenharmony_ci### getCipherSuite<sup>10+</sup>
8576e41f4b71Sopenharmony_ci
8577e41f4b71Sopenharmony_cigetCipherSuite(): Promise\<Array\<string\>\>
8578e41f4b71Sopenharmony_ci
8579e41f4b71Sopenharmony_ciObtains the cipher suite negotiated by both communication parties after a TLS socket server connection is established. This API uses a promise to return the result.
8580e41f4b71Sopenharmony_ci
8581e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
8582e41f4b71Sopenharmony_ci
8583e41f4b71Sopenharmony_ci**Return value**
8584e41f4b71Sopenharmony_ci
8585e41f4b71Sopenharmony_ci| Type                      | Description                                                        |
8586e41f4b71Sopenharmony_ci| -------------------------- | ------------------------------------------------------------ |
8587e41f4b71Sopenharmony_ci| Promise\<Array\<string\>\> | Promise used to return the result. If the operation fails, an error message is returned.|
8588e41f4b71Sopenharmony_ci
8589e41f4b71Sopenharmony_ci**Error codes**
8590e41f4b71Sopenharmony_ci
8591e41f4b71Sopenharmony_ci| ID| Error Message                              |
8592e41f4b71Sopenharmony_ci| -------- | -------------------------------------- |
8593e41f4b71Sopenharmony_ci| 2303501  | SSL is null.                           |
8594e41f4b71Sopenharmony_ci| 2303502  | An error occurred when reading data on the TLS socket.|
8595e41f4b71Sopenharmony_ci| 2303505  | An error occurred in the TLS system call. |
8596e41f4b71Sopenharmony_ci| 2300002  | System internal error.                 |
8597e41f4b71Sopenharmony_ci
8598e41f4b71Sopenharmony_ci**Example**
8599e41f4b71Sopenharmony_ci
8600e41f4b71Sopenharmony_ci```ts
8601e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
8602e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
8603e41f4b71Sopenharmony_ci
8604e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
8605e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
8606e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
8607e41f4b71Sopenharmony_ci  port: 8080
8608e41f4b71Sopenharmony_ci}
8609e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
8610e41f4b71Sopenharmony_ci  key: "xxxx",
8611e41f4b71Sopenharmony_ci  cert: "xxxx",
8612e41f4b71Sopenharmony_ci  ca: ["xxxx"],
8613e41f4b71Sopenharmony_ci  password: "xxxx",
8614e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
8615e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
8616e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
8617e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
8618e41f4b71Sopenharmony_ci}
8619e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
8620e41f4b71Sopenharmony_ci  address: netAddress,
8621e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
8622e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
8623e41f4b71Sopenharmony_ci}
8624e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
8625e41f4b71Sopenharmony_ci  console.log("listen callback success");
8626e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
8627e41f4b71Sopenharmony_ci  console.log("failed" + err);
8628e41f4b71Sopenharmony_ci});
8629e41f4b71Sopenharmony_citlsServer.on('connect', (client: socket.TLSSocketConnection) => {
8630e41f4b71Sopenharmony_ci  client.getCipherSuite().then((data: Array<string>) => {
8631e41f4b71Sopenharmony_ci    console.log('getCipherSuite success:' + JSON.stringify(data));
8632e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
8633e41f4b71Sopenharmony_ci    console.error("failed" + err);
8634e41f4b71Sopenharmony_ci  });
8635e41f4b71Sopenharmony_ci});
8636e41f4b71Sopenharmony_ci```
8637e41f4b71Sopenharmony_ci
8638e41f4b71Sopenharmony_ci### getSignatureAlgorithms<sup>10+</sup>
8639e41f4b71Sopenharmony_ci
8640e41f4b71Sopenharmony_cigetSignatureAlgorithms(callback: AsyncCallback\<Array\<string\>\>): void
8641e41f4b71Sopenharmony_ci
8642e41f4b71Sopenharmony_ciObtains the signing algorithm negotiated by both communication parties after a TLS socket server connection is established. This API uses an asynchronous callback to return the result.
8643e41f4b71Sopenharmony_ci
8644e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
8645e41f4b71Sopenharmony_ci
8646e41f4b71Sopenharmony_ci**Parameters**
8647e41f4b71Sopenharmony_ci
8648e41f4b71Sopenharmony_ci| Name  | Type                            | Mandatory| Description                              |
8649e41f4b71Sopenharmony_ci| -------- | -------------------------------- | ---- | ---------------------------------- |
8650e41f4b71Sopenharmony_ci| callback | AsyncCallback\<Array\<string\>\> | Yes  | Callback used to return the result. |
8651e41f4b71Sopenharmony_ci
8652e41f4b71Sopenharmony_ci**Error codes**
8653e41f4b71Sopenharmony_ci
8654e41f4b71Sopenharmony_ci| ID| Error Message              |
8655e41f4b71Sopenharmony_ci| -------- | ---------------------- |
8656e41f4b71Sopenharmony_ci| 401      | Parameter error.       |
8657e41f4b71Sopenharmony_ci| 2303501  | SSL is null.           |
8658e41f4b71Sopenharmony_ci| 2300002  | System internal error. |
8659e41f4b71Sopenharmony_ci
8660e41f4b71Sopenharmony_ci**Example**
8661e41f4b71Sopenharmony_ci
8662e41f4b71Sopenharmony_ci```ts
8663e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
8664e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
8665e41f4b71Sopenharmony_ci
8666e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
8667e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
8668e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
8669e41f4b71Sopenharmony_ci  port: 8080
8670e41f4b71Sopenharmony_ci}
8671e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
8672e41f4b71Sopenharmony_ci  key: "xxxx",
8673e41f4b71Sopenharmony_ci  cert: "xxxx",
8674e41f4b71Sopenharmony_ci  ca: ["xxxx"],
8675e41f4b71Sopenharmony_ci  password: "xxxx",
8676e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
8677e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
8678e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
8679e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
8680e41f4b71Sopenharmony_ci}
8681e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
8682e41f4b71Sopenharmony_ci  address: netAddress,
8683e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
8684e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
8685e41f4b71Sopenharmony_ci}
8686e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
8687e41f4b71Sopenharmony_ci  console.log("listen callback success");
8688e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
8689e41f4b71Sopenharmony_ci  console.log("failed" + err);
8690e41f4b71Sopenharmony_ci});
8691e41f4b71Sopenharmony_citlsServer.on('connect', (client: socket.TLSSocketConnection) => {
8692e41f4b71Sopenharmony_ci  client.getSignatureAlgorithms((err: BusinessError, data: Array<string>) => {
8693e41f4b71Sopenharmony_ci    if (err) {
8694e41f4b71Sopenharmony_ci      console.log("getSignatureAlgorithms callback error = " + err);
8695e41f4b71Sopenharmony_ci    } else {
8696e41f4b71Sopenharmony_ci      console.log("getSignatureAlgorithms callback = " + data);
8697e41f4b71Sopenharmony_ci    }
8698e41f4b71Sopenharmony_ci  });
8699e41f4b71Sopenharmony_ci});
8700e41f4b71Sopenharmony_ci```
8701e41f4b71Sopenharmony_ci
8702e41f4b71Sopenharmony_ci### getSignatureAlgorithms<sup>10+</sup>
8703e41f4b71Sopenharmony_ci
8704e41f4b71Sopenharmony_cigetSignatureAlgorithms(): Promise\<Array\<string\>\>
8705e41f4b71Sopenharmony_ci
8706e41f4b71Sopenharmony_ciObtains the signing algorithm negotiated by both communication parties after a TLS socket server connection is established. This API uses a promise to return the result.
8707e41f4b71Sopenharmony_ci
8708e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
8709e41f4b71Sopenharmony_ci
8710e41f4b71Sopenharmony_ci**Return value**
8711e41f4b71Sopenharmony_ci
8712e41f4b71Sopenharmony_ci| Type                      | Description                                         |
8713e41f4b71Sopenharmony_ci| -------------------------- | --------------------------------------------- |
8714e41f4b71Sopenharmony_ci| Promise\<Array\<string\>\> | Promise used to return the result.|
8715e41f4b71Sopenharmony_ci
8716e41f4b71Sopenharmony_ci**Error codes**
8717e41f4b71Sopenharmony_ci
8718e41f4b71Sopenharmony_ci| ID| Error Message              |
8719e41f4b71Sopenharmony_ci| -------- | ---------------------- |
8720e41f4b71Sopenharmony_ci| 2303501  | SSL is null.           |
8721e41f4b71Sopenharmony_ci| 2300002  | System internal error. |
8722e41f4b71Sopenharmony_ci
8723e41f4b71Sopenharmony_ci**Example**
8724e41f4b71Sopenharmony_ci
8725e41f4b71Sopenharmony_ci```ts
8726e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
8727e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
8728e41f4b71Sopenharmony_ci
8729e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
8730e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
8731e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
8732e41f4b71Sopenharmony_ci  port: 8080
8733e41f4b71Sopenharmony_ci}
8734e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
8735e41f4b71Sopenharmony_ci  key: "xxxx",
8736e41f4b71Sopenharmony_ci  cert: "xxxx",
8737e41f4b71Sopenharmony_ci  ca: ["xxxx"],
8738e41f4b71Sopenharmony_ci  password: "xxxx",
8739e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
8740e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
8741e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
8742e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
8743e41f4b71Sopenharmony_ci}
8744e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
8745e41f4b71Sopenharmony_ci  address: netAddress,
8746e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
8747e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
8748e41f4b71Sopenharmony_ci}
8749e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
8750e41f4b71Sopenharmony_ci  console.log("listen callback success");
8751e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
8752e41f4b71Sopenharmony_ci  console.log("failed" + err);
8753e41f4b71Sopenharmony_ci});
8754e41f4b71Sopenharmony_citlsServer.on('connect', (client: socket.TLSSocketConnection) => {
8755e41f4b71Sopenharmony_ci  client.getSignatureAlgorithms().then((data: Array<string>) => {
8756e41f4b71Sopenharmony_ci    console.log("getSignatureAlgorithms success" + data);
8757e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
8758e41f4b71Sopenharmony_ci    console.error("failed" + err);
8759e41f4b71Sopenharmony_ci  });
8760e41f4b71Sopenharmony_ci});
8761e41f4b71Sopenharmony_ci```
8762e41f4b71Sopenharmony_ci
8763e41f4b71Sopenharmony_ci### getLocalAddress<sup>12+</sup>
8764e41f4b71Sopenharmony_ci
8765e41f4b71Sopenharmony_cigetLocalAddress(): Promise\<NetAddress\>
8766e41f4b71Sopenharmony_ci
8767e41f4b71Sopenharmony_ciObtains the local socket address of a **TLSSocketConnection** connection. This API uses a promise to return the result.
8768e41f4b71Sopenharmony_ci
8769e41f4b71Sopenharmony_ci> **NOTE**
8770e41f4b71Sopenharmony_ci> Call this API only after the **TLSSocketServer** connection is successfully established.
8771e41f4b71Sopenharmony_ci
8772e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
8773e41f4b71Sopenharmony_ci
8774e41f4b71Sopenharmony_ci**Return value**
8775e41f4b71Sopenharmony_ci
8776e41f4b71Sopenharmony_ci| Type           | Description                                                |
8777e41f4b71Sopenharmony_ci|  -------------- |  --------------------------------------------------- |
8778e41f4b71Sopenharmony_ci| Promise\<[NetAddress](#netaddress)\> | Promise used to return the result.|
8779e41f4b71Sopenharmony_ci
8780e41f4b71Sopenharmony_ci**Error codes**
8781e41f4b71Sopenharmony_ci
8782e41f4b71Sopenharmony_ci| ID| Error Message                                   |
8783e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- |
8784e41f4b71Sopenharmony_ci| 2300002  | System internal error.                      |
8785e41f4b71Sopenharmony_ci| 2301009  | Bad file descriptor.                            |
8786e41f4b71Sopenharmony_ci| 2303188  | Socket operation on non-socket. |
8787e41f4b71Sopenharmony_ci
8788e41f4b71Sopenharmony_ci**Example**
8789e41f4b71Sopenharmony_ci
8790e41f4b71Sopenharmony_ci```ts
8791e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
8792e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
8793e41f4b71Sopenharmony_ci
8794e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
8795e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
8796e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
8797e41f4b71Sopenharmony_ci  port: 8080
8798e41f4b71Sopenharmony_ci}
8799e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
8800e41f4b71Sopenharmony_ci  key: "xxxx",
8801e41f4b71Sopenharmony_ci  cert: "xxxx",
8802e41f4b71Sopenharmony_ci  ca: ["xxxx"],
8803e41f4b71Sopenharmony_ci  password: "xxxx",
8804e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
8805e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
8806e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
8807e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
8808e41f4b71Sopenharmony_ci}
8809e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
8810e41f4b71Sopenharmony_ci  address: netAddress,
8811e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
8812e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
8813e41f4b71Sopenharmony_ci}
8814e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
8815e41f4b71Sopenharmony_ci  console.info("listen callback success");
8816e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
8817e41f4b71Sopenharmony_ci  console.error("failed" + err);
8818e41f4b71Sopenharmony_ci});
8819e41f4b71Sopenharmony_ci
8820e41f4b71Sopenharmony_citlsServer.on('connect', (client: socket.TLSSocketConnection) => {
8821e41f4b71Sopenharmony_ci  client.getLocalAddress().then((localAddress: socket.NetAddress) => {
8822e41f4b71Sopenharmony_ci    console.info("Family IP Port: " + JSON.stringify(localAddress));
8823e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
8824e41f4b71Sopenharmony_ci    console.error("TLS Client Get Family IP Port failed, error: " + JSON.stringify(err));
8825e41f4b71Sopenharmony_ci  })
8826e41f4b71Sopenharmony_ci});
8827e41f4b71Sopenharmony_ci```
8828e41f4b71Sopenharmony_ci
8829e41f4b71Sopenharmony_ci### on('message')<sup>10+</sup>
8830e41f4b71Sopenharmony_ci
8831e41f4b71Sopenharmony_cion(type: 'message', callback: Callback\<SocketMessageInfo\>): void
8832e41f4b71Sopenharmony_ci
8833e41f4b71Sopenharmony_ciSubscribes to **message** events of a **TLSSocketConnection** object. This API uses an asynchronous callback to return the result.
8834e41f4b71Sopenharmony_ci
8835e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
8836e41f4b71Sopenharmony_ci
8837e41f4b71Sopenharmony_ci**Parameters**
8838e41f4b71Sopenharmony_ci
8839e41f4b71Sopenharmony_ci| Name  | Type                                                        | Mandatory| Description                                     |
8840e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------- |
8841e41f4b71Sopenharmony_ci| type     | string                                                       | Yes  | Event type.<br/> **message**: message receiving event.|
8842e41f4b71Sopenharmony_ci| callback | Callback\<[SocketMessageInfo](#socketmessageinfo11)\> | Yes  | Callback used to return the result. If the operation is successful, the TLS socket connection information is returned. If the operation fails, an error message is returned.                              |
8843e41f4b71Sopenharmony_ci
8844e41f4b71Sopenharmony_ci**Error codes**
8845e41f4b71Sopenharmony_ci
8846e41f4b71Sopenharmony_ci| ID| Error Message        |
8847e41f4b71Sopenharmony_ci| -------- | ---------------- |
8848e41f4b71Sopenharmony_ci| 401      | Parameter error. |
8849e41f4b71Sopenharmony_ci
8850e41f4b71Sopenharmony_ci**Example**
8851e41f4b71Sopenharmony_ci
8852e41f4b71Sopenharmony_ci```ts
8853e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
8854e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
8855e41f4b71Sopenharmony_ci
8856e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
8857e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
8858e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
8859e41f4b71Sopenharmony_ci  port: 8080
8860e41f4b71Sopenharmony_ci}
8861e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
8862e41f4b71Sopenharmony_ci  key: "xxxx",
8863e41f4b71Sopenharmony_ci  cert: "xxxx",
8864e41f4b71Sopenharmony_ci  ca: ["xxxx"],
8865e41f4b71Sopenharmony_ci  password: "xxxx",
8866e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
8867e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
8868e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
8869e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
8870e41f4b71Sopenharmony_ci}
8871e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
8872e41f4b71Sopenharmony_ci  address: netAddress,
8873e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
8874e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
8875e41f4b71Sopenharmony_ci}
8876e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
8877e41f4b71Sopenharmony_ci  console.log("listen callback success");
8878e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
8879e41f4b71Sopenharmony_ci  console.log("failed" + err);
8880e41f4b71Sopenharmony_ci});
8881e41f4b71Sopenharmony_ci
8882e41f4b71Sopenharmony_citlsServer.on('connect', (client: socket.TLSSocketConnection) => {
8883e41f4b71Sopenharmony_ci  client.on('message', (value: socket.SocketMessageInfo) => {
8884e41f4b71Sopenharmony_ci    let messageView = '';
8885e41f4b71Sopenharmony_ci    for (let i: number = 0; i < value.message.byteLength; i++) {
8886e41f4b71Sopenharmony_ci      let uint8Array = new Uint8Array(value.message) 
8887e41f4b71Sopenharmony_ci      let messages = uint8Array[i]
8888e41f4b71Sopenharmony_ci      let message = String.fromCharCode(messages);
8889e41f4b71Sopenharmony_ci      messageView += message;
8890e41f4b71Sopenharmony_ci    }
8891e41f4b71Sopenharmony_ci    console.log('on message message: ' + JSON.stringify(messageView));
8892e41f4b71Sopenharmony_ci    console.log('remoteInfo: ' + JSON.stringify(value.remoteInfo));
8893e41f4b71Sopenharmony_ci  });
8894e41f4b71Sopenharmony_ci});
8895e41f4b71Sopenharmony_ci```
8896e41f4b71Sopenharmony_ci
8897e41f4b71Sopenharmony_ci### off('message')<sup>10+</sup>
8898e41f4b71Sopenharmony_ci
8899e41f4b71Sopenharmony_cioff(type: 'message', callback?: Callback\<SocketMessageInfo\>): void
8900e41f4b71Sopenharmony_ci
8901e41f4b71Sopenharmony_ciUnsubscribes from **message** events of a **TLSSocketConnection** object. This API uses an asynchronous callback to return the result.
8902e41f4b71Sopenharmony_ci
8903e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
8904e41f4b71Sopenharmony_ci
8905e41f4b71Sopenharmony_ci**Parameters**
8906e41f4b71Sopenharmony_ci
8907e41f4b71Sopenharmony_ci| Name  | Type                                                        | Mandatory| Description                                     |
8908e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------- |
8909e41f4b71Sopenharmony_ci| type     | string                                                       | Yes  | Event type.<br/> **message**: message receiving event.|
8910e41f4b71Sopenharmony_ci| callback | Callback\<[SocketMessageInfo](#socketmessageinfo11)\> | No  | Callback used to return the result. If the operation is successful, the TLS socket connection information is returned. If the operation fails, an error message is returned. |
8911e41f4b71Sopenharmony_ci
8912e41f4b71Sopenharmony_ci**Error codes**
8913e41f4b71Sopenharmony_ci
8914e41f4b71Sopenharmony_ci| ID| Error Message        |
8915e41f4b71Sopenharmony_ci| -------- | ---------------- |
8916e41f4b71Sopenharmony_ci| 401      | Parameter error. |
8917e41f4b71Sopenharmony_ci
8918e41f4b71Sopenharmony_ci**Example**
8919e41f4b71Sopenharmony_ci
8920e41f4b71Sopenharmony_ci```ts
8921e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
8922e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
8923e41f4b71Sopenharmony_ci
8924e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
8925e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
8926e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
8927e41f4b71Sopenharmony_ci  port: 8080
8928e41f4b71Sopenharmony_ci}
8929e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
8930e41f4b71Sopenharmony_ci  key: "xxxx",
8931e41f4b71Sopenharmony_ci  cert: "xxxx",
8932e41f4b71Sopenharmony_ci  ca: ["xxxx"],
8933e41f4b71Sopenharmony_ci  password: "xxxx",
8934e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
8935e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
8936e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
8937e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
8938e41f4b71Sopenharmony_ci}
8939e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
8940e41f4b71Sopenharmony_ci  address: netAddress,
8941e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
8942e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
8943e41f4b71Sopenharmony_ci}
8944e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
8945e41f4b71Sopenharmony_ci  console.log("listen callback success");
8946e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
8947e41f4b71Sopenharmony_ci  console.log("failed" + err);
8948e41f4b71Sopenharmony_ci});
8949e41f4b71Sopenharmony_ci
8950e41f4b71Sopenharmony_cilet callback = (value: socket.SocketMessageInfo) => {
8951e41f4b71Sopenharmony_ci  let messageView = '';
8952e41f4b71Sopenharmony_ci  for (let i: number = 0; i < value.message.byteLength; i++) {
8953e41f4b71Sopenharmony_ci    let uint8Array = new Uint8Array(value.message) 
8954e41f4b71Sopenharmony_ci    let messages = uint8Array[i]
8955e41f4b71Sopenharmony_ci    let message = String.fromCharCode(messages);
8956e41f4b71Sopenharmony_ci    messageView += message;
8957e41f4b71Sopenharmony_ci  }
8958e41f4b71Sopenharmony_ci  console.log('on message message: ' + JSON.stringify(messageView));
8959e41f4b71Sopenharmony_ci  console.log('remoteInfo: ' + JSON.stringify(value.remoteInfo));
8960e41f4b71Sopenharmony_ci}
8961e41f4b71Sopenharmony_citlsServer.on('connect', (client: socket.TLSSocketConnection) => {
8962e41f4b71Sopenharmony_ci  client.on('message', callback);
8963e41f4b71Sopenharmony_ci  // You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
8964e41f4b71Sopenharmony_ci  client.off('message', callback);
8965e41f4b71Sopenharmony_ci  client.off('message');
8966e41f4b71Sopenharmony_ci});
8967e41f4b71Sopenharmony_ci```
8968e41f4b71Sopenharmony_ci
8969e41f4b71Sopenharmony_ci### on('close')<sup>10+</sup>
8970e41f4b71Sopenharmony_ci
8971e41f4b71Sopenharmony_cion(type: 'close', callback: Callback\<void\>): void
8972e41f4b71Sopenharmony_ci
8973e41f4b71Sopenharmony_ciSubscribes to **close** events of a **TLSSocketConnection** object. This API uses an asynchronous callback to return the result.
8974e41f4b71Sopenharmony_ci
8975e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
8976e41f4b71Sopenharmony_ci
8977e41f4b71Sopenharmony_ci**Parameters**
8978e41f4b71Sopenharmony_ci
8979e41f4b71Sopenharmony_ci| Name  | Type            | Mandatory| Description                               |
8980e41f4b71Sopenharmony_ci| -------- | ---------------- | ---- | ----------------------------------- |
8981e41f4b71Sopenharmony_ci| type     | string           | Yes  | Event type.<br/> **close**: close event.|
8982e41f4b71Sopenharmony_ci| callback | Callback\<void\> | Yes  | Callback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.   |
8983e41f4b71Sopenharmony_ci
8984e41f4b71Sopenharmony_ci**Error codes**
8985e41f4b71Sopenharmony_ci
8986e41f4b71Sopenharmony_ci| ID| Error Message        |
8987e41f4b71Sopenharmony_ci| -------- | ---------------- |
8988e41f4b71Sopenharmony_ci| 401      | Parameter error. |
8989e41f4b71Sopenharmony_ci
8990e41f4b71Sopenharmony_ci**Example**
8991e41f4b71Sopenharmony_ci
8992e41f4b71Sopenharmony_ci```ts
8993e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
8994e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
8995e41f4b71Sopenharmony_ci
8996e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
8997e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
8998e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
8999e41f4b71Sopenharmony_ci  port: 8080
9000e41f4b71Sopenharmony_ci}
9001e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
9002e41f4b71Sopenharmony_ci  key: "xxxx",
9003e41f4b71Sopenharmony_ci  cert: "xxxx",
9004e41f4b71Sopenharmony_ci  ca: ["xxxx"],
9005e41f4b71Sopenharmony_ci  password: "xxxx",
9006e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
9007e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
9008e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
9009e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
9010e41f4b71Sopenharmony_ci}
9011e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
9012e41f4b71Sopenharmony_ci  address: netAddress,
9013e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
9014e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
9015e41f4b71Sopenharmony_ci}
9016e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
9017e41f4b71Sopenharmony_ci  console.log("listen callback success");
9018e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
9019e41f4b71Sopenharmony_ci  console.log("failed" + err);
9020e41f4b71Sopenharmony_ci});
9021e41f4b71Sopenharmony_citlsServer.on('connect', (client: socket.TLSSocketConnection) => {
9022e41f4b71Sopenharmony_ci  client.on('close', () => {
9023e41f4b71Sopenharmony_ci    console.log("on close success")
9024e41f4b71Sopenharmony_ci  });
9025e41f4b71Sopenharmony_ci});
9026e41f4b71Sopenharmony_ci```
9027e41f4b71Sopenharmony_ci
9028e41f4b71Sopenharmony_ci### off('close')<sup>10+</sup>
9029e41f4b71Sopenharmony_ci
9030e41f4b71Sopenharmony_cioff(type: 'close', callback?: Callback\<void\>): void
9031e41f4b71Sopenharmony_ci
9032e41f4b71Sopenharmony_ciUnsubscribes from **close** events of a **TLSSocketConnection** object. This API uses an asynchronous callback to return the result.
9033e41f4b71Sopenharmony_ci
9034e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
9035e41f4b71Sopenharmony_ci
9036e41f4b71Sopenharmony_ci**Parameters**
9037e41f4b71Sopenharmony_ci
9038e41f4b71Sopenharmony_ci| Name  | Type            | Mandatory| Description                               |
9039e41f4b71Sopenharmony_ci| -------- | ---------------- | ---- | ----------------------------------- |
9040e41f4b71Sopenharmony_ci| type     | string           | Yes  | Event type.<br/> **close**: close event.|
9041e41f4b71Sopenharmony_ci| callback | Callback\<void\> | No  | Callback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.                        |
9042e41f4b71Sopenharmony_ci
9043e41f4b71Sopenharmony_ci**Error codes**
9044e41f4b71Sopenharmony_ci
9045e41f4b71Sopenharmony_ci| ID| Error Message        |
9046e41f4b71Sopenharmony_ci| -------- | ---------------- |
9047e41f4b71Sopenharmony_ci| 401      | Parameter error. |
9048e41f4b71Sopenharmony_ci
9049e41f4b71Sopenharmony_ci**Example**
9050e41f4b71Sopenharmony_ci
9051e41f4b71Sopenharmony_ci```ts
9052e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
9053e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
9054e41f4b71Sopenharmony_ci
9055e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
9056e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
9057e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
9058e41f4b71Sopenharmony_ci  port: 8080
9059e41f4b71Sopenharmony_ci}
9060e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
9061e41f4b71Sopenharmony_ci  key: "xxxx",
9062e41f4b71Sopenharmony_ci  cert: "xxxx",
9063e41f4b71Sopenharmony_ci  ca: ["xxxx"],
9064e41f4b71Sopenharmony_ci  password: "xxxx",
9065e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
9066e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
9067e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
9068e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
9069e41f4b71Sopenharmony_ci}
9070e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
9071e41f4b71Sopenharmony_ci  address: netAddress,
9072e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
9073e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
9074e41f4b71Sopenharmony_ci}
9075e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
9076e41f4b71Sopenharmony_ci  console.log("listen callback success");
9077e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
9078e41f4b71Sopenharmony_ci  console.log("failed" + err);
9079e41f4b71Sopenharmony_ci});
9080e41f4b71Sopenharmony_ci
9081e41f4b71Sopenharmony_cilet callback = () => {
9082e41f4b71Sopenharmony_ci  console.log("on close success");
9083e41f4b71Sopenharmony_ci}
9084e41f4b71Sopenharmony_citlsServer.on('connect', (client: socket.TLSSocketConnection) => {
9085e41f4b71Sopenharmony_ci  client.on('close', callback);
9086e41f4b71Sopenharmony_ci  // You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
9087e41f4b71Sopenharmony_ci  client.off('close', callback);
9088e41f4b71Sopenharmony_ci  client.off('close');
9089e41f4b71Sopenharmony_ci});
9090e41f4b71Sopenharmony_ci```
9091e41f4b71Sopenharmony_ci
9092e41f4b71Sopenharmony_ci### on('error')<sup>10+</sup>
9093e41f4b71Sopenharmony_ci
9094e41f4b71Sopenharmony_cion(type: 'error', callback: ErrorCallback): void
9095e41f4b71Sopenharmony_ci
9096e41f4b71Sopenharmony_ciSubscribes to **error** events of a **TLSSocketConnection** object. This API uses an asynchronous callback to return the result.
9097e41f4b71Sopenharmony_ci
9098e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
9099e41f4b71Sopenharmony_ci
9100e41f4b71Sopenharmony_ci**Parameters**
9101e41f4b71Sopenharmony_ci
9102e41f4b71Sopenharmony_ci| Name  | Type         | Mandatory| Description                                |
9103e41f4b71Sopenharmony_ci| -------- | ------------- | ---- | ------------------------------------ |
9104e41f4b71Sopenharmony_ci| type     | string        | Yes  | Event type.<br/> **error**: error event.|
9105e41f4b71Sopenharmony_ci| callback | ErrorCallback | Yes  | Callback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.                       |
9106e41f4b71Sopenharmony_ci
9107e41f4b71Sopenharmony_ci**Error codes**
9108e41f4b71Sopenharmony_ci
9109e41f4b71Sopenharmony_ci| ID| Error Message        |
9110e41f4b71Sopenharmony_ci| -------- | ---------------- |
9111e41f4b71Sopenharmony_ci| 401      | Parameter error. |
9112e41f4b71Sopenharmony_ci
9113e41f4b71Sopenharmony_ci**Example**
9114e41f4b71Sopenharmony_ci
9115e41f4b71Sopenharmony_ci```ts
9116e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
9117e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
9118e41f4b71Sopenharmony_ci
9119e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
9120e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
9121e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
9122e41f4b71Sopenharmony_ci  port: 8080
9123e41f4b71Sopenharmony_ci}
9124e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
9125e41f4b71Sopenharmony_ci  key: "xxxx",
9126e41f4b71Sopenharmony_ci  cert: "xxxx",
9127e41f4b71Sopenharmony_ci  ca: ["xxxx"],
9128e41f4b71Sopenharmony_ci  password: "xxxx",
9129e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
9130e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
9131e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
9132e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
9133e41f4b71Sopenharmony_ci}
9134e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
9135e41f4b71Sopenharmony_ci  address: netAddress,
9136e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
9137e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
9138e41f4b71Sopenharmony_ci}
9139e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
9140e41f4b71Sopenharmony_ci  console.log("listen callback success");
9141e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
9142e41f4b71Sopenharmony_ci  console.log("failed" + err);
9143e41f4b71Sopenharmony_ci});
9144e41f4b71Sopenharmony_ci
9145e41f4b71Sopenharmony_citlsServer.on('connect', (client: socket.TLSSocketConnection) => {
9146e41f4b71Sopenharmony_ci  client.on('error', (err: BusinessError) => {
9147e41f4b71Sopenharmony_ci    console.log("on error, err:" + JSON.stringify(err))
9148e41f4b71Sopenharmony_ci  });
9149e41f4b71Sopenharmony_ci});
9150e41f4b71Sopenharmony_ci```
9151e41f4b71Sopenharmony_ci
9152e41f4b71Sopenharmony_ci### off('error')<sup>10+</sup>
9153e41f4b71Sopenharmony_ci
9154e41f4b71Sopenharmony_cioff(type: 'error', callback?: ErrorCallback): void
9155e41f4b71Sopenharmony_ci
9156e41f4b71Sopenharmony_ciUnsubscribes from **error** events of a **TLSSocketConnection** object. This API uses an asynchronous callback to return the result.
9157e41f4b71Sopenharmony_ci
9158e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
9159e41f4b71Sopenharmony_ci
9160e41f4b71Sopenharmony_ci**Parameters**
9161e41f4b71Sopenharmony_ci
9162e41f4b71Sopenharmony_ci| Name  | Type         | Mandatory| Description                                |
9163e41f4b71Sopenharmony_ci| -------- | ------------- | ---- | ------------------------------------ |
9164e41f4b71Sopenharmony_ci| type     | string        | Yes  | Event type.<br/> **error**: error event.|
9165e41f4b71Sopenharmony_ci| callback | ErrorCallback | No  | Callback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.                       |
9166e41f4b71Sopenharmony_ci
9167e41f4b71Sopenharmony_ci**Error codes**
9168e41f4b71Sopenharmony_ci
9169e41f4b71Sopenharmony_ci| ID| Error Message        |
9170e41f4b71Sopenharmony_ci| -------- | ---------------- |
9171e41f4b71Sopenharmony_ci| 401      | Parameter error. |
9172e41f4b71Sopenharmony_ci
9173e41f4b71Sopenharmony_ci**Example**
9174e41f4b71Sopenharmony_ci
9175e41f4b71Sopenharmony_ci```ts
9176e41f4b71Sopenharmony_ciimport { socket } from '@kit.NetworkKit';
9177e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
9178e41f4b71Sopenharmony_ci
9179e41f4b71Sopenharmony_cilet tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance();
9180e41f4b71Sopenharmony_cilet netAddress: socket.NetAddress = {
9181e41f4b71Sopenharmony_ci  address: '192.168.xx.xxx',
9182e41f4b71Sopenharmony_ci  port: 8080
9183e41f4b71Sopenharmony_ci}
9184e41f4b71Sopenharmony_cilet tlsSecureOptions: socket.TLSSecureOptions = {
9185e41f4b71Sopenharmony_ci  key: "xxxx",
9186e41f4b71Sopenharmony_ci  cert: "xxxx",
9187e41f4b71Sopenharmony_ci  ca: ["xxxx"],
9188e41f4b71Sopenharmony_ci  password: "xxxx",
9189e41f4b71Sopenharmony_ci  protocols: socket.Protocol.TLSv12,
9190e41f4b71Sopenharmony_ci  useRemoteCipherPrefer: true,
9191e41f4b71Sopenharmony_ci  signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
9192e41f4b71Sopenharmony_ci  cipherSuite: "AES256-SHA256"
9193e41f4b71Sopenharmony_ci}
9194e41f4b71Sopenharmony_cilet tlsConnectOptions: socket.TLSConnectOptions = {
9195e41f4b71Sopenharmony_ci  address: netAddress,
9196e41f4b71Sopenharmony_ci  secureOptions: tlsSecureOptions,
9197e41f4b71Sopenharmony_ci  ALPNProtocols: ["spdy/1", "http/1.1"]
9198e41f4b71Sopenharmony_ci}
9199e41f4b71Sopenharmony_citlsServer.listen(tlsConnectOptions).then(() => {
9200e41f4b71Sopenharmony_ci  console.log("listen callback success");
9201e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
9202e41f4b71Sopenharmony_ci  console.log("failed" + err);
9203e41f4b71Sopenharmony_ci});
9204e41f4b71Sopenharmony_ci
9205e41f4b71Sopenharmony_cilet callback = (err: BusinessError) => {
9206e41f4b71Sopenharmony_ci  console.log("on error, err:" + JSON.stringify(err));
9207e41f4b71Sopenharmony_ci}
9208e41f4b71Sopenharmony_citlsServer.on('connect', (client: socket.TLSSocketConnection) => {
9209e41f4b71Sopenharmony_ci  client.on('error', callback);
9210e41f4b71Sopenharmony_ci  // You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
9211e41f4b71Sopenharmony_ci  client.off('error', callback);
9212e41f4b71Sopenharmony_ci  client.off('error');
9213e41f4b71Sopenharmony_ci});
9214e41f4b71Sopenharmony_ci```
9215