# @ohos.net.socket (Socket Connection) The **socket** module implements data transfer over TCP, UDP, Web, and TLS socket connections. > **NOTE** > > 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. ## Modules to Import ```ts import socket from '@ohos.net.socket'; ``` ## socket.constructUDPSocketInstance constructUDPSocketInstance(): UDPSocket Creates a **UDPSocket** object. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | --------------------------------- | ---------------------- | | [UDPSocket](#udpsocket) | **UDPSocket** object.| **Example** ```ts import socket from '@ohos.net.socket'; let udp: socket.UDPSocket = socket.constructUDPSocketInstance(); ``` ## UDPSocket Defines a UDP socket connection. Before calling UDPSocket APIs, you need to call [socket.constructUDPSocketInstance](#socketconstructudpsocketinstance) to create a **UDPSocket** object. ### bind bind(address: NetAddress, callback: AsyncCallback\): void Binds 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. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------------------------- | ---- | ------------------------------------------------------ | | address | [NetAddress](#netaddress) | Yes | Destination address. For details, see [NetAddress](#netaddress).| | callback | AsyncCallback\ | Yes | Callback used to return the result. | **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 201 | Permission denied. | **Example** ```ts import socket from '@ohos.net.socket'; import { BusinessError } from '@ohos.base'; let udp: socket.UDPSocket = socket.constructUDPSocketInstance(); let bindAddr: socket.NetAddress = { address: '192.168.xx.xxx', port: 1234 } udp.bind(bindAddr, (err: BusinessError) => { if (err) { console.log('bind fail'); return; } console.log('bind success'); }); ``` ### bind bind(address: NetAddress): Promise\ Binds 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. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ------- | ---------------------------------- | ---- | ------------------------------------------------------ | | address | [NetAddress](#netaddress) | Yes | Destination address. For details, see [NetAddress](#netaddress).| **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 201 | Permission denied. | **Return value** | Type | Description | | -------------- | ----------------------------------------- | | Promise\ | Promise used to return the result.| **Example** ```ts import socket from '@ohos.net.socket'; import { BusinessError } from '@ohos.base'; let udp: socket.UDPSocket = socket.constructUDPSocketInstance(); let bindAddr: socket.NetAddress = { address: '192.168.xx.xxx', port: 8080 } udp.bind(bindAddr).then(() => { console.log('bind success'); }).catch((err: BusinessError) => { console.log('bind fail'); }); ``` ### send send(options: UDPSendOptions, callback: AsyncCallback\): void Sends data over a UDP socket connection. This API uses an asynchronous callback to return the result. Before sending data, call [UDPSocket.bind()](#bind) to bind the IP address and port. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------------------------------- | ---- | ------------------------------------------------------------ | | options | [UDPSendOptions](#udpsendoptions) | Yes | Parameters for sending data over a UDP socket connection. For details, see [UDPSendOptions](#udpsendoptions).| | callback | AsyncCallback\ | Yes | Callback used to return the result. | **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 201 | Permission denied. | **Example** ```ts import socket from '@ohos.net.socket'; import { BusinessError } from '@ohos.base'; let udp: socket.UDPSocket = socket.constructUDPSocketInstance(); let sendOptions: socket.UDPSendOptions = { data: 'Hello, server!', address: { address: '192.168.xx.xxx', port: 8080 } } udp.send(sendOptions, (err: BusinessError) => { if (err) { console.log('send fail'); return; } console.log('send success'); }); ``` ### send send(options: UDPSendOptions): Promise\ Sends data over a UDP socket connection. This API uses a promise to return the result. Before sending data, call [UDPSocket.bind()](#bind) to bind the IP address and port. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ------- | ---------------------------------------- | ---- | ------------------------------------------------------------ | | options | [UDPSendOptions](#udpsendoptions) | Yes | Parameters for sending data over a UDP socket connection. For details, see [UDPSendOptions](#udpsendoptions).| **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 201 | Permission denied. | **Return value** | Type | Description | | -------------- | --------------------------------------------- | | Promise\ | Promise used to return the result.| **Example** ```ts import socket from '@ohos.net.socket'; import { BusinessError } from '@ohos.base'; let udp: socket.UDPSocket = socket.constructUDPSocketInstance(); let sendOptions: socket.UDPSendOptions = { data: 'Hello, server!', address: { address: '192.168.xx.xxx', port: 8080 } } udp.send(sendOptions).then(() => { console.log('send success'); }).catch((err: BusinessError) => { console.log('send fail'); }); ``` ### close close(callback: AsyncCallback\): void Closes a UDP socket connection. This API uses an asynchronous callback to return the result. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | --------------------- | ---- | ---------- | | callback | AsyncCallback\ | Yes | Callback used to return the result.| **Example** ```ts import socket from '@ohos.net.socket'; import { BusinessError } from '@ohos.base'; let udp: socket.UDPSocket = socket.constructUDPSocketInstance(); udp.close((err: BusinessError) => { if (err) { console.log('close fail'); return; } console.log('close success'); }) ``` ### close close(): Promise\ Closes a UDP socket connection. This API uses a promise to return the result. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | -------------- | ----------------------------------------- | | Promise\ | Promise used to return the result.| **Example** ```ts import socket from '@ohos.net.socket'; import { BusinessError } from '@ohos.base'; let udp: socket.UDPSocket = socket.constructUDPSocketInstance(); udp.close().then(() => { console.log('close success'); }).catch((err: BusinessError) => { console.log('close fail'); }); ``` ### getState getState(callback: AsyncCallback\): void Obtains the status of the UDP socket connection. This API uses an asynchronous callback to return the result. > **NOTE** > This API can be called only after **bind** is successfully called. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------------------------------ | ---- | ---------- | | callback | AsyncCallback<[SocketStateBase](#socketstatebase)> | Yes | Callback used to return the result.| **Error codes** | ID| Error Message | | ------- | ----------------------- | | 201 | Permission denied. | **Example** ```ts import socket from '@ohos.net.socket'; import { BusinessError } from '@ohos.base'; let udp: socket.UDPSocket = socket.constructUDPSocketInstance(); let bindAddr: socket.NetAddress = { address: '192.168.xx.xxx', port: 8080 } udp.bind(bindAddr, (err: BusinessError) => { if (err) { console.log('bind fail'); return; } console.log('bind success'); udp.getState((err: BusinessError, data: socket.SocketStateBase) => { if (err) { console.log('getState fail'); return; } console.log('getState success:' + JSON.stringify(data)); }) }) ``` ### getState getState(): Promise\ Obtains the status of the UDP socket connection. This API uses a promise to return the result. > **NOTE** > This API can be called only after **bind** is successfully called. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | ----------------------------------------------- | ----------------------------------------- | | Promise\<[SocketStateBase](#socketstatebase)\> | Promise used to return the result.| **Example** ```ts import socket from '@ohos.net.socket'; import { BusinessError } from '@ohos.base'; let udp: socket.UDPSocket = socket.constructUDPSocketInstance(); let bindAddr: socket.NetAddress = { address: '192.168.xx.xxx', port: 8080 } udp.bind(bindAddr, (err: BusinessError) => { if (err) { console.log('bind fail'); return; } console.log('bind success'); udp.getState().then((data: socket.SocketStateBase) => { console.log('getState success:' + JSON.stringify(data)); }).catch((err: BusinessError) => { console.log('getState fail' + JSON.stringify(err)); }); }); ``` ### setExtraOptions setExtraOptions(options: UDPExtraOptions, callback: AsyncCallback\): void Sets other properties of the UDP socket connection. This API uses an asynchronous callback to return the result. > **NOTE** > This API can be called only after **bind** is successfully called. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------------------------------- | ---- | ------------------------------------------------------------ | | options | [UDPExtraOptions](#udpextraoptions) | Yes | Other properties of the UDP socket connection. For details, see [UDPExtraOptions](#udpextraoptions).| | callback | AsyncCallback\ | Yes | Callback used to return the result. | **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 201 | Permission denied. | **Example** ```ts import socket from '@ohos.net.socket'; import { BusinessError } from '@ohos.base'; let udp: socket.UDPSocket = socket.constructUDPSocketInstance(); let bindAddr: socket.NetAddress = { address: '192.168.xx.xxx', port: 8080 } udp.bind(bindAddr, (err: BusinessError) => { if (err) { console.log('bind fail'); return; } console.log('bind success'); let udpextraoptions: socket.UDPExtraOptions = { receiveBufferSize: 1000, sendBufferSize: 1000, reuseAddress: false, socketTimeout: 6000, broadcast: true } udp.setExtraOptions(udpextraoptions, (err: BusinessError) => { if (err) { console.log('setExtraOptions fail'); return; } console.log('setExtraOptions success'); }) }) ``` ### setExtraOptions setExtraOptions(options: UDPExtraOptions): Promise\ Sets other properties of the UDP socket connection. This API uses a promise to return the result. > **NOTE** > This API can be called only after **bind** is successfully called. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ------- | ---------------------------------------- | ---- | ------------------------------------------------------------ | | options | [UDPExtraOptions](#udpextraoptions) | Yes | Other properties of the UDP socket connection. For details, see [UDPExtraOptions](#udpextraoptions).| **Return value** | Type | Description | | -------------- | --------------------------------------------------- | | Promise\ | Promise used to return the result.| **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 201 | Permission denied. | **Example** ```ts import socket from '@ohos.net.socket'; import { BusinessError } from '@ohos.base'; let udp: socket.UDPSocket = socket.constructUDPSocketInstance(); let bindAddr: socket.NetAddress = { address: '192.168.xx.xxx', port: 8080 } udp.bind(bindAddr, (err: BusinessError) => { if (err) { console.log('bind fail'); return; } console.log('bind success'); let udpextraoptions: socket.UDPExtraOptions = { receiveBufferSize: 1000, sendBufferSize: 1000, reuseAddress: false, socketTimeout: 6000, broadcast: true } udp.setExtraOptions(udpextraoptions).then(() => { console.log('setExtraOptions success'); }).catch((err: BusinessError) => { console.log('setExtraOptions fail'); }); }) ``` ### on('message') on(type: 'message', callback: Callback\): void Subscribes to **message** events of the UDP socket connection. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------------------------------------ | ---- | ----------------------------------------- | | type | string | Yes | Event type.
**message**: message receiving event.| | callback | Callback\<[SocketMessageInfo](#socketmessageinfo11)\> | Yes | Callback used to return the result. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let udp: socket.UDPSocket = socket.constructUDPSocketInstance(); class SocketInfo { message: ArrayBuffer = new ArrayBuffer(1); remoteInfo: socket.SocketRemoteInfo = {} as socket.SocketRemoteInfo; } let messageView = ''; udp.on('message', (value: SocketInfo) => { for (let i: number = 0; i < value.message.byteLength; i++) { let uint8Array = new Uint8Array(value.message) let messages = uint8Array[i] let message = String.fromCharCode(messages); messageView += message; } console.log('on message message: ' + JSON.stringify(messageView)); console.log('remoteInfo: ' + JSON.stringify(value.remoteInfo)); }); ``` ### off('message') off(type: 'message', callback?: Callback\<[SocketMessageInfo]\>): void Unsubscribes from **message** events of the UDP socket connection. This API uses an asynchronous callback to return the result. > **NOTE** > 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------------------------------------ | ---- | ----------------------------------------- | | type | string | Yes | Event type.
**message**: message receiving event.| | callback | Callback<[SocketMessageInfo](#socketmessageinfo11)> | No | Callback used to return the result. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; class SocketInfo { message: ArrayBuffer = new ArrayBuffer(1); remoteInfo: socket.SocketRemoteInfo = {} as socket.SocketRemoteInfo; } let udp: socket.UDPSocket = socket.constructUDPSocketInstance(); let messageView = ''; let callback = (value: SocketInfo) => { for (let i: number = 0; i < value.message.byteLength; i++) { let uint8Array = new Uint8Array(value.message) let messages = uint8Array[i] let message = String.fromCharCode(messages); messageView += message; } console.log('on message message: ' + JSON.stringify(messageView)); console.log('remoteInfo: ' + JSON.stringify(value.remoteInfo)); } udp.on('message', callback); // 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. udp.off('message', callback); udp.off('message'); ``` ### on('listening' | 'close') on(type: 'listening' | 'close', callback: Callback\): void Subscribes to **listening** events or **close** events of the UDP socket connection. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------- | ---- | ------------------------------------------------------------ | | type | string | Yes | Event type.
- **listening**: data packet message event.
- **close**: close event.| | callback | Callback\ | Yes | Callback used to return the result. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let udp: socket.UDPSocket = socket.constructUDPSocketInstance(); udp.on('listening', () => { console.log("on listening success"); }); udp.on('close', () => { console.log("on close success"); }); ``` ### off('listening' | 'close') off(type: 'listening' | 'close', callback?: Callback\): void Unsubscribes from **listening** events or **close** events of the UDP socket connection. This API uses an asynchronous callback to return the result. > **NOTE** > 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------- | ---- | ------------------------------------------------------------ | | type | string | Yes | Event type.
- **listening**: data packet message event.
- **close**: close event.| | callback | Callback\ | No | Callback used to return the result. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let udp: socket.UDPSocket = socket.constructUDPSocketInstance(); let callback1 = () => { console.log("on listening, success"); } udp.on('listening', callback1); // 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. udp.off('listening', callback1); udp.off('listening'); let callback2 = () => { console.log("on close, success"); } udp.on('close', callback2); // 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. udp.off('close', callback2); udp.off('close'); ``` ### on('error') on(type: 'error', callback: ErrorCallback): void Subscribes to **error** events of the UDP socket connection. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------- | ---- | ------------------------------------ | | type | string | Yes | Event type.
**error**: error event.| | callback | ErrorCallback | Yes | Callback used to return the result. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let udp: socket.UDPSocket = socket.constructUDPSocketInstance(); udp.on('error', (err: BusinessError) => { console.log("on error, err:" + JSON.stringify(err)) }); ``` ### off('error') off(type: 'error', callback?: ErrorCallback): void Unsubscribes from **error** events of the UDP socket connection. This API uses an asynchronous callback to return the result. > **NOTE** > 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------- | ---- | ------------------------------------ | | type | string | Yes | Event type.
**error**: error event.| | callback | ErrorCallback | No | Callback used to return the result. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let udp: socket.UDPSocket = socket.constructUDPSocketInstance(); let callback = (err: BusinessError) => { console.log("on error, err:" + JSON.stringify(err)); } udp.on('error', callback); // 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. udp.off('error', callback); udp.off('error'); ``` ## NetAddress Defines the destination address. **System capability**: SystemCapability.Communication.NetStack | Name | Type | Mandatory| Description | | ------- | ------ | ---- | ------------------------------------------------------------ | | address | string | Yes | Bound IP address. | | port | number | No | Port number. The value ranges from **0** to **65535**. If this parameter is not specified, the system randomly allocates a port. | | family | number | No | Network protocol type.
- **1**: IPv4
- **2**: IPv6
The default value is **1**.| ## UDPSendOptions Defines the parameters for sending data over a UDP socket connection. **System capability**: SystemCapability.Communication.NetStack | Name | Type | Mandatory| Description | | ------- | ---------------------------------- | ---- | -------------- | | data | string \| ArrayBuffer | Yes | Data to send. | | address | [NetAddress](#netaddress) | Yes | Destination address.| ## UDPExtraOptions Defines other properties of the UDP socket connection. **System capability**: SystemCapability.Communication.NetStack | Name | Type | Mandatory| Description | | ----------------- | ------- | ---- | -------------------------------- | | broadcast | boolean | No | Whether to send broadcast messages. The default value is **false**. | | receiveBufferSize | number | No | Size of the receive buffer, in bytes. The default value is **0**. | | sendBufferSize | number | No | Size of the send buffer, in bytes. The default value is **0**. | | reuseAddress | boolean | No | Whether to reuse addresses. The default value is **false**. | | socketTimeout | number | No | Timeout duration of the UDP socket connection, in ms. The default value is **0**.| ## SocketStateBase Defines the status of the socket connection. **System capability**: SystemCapability.Communication.NetStack | Name | Type | Mandatory| Description | | ----------- | ------- | ---- | ---------- | | isBound | boolean | Yes | Whether the connection is in the bound state.| | isClose | boolean | Yes | Whether the connection is in the closed state.| | isConnected | boolean | Yes | Whether the connection is in the connected state.| ## SocketRemoteInfo Defines information about the socket connection. **System capability**: SystemCapability.Communication.NetStack | Name | Type | Mandatory| Description | | ------- | ------ | ---- | ------------------------------------------------------------ | | address | string | Yes | Bound IP address. | | family | string | Yes | Network protocol type.
- IPv4
- IPv6
The default value is **IPv4**.| | port | number | Yes | Port number. The value ranges from **0** to **65535**. | | size | number | Yes | Length of the server response message, in bytes. | ## Description of UDP Error Codes The UDP error code mapping is in the format of 2301000 + Linux kernel error code. For details about error codes, see [Socket Error Codes](../errorcodes/errorcode-net-socket.md). ## socket.constructMulticastSocketInstance11+ constructMulticastSocketInstance(): MulticastSocket Creates a **MulticastSocket** object. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | ----------------------------------- | ----------------------------- | | [MulticastSocket](#multicastsocket11) | **MulticastSocket** object.| **Example** ```ts import socket from "@ohos.net.socket"; let multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance(); ``` ## MulticastSocket11+ Defines a **MulticastSocket** connection. Before calling MulticastSocket APIs, you need to call [socket.constructMulticastSocketInstance](#socketconstructmulticastsocketinstance11) to create a **MulticastSocket** object. ### addMembership11+ addMembership(multicastAddress: NetAddress, callback: AsyncCallback\): void; Adds a member to a multicast group. This API uses an asynchronous callback to return the result. > **NOTE** > The IP addresses used for multicast belong to a specific range, for example, 224.0.0.0 to 239.255.255.255. > 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. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ----------------- | ----------------------------- | ---- | ----------------------------------------- | | multicastAddress | [NetAddress](#netaddress) | Yes | Destination address. For details, see [NetAddress](#netaddress).| | callback | AsyncCallback\ | Yes | Callback used to return the result. | **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 201 | Permission denied. | | 2301022 | Invalid argument. | | 2301088 | Not a socket. | | 2301098 | Address in use. | **Example** ```ts import socket from "@ohos.net.socket"; let multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance(); let addr: socket.NetAddress = { address: '239.255.0.1', port: 8080 } multicast.addMembership(addr, (err) => { if (err) { console.log('add membership fail, err: ' + JSON.stringify(err)); return; } console.log('add membership success'); }) ``` ### addMembership11+ addMembership(multicastAddress: NetAddress): Promise\; Adds a member to a multicast group. This API uses a promise to return the result. > **NOTE** > The IP addresses used for multicast belong to a specific range, for example, 224.0.0.0 to 239.255.255.255. > 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. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ----------------- | ----------------------------- | ---- | -------------------------------------------- | | multicastAddress | [NetAddress](#netaddress) | Yes | Destination address. For details, see [NetAddress](#netaddress).| **Return value** | Type | Description | | -------------- | ----------------------------------------------- | | Promise\ | Promise used to return the result.| **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 201 | Permission denied. | | 2301088 | Not a socket. | | 2301098 | Address in use. | **Example** ```ts import socket from "@ohos.net.socket"; let multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance(); let addr: socket.NetAddress = { address: '239.255.0.1', port: 8080 } multicast.addMembership(addr).then(() => { console.log('addMembership success'); }).catch((err) => { console.log('addMembership fail'); }); ``` ### dropMembership11+ dropMembership(multicastAddress: NetAddress, callback: AsyncCallback\): void; Drops a member from a multicast group. This API uses an asynchronous callback to return the result. > **NOTE** > The IP addresses used for multicast belong to a specific range, for example, 224.0.0.0 to 239.255.255.255. > You can drop only a member that has been added to a multicast group by using [addMembership](#addmembership11). **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ----------------- | ----------------------------- | ---- | ------------------------------------------- | | multicastAddress | [NetAddress](#netaddress) | Yes | Destination address. For details, see [NetAddress](#netaddress). | | callback | AsyncCallback\ | Yes | Callback used to return the result. | **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 201 | Permission denied. | | 2301088 | Not a socket. | | 2301098 | Address in use. | **Example** ```ts import socket from "@ohos.net.socket"; let multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance(); let addr: socket.NetAddress = { address: '239.255.0.1', port: 8080 } multicast.dropMembership(addr, (err) => { if (err) { console.log('drop membership fail, err: ' + JSON.stringify(err)); return; } console.log('drop membership success'); }) ``` ### dropMembership11+ dropMembership(multicastAddress: NetAddress): Promise\; Drops a member from a multicast group. This API uses a promise to return the result. > **NOTE** > The IP addresses used for multicast belong to a specific range, for example, 224.0.0.0 to 239.255.255.255. > You can drop only a member that has been added to a multicast group by using [addMembership](#addmembership11). **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ----------------- | ------------------------------------- | ---- | -------------------------------------------- | | multicastAddress | [NetAddress](#netaddress) | Yes | Destination address. For details, see [NetAddress](#netaddress). | **Return value** | Type | Description | | -------------- | ----------------------------------------------- | | Promise\ | Promise used to return the result.| **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 201 | Permission denied. | | 2301088 | Not a socket. | | 2301098 | Address in use. | **Example** ```ts import socket from "@ohos.net.socket"; let multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance(); let addr: socket.NetAddress = { address: '239.255.0.1', port: 8080 } multicast.dropMembership(addr).then(() => { console.log('drop membership success'); }).catch((err) => { console.log('drop membership fail'); }); ``` ### setMulticastTTL11+ setMulticastTTL(ttl: number, callback: AsyncCallback\): void; Sets the time to live (TTL) for multicast packets. This API uses an asynchronous callback to return the result. > **NOTE** > TTL is used to limit the maximum number of router hops for packet transmission on a network. > The value ranges from 0 to 255. The default value is **1**. > 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. > This API is effective only after [addMembership](#addmembership11) is called. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ------------- | --------------------- | ---- | ----------------------------- | | ttl | number | Yes | TTL value. The value is of the number type.| | callback | AsyncCallback\ | Yes | Callback used to return the result. | **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 2301022 | Invalid argument. | | 2301088 | Not a socket. | **Example** ```ts import socket from "@ohos.net.socket"; let multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance(); let ttl = 8 multicast.setMulticastTTL(ttl, (err) => { if (err) { console.log('set ttl fail, err: ' + JSON.stringify(err)); return; } console.log('set ttl success'); }) ``` ### setMulticastTTL11+ setMulticastTTL(ttl: number): Promise\; Sets the TTL for multicast packets. This API uses a promise to return the result. > **NOTE** > TTL is used to limit the maximum number of router hops for packet transmission on a network. > The value ranges from 0 to 255. The default value is **1**. > 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. > This API is effective only after [addMembership](#addmembership11) is called. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ------------- | ---------------------- | ---- | ------------------------------ | | ttl | number | Yes | TTL value. The value is of the number type.| **Return value** | Type | Description | | -------------- | ---------------------------------------------- | | Promise\ | Promise used to return the result.| **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 2301022 | Invalid argument. | | 2301088 | Not a socket. | **Example** ```ts import socket from "@ohos.net.socket"; let multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance(); multicast.setMulticastTTL(8).then(() => { console.log('set ttl success'); }).catch((err) => { console.log('set ttl failed'); }); ``` ### getMulticastTTL11+ getMulticastTTL(callback: AsyncCallback\): void; Obtains the TTL for multicast packets. This API uses an asynchronous callback to return the result. > **NOTE** > TTL is used to limit the maximum number of router hops for packet transmission on a network. > The value ranges from 0 to 255. The default value is **1**. > 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. > This API is effective only after [addMembership](#addmembership11) is called. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ------------- | ----------------------- | ---- | --------------------------- | | callback | AsyncCallback\ | Yes | Callback used to return the result. | **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 2301088 | Not a socket. | **Example** ```ts import socket from "@ohos.net.socket"; let multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance(); multicast.getMulticastTTL((err, value) => { if (err) { console.log('set ttl fail, err: ' + JSON.stringify(err)); return; } console.log('set ttl success, value: ' + JSON.stringify(value)); }) ``` ### getMulticastTTL11+ getMulticastTTL(): Promise\; Obtains the TTL for multicast packets. This API uses a promise to return the result. > **NOTE** > TTL is used to limit the maximum number of router hops for packet transmission on a network. > The value ranges from 0 to 255. The default value is **1**. > 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. > This API is effective only after [addMembership](#addmembership11) is called. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | ---------------- | --------------------------- | | Promise\ | Promise used to return the result.| **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 2301088 | Not a socket. | **Example** ```ts import socket from "@ohos.net.socket"; let multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance(); multicast.getMulticastTTL().then((value) => { console.log('ttl: ', JSON.stringify(value)); }).catch((err) => { console.log('set ttl failed'); }); ``` ### setLoopbackMode11+ setLoopbackMode(flag: boolean, callback: AsyncCallback\): void; Sets the loopback mode flag for multicast communication. This API uses an asynchronous callback to return the result. > **NOTE** > Use this API to enable or disable the loopback mode. By default, the loopback mode is enabled. > The value **true** indicates that the host is allowed to receive the multicast packets sent by itself, and the value **false** indicates the opposite. > This API is effective only after [addMembership](#addmembership11) is called. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ------------- | --------------------- | ---- | ---------------------------- | | flag | boolean | Yes | Loopback mode flag. The value is of the Boolean type. | | callback | AsyncCallback\ | Yes | Callback used to return the result. | **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 2301088 | Not a socket. | **Example** ```ts import socket from "@ohos.net.socket"; let multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance(); multicast.setLoopbackMode(false, (err) => { if (err) { console.log('set loopback mode fail, err: ' + JSON.stringify(err)); return; } console.log('set loopback mode success'); }) ``` ### setLoopbackMode11+ setLoopbackMode(flag: boolean): Promise\; Sets the loopback mode flag for multicast communication. This API uses an asynchronous callback to return the result. > **NOTE** > Use this API to enable or disable the loopback mode. By default, the loopback mode is enabled. > The value **true** indicates that the host is allowed to receive the multicast packets sent by itself, and the value **false** indicates the opposite. > This API is effective only after [addMembership](#addmembership11) is called. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ------------- | ---------------------- | ---- | -------------------------------- | | flag | boolean | Yes | Loopback mode flag. The value is of the Boolean type.| **Return value** | Type | Description | | -------------- | ---------------------------------------------- | | Promise\ | Promise used to return the result.| **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 2301088 | Not a socket. | **Example** ```ts import socket from "@ohos.net.socket"; let multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance(); multicast.setLoopbackMode(false).then(() => { console.log('set loopback mode success'); }).catch((err) => { console.log('set loopback mode failed'); }); ``` ### getLoopbackMode11+ getLoopbackMode(callback: AsyncCallback\): void; Obtains the loopback mode flag for multicast communication. This API uses a promise to return the result. > **NOTE** > Use this API to check whether the loopback mode is enabled. > 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. > This API is effective only after [addMembership](#addmembership11) is called. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ------------- | ----------------------- | ---- | --------------------------- | | callback | AsyncCallback\ | Yes | Callback used to return the result. | **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 2301088 | Not a socket. | **Example** ```ts import socket from "@ohos.net.socket"; let multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance(); multicast.getLoopbackMode((err, value) => { if (err) { console.log('get loopback mode fail, err: ' + JSON.stringify(err)); return; } console.log('get loopback mode success, value: ' + JSON.stringify(value)); }) ``` ### getLoopbackMode11+ getLoopbackMode(): Promise\; Obtains the loopback mode flag for multicast communication. This API uses a promise to return the result. > **NOTE** > Use this API to check whether the loopback mode is enabled. > 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. > This API is effective only after [addMembership](#addmembership11) is called. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | ---------------- | --------------------------- | | Promise\ | Promise used to return the result.| **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 2301088 | Not a socket. | **Example** ```ts import socket from "@ohos.net.socket"; let multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance(); multicast.getLoopbackMode().then((value) => { console.log('loopback mode: ', JSON.stringify(value)); }).catch((err) => { console.log('get loopback mode failed'); }); ``` ### send7+ send(options: UDPSendOptions, callback: AsyncCallback\): void Sends data in multicast communication. This API uses an asynchronous callback to return the result. Before sending data, call [addMembership](#addmembership11) to add a member to the multicast group. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | --------------------------------- | ---- | ------------------------------------------------------------ | | options | [UDPSendOptions](#udpsendoptions) | Yes | Parameters for sending data over a UDP socket connection. For details, see [UDPSendOptions](#udpsendoptions).| | callback | AsyncCallback\ | Yes | Callback used to return the result. | **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 201 | Permission denied. | **Example** ```ts import socket from '@ohos.net.socket'; let multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance(); let sendOptions: socket.UDPSendOptions = { data: 'Hello, server!', address: { address: '239.255.0.1', port: 8080 } } multicast.send(sendOptions, (err) => { if (err) { console.log('send fail: ' + JSON.stringify(err)); return; } console.log('send success'); }); ``` ### send7+ send(options: UDPSendOptions): Promise\ Sends data in multicast communication. This API uses a promise to return the result. Before sending data, call [addMembership](#addmembership11) to add a member to the multicast group. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ------- | ---------------------------------------- | ---- | ------------------------------------------------------------ | | options | [UDPSendOptions](#udpsendoptions) | Yes | Parameters for sending data over a UDP socket connection. For details, see [UDPSendOptions](#udpsendoptions).| **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 201 | Permission denied. | **Return value** | Type | Description | | :-------------- | :------------------------------- | | Promise\ | Promise used to return the result.| **Example** ```ts import socket from '@ohos.net.socket'; let multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance(); let sendOptions: socket.UDPSendOptions = { data: 'Hello, server!', address: { address: '239.255.0.1', port: 8080 } } multicast.send(sendOptions).then(() => { console.log('send success'); }).catch((err) => { console.log('send fail, ' + JSON.stringify(err)); }); ``` ### on('message')7+ on(type: 'message', callback: Callback\): void Subscribes to **message** events of a **MulticastSocket** object. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------------------------------------------------------------- | ---- | ----------------------------------- | | type | string | Yes | Event type.
**message**: message receiving event.| | callback | Callback\<[SocketMessageInfo](#socketmessageinfo11)\> | Yes | Callback used to return the result. | **Example** ```ts import socket from "@ohos.net.socket" let multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance() multicast.on('message', (data) => { console.info ('Received data:' + JSON.stringify (data)) const uintArray = new Uint8Array(data.message) let str = '' for (let i = 0; i < uintArray.length; ++i) { str += String.fromCharCode(uintArray[i]) } console.info(str) }) ``` ### off('message')7+ off(type: 'message', callback?: Callback\): void Unsubscribes to **message** events of a **MulticastSocket** object. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ----------------------------------------------------------------------------------- | ---- | ----------------------------------- | | type | string | Yes | Event type.
**message**: message receiving event.| | callback | Callback<[SocketMessageInfo](#socketmessageinfo11)> | No | Callback used to return the result. | **Example** ```ts import socket from "@ohos.net.socket" let multicast: socket.MulticastSocket = socket.constructMulticastSocketInstance() multicast.on('message', (data) => { console.info ('Received data:' + JSON.stringify (data)) const uintArray = new Uint8Array(data.message) let str = '' for (let i = 0; i < uintArray.length; ++i) { str += String.fromCharCode(uintArray[i]) } console.info(str) }) multicast.off('message') ``` ## socket.constructTCPSocketInstance7+ constructTCPSocketInstance(): TCPSocket Creates a **TCPSocket** object. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | --------------------------------- | ---------------------- | | [TCPSocket](#tcpsocket) | **TCPSocket** object.| **Example** ```ts import socket from "@ohos.net.socket"; let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); ``` ## TCPSocket Defines a TCP socket connection. Before calling TCPSocket APIs, you need to call [socket.constructTCPSocketInstance](#socketconstructtcpsocketinstance7) to create a **TCPSocket** object. ### bind bind(address: NetAddress, callback: AsyncCallback\): void Binds 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. > **NOTE** > If the bind operation fails due to a port conflict, the system will randomly allocate a port number. > 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. > 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. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------------------------- | ---- | ------------------------------------------------------ | | address | [NetAddress](#netaddress) | Yes | Destination address. For details, see [NetAddress](#netaddress).| | callback | AsyncCallback\ | Yes | Callback used to return the result. | **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 201 | Permission denied. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); let bindAddr: socket.NetAddress = { address: '192.168.xx.xxx', port: 8080 } tcp.bind(bindAddr, (err: BusinessError) => { if (err) { console.log('bind fail'); return; } console.log('bind success'); }) ``` ### bind bind(address: NetAddress): Promise\ Binds 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. > **NOTE** > If the bind operation fails due to a port conflict, the system will randomly allocate a port number. > 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. > 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. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ------- | ---------------------------------- | ---- | ------------------------------------------------------ | | address | [NetAddress](#netaddress) | Yes | Destination address. For details, see [NetAddress](#netaddress).| **Return value** | Type | Description | | --------------- | ------------------------------------------------------- | | Promise\ | Promise used to return the result.| **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 201 | Permission denied. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); let bindAddr: socket.NetAddress = { address: '192.168.xx.xxx', port: 8080 } tcp.bind(bindAddr).then(() => { console.log('bind success'); }).catch((err: BusinessError) => { console.log('bind fail'); }); ``` ### connect connect(options: TCPConnectOptions, callback: AsyncCallback\): void Sets up a connection to the specified IP address and port number. This API uses an asynchronous callback to return the result. > **NOTE** > This API allows you to connect to the TCP server without first executing **tcp.bind**. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------------------------------- | ---- | ------------------------------------------------------------ | | options | [TCPConnectOptions](#tcpconnectoptions) | Yes | TCP socket connection parameters. For details, see [TCPConnectOptions](#tcpconnectoptions).| | callback | AsyncCallback\ | Yes | Callback used to return the result. | **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 201 | Permission denied. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); let tcpconnectoptions: socket.TCPConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, timeout: 6000 } tcp.connect(tcpconnectoptions, (err: BusinessError) => { if (err) { console.log('connect fail'); return; } console.log('connect success'); }) ``` ### connect connect(options: TCPConnectOptions): Promise\ Sets up a connection to the specified IP address and port number. This API uses a promise to return the result. > **NOTE** > This API allows you to connect to the TCP server without first executing **tcp.bind**. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ------- | ---------------------------------------- | ---- | ------------------------------------------------------------ | | options | [TCPConnectOptions](#tcpconnectoptions) | Yes | TCP socket connection parameters. For details, see [TCPConnectOptions](#tcpconnectoptions).| **Return value** | Type | Description | | -------------- | --------------------------------------------------------- | | Promise\ | Promise used to return the result.| **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 201 | Permission denied. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); let tcpconnectoptions: socket.TCPConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, timeout: 6000 } tcp.connect(tcpconnectoptions).then(() => { console.log('connect success') }).catch((err: BusinessError) => { console.log('connect fail'); }); ``` ### send send(options: TCPSendOptions, callback: AsyncCallback\): void Sends data over a TCP socket connection. This API uses an asynchronous callback to return the result. > **NOTE** > This API can be called only after **connect** is successfully called. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | | options | [TCPSendOptions](#tcpsendoptions) | Yes | Parameters for sending data over a TCP socket connection. For details, see [TCPSendOptions](#tcpsendoptions).| | callback | AsyncCallback\ | Yes | Callback used to return the result. | **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 201 | Permission denied. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); let tcpconnectoptions: socket.TCPConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, timeout: 6000 } tcp.connect(tcpconnectoptions, () => { console.log('connect success'); let tcpSendOptions: socket.TCPSendOptions = { data: 'Hello, server!' } tcp.send(tcpSendOptions, (err: BusinessError) => { if (err) { console.log('send fail'); return; } console.log('send success'); }) }) ``` ### send send(options: TCPSendOptions): Promise\ Sends data over a TCP socket connection. This API uses a promise to return the result. > **NOTE** > This API can be called only after **connect** is successfully called. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ------- | --------------------------------------- | ---- | ------------------------------------------------------------ | | options | [TCPSendOptions](#tcpsendoptions) | Yes | Parameters for sending data over a TCP socket connection. For details, see [TCPSendOptions](#tcpsendoptions).| **Return value** | Type | Description | | -------------- | ------------------------------------------------- | | Promise\ | Promise used to return the result.| **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 201 | Permission denied. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); let tcpconnectoptions: socket.TCPConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, timeout: 6000 } tcp.connect(tcpconnectoptions, () => { console.log('connect success'); let tcpSendOptions: socket.TCPSendOptions = { data: 'Hello, server!' } tcp.send(tcpSendOptions).then(() => { console.log('send success'); }).catch((err: BusinessError) => { console.log('send fail'); }); }) ``` ### close close(callback: AsyncCallback\): void Closes a TCP socket connection. This API uses an asynchronous callback to return the result. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | --------------------- | ---- | ---------- | | callback | AsyncCallback\ | Yes | Callback used to return the result.| **Error codes** | ID| Error Message | | ------- | ----------------------- | | 201 | Permission denied. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); tcp.close((err: BusinessError) => { if (err) { console.log('close fail'); return; } console.log('close success'); }) ``` ### close close(): Promise\ Closes a TCP socket connection. This API uses a promise to return the result. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | -------------- | ----------------------------------------- | | Promise\ | Promise used to return the result.| **Error codes** | ID| Error Message | | ------- | ----------------------- | | 201 | Permission denied. | **Example** ```ts import socket from "@ohos.net.socket"; let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); tcp.close().then(() => { console.log('close success'); }).catch((err: BusinessError) => { console.log('close fail'); }); ``` ### getRemoteAddress getRemoteAddress(callback: AsyncCallback\): void Obtains the remote address of a socket connection. This API uses an asynchronous callback to return the result. > **NOTE** > This API can be called only after **connect** is successfully called. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------------------------- | ---- | ---------- | | callback | AsyncCallback<[NetAddress](#netaddress)> | Yes | Callback used to return the result.| **Error codes** | ID| Error Message | | ------- | ----------------------- | | 201 | Permission denied. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); let tcpconnectoptions: socket.TCPConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, timeout: 6000 } tcp.connect(tcpconnectoptions, () => { console.log('connect success'); tcp.getRemoteAddress((err: BusinessError, data: socket.NetAddress) => { if (err) { console.log('getRemoteAddressfail'); return; } console.log('getRemoteAddresssuccess:' + JSON.stringify(data)); }) }); ``` ### getRemoteAddress getRemoteAddress(): Promise\ Obtains the remote address of a socket connection. This API uses a promise to return the result. > **NOTE** > This API can be called only after **connect** is successfully called. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | ------------------------------------------ | ------------------------------------------ | | Promise<[NetAddress](#netaddress)> | Promise used to return the result.| **Error codes** | ID| Error Message | | ------- | ----------------------- | | 201 | Permission denied. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); let tcpconnectoptions: socket.TCPConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, timeout: 6000 } tcp.connect(tcpconnectoptions).then(() => { console.log('connect success'); tcp.getRemoteAddress().then(() => { console.log('getRemoteAddress success'); }).catch((err: BusinessError) => { console.log('getRemoteAddressfail'); }); }).catch((err: BusinessError) => { console.log('connect fail'); }); ``` ### getState getState(callback: AsyncCallback\): void Obtains the status of the TCP socket connection. This API uses an asynchronous callback to return the result. > **NOTE** > This API can be called only after **bind** or **connect** is successfully called. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------------------------------ | ---- | ---------- | | callback | AsyncCallback<[SocketStateBase](#socketstatebase)> | Yes | Callback used to return the result.| **Error codes** | ID| Error Message | | ------- | ----------------------- | | 201 | Permission denied. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); let tcpconnectoptions: socket.TCPConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, timeout: 6000 } tcp.connect(tcpconnectoptions, () => { console.log('connect success'); tcp.getState((err: BusinessError, data: socket.SocketStateBase) => { if (err) { console.log('getState fail'); return; } console.log('getState success:' + JSON.stringify(data)); }); }); ``` ### getState getState(): Promise\ Obtains the status of the TCP socket connection. This API uses a promise to return the result. > **NOTE** > This API can be called only after **bind** or **connect** is successfully called. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | ----------------------------------------------- | ----------------------------------------- | | Promise<[SocketStateBase](#socketstatebase)> | Promise used to return the result.| **Error codes** | ID| Error Message | | ------- | ----------------------- | | 201 | Permission denied. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); let tcpconnectoptions: socket.TCPConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, timeout: 6000 } tcp.connect(tcpconnectoptions).then(() => { console.log('connect success'); tcp.getState().then(() => { console.log('getState success'); }).catch((err: BusinessError) => { console.log('getState fail'); }); }).catch((err: BusinessError) => { console.log('connect fail'); }); ``` ### getSocketFd10+ getSocketFd(callback: AsyncCallback\): void Obtains the file descriptor of the **TCPSocket** object. This API uses an asynchronous callback to return the result. > **NOTE** > This API can be called only after **bind** or **connect** is successfully called. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------------------------------ | ---- | ---------- | | callback | AsyncCallback\ | Yes | Callback used to return the result. If the operation is successful, the file descriptor of the socket is returned. Otherwise, **undefined** is returned.| **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); let bindAddr: socket.NetAddress = { address: '0.0.0.0' } tcp.bind(bindAddr) let tcpconnectoptions: socket.TCPConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, timeout: 6000 } tcp.connect(tcpconnectoptions) tcp.getSocketFd((err: BusinessError, data: number) => { console.info("getSocketFd failed: " + err); console.info("tunenlfd: " + data); }) ``` ### getSocketFd10+ getSocketFd(): Promise\ Obtains the file descriptor of the **TCPSocket** object. This API uses a promise to return the result. > **NOTE** > This API can be called only after **bind** or **connect** is successfully called. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | ----------------------------------------------- | ----------------------------------------- | | Promise\ | Promise used to return the result.| **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); let bindAddr: socket.NetAddress = { address: '0.0.0.0' } tcp.bind(bindAddr) let tcpconnectoptions: socket.TCPConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, timeout: 6000 } tcp.connect(tcpconnectoptions) tcp.getSocketFd().then((data: number) => { console.info("tunenlfd: " + data); }) ``` ### setExtraOptions setExtraOptions(options: TCPExtraOptions, callback: AsyncCallback\): void Sets other properties of the TCP socket connection. This API uses an asynchronous callback to return the result. > **NOTE** > This API can be called only after **bind** or **connect** is successfully called. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | | options | [TCPExtraOptions](#tcpextraoptions) | Yes | Other properties of the TCP socket connection. For details, see [TCPExtraOptions](#tcpextraoptions).| | callback | AsyncCallback\ | Yes | Callback used to return the result. | **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 201 | Permission denied. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); let tcpconnectoptions: socket.TCPConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, timeout: 6000 } tcp.connect(tcpconnectoptions, () => { console.log('connect success'); let tcpExtraOptions: socket.TCPExtraOptions = { keepAlive: true, OOBInline: true, TCPNoDelay: true, socketLinger: { on: true, linger: 10 }, receiveBufferSize: 1000, sendBufferSize: 1000, reuseAddress: true, socketTimeout: 3000 } tcp.setExtraOptions(tcpExtraOptions, (err: BusinessError) => { if (err) { console.log('setExtraOptions fail'); return; } console.log('setExtraOptions success'); }); }); ``` ### setExtraOptions setExtraOptions(options: TCPExtraOptions): Promise\ Sets other properties of the TCP socket connection. This API uses a promise to return the result. > **NOTE** > This API can be called only after **bind** or **connect** is successfully called. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | | options | [TCPExtraOptions](#tcpextraoptions) | Yes | Other properties of the TCP socket connection. For details, see [TCPExtraOptions](#tcpextraoptions).| **Return value** | Type | Description | | -------------- | --------------------------------------------------- | | Promise\ | Promise used to return the result.| **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 201 | Permission denied. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); let tcpconnectoptions: socket.TCPConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, timeout: 6000 } tcp.connect(tcpconnectoptions, () => { console.log('connect success'); let tcpExtraOptions: socket.TCPExtraOptions = { keepAlive: true, OOBInline: true, TCPNoDelay: true, socketLinger: { on: true, linger: 10 }, receiveBufferSize: 1000, sendBufferSize: 1000, reuseAddress: true, socketTimeout: 3000 } tcp.setExtraOptions(tcpExtraOptions).then(() => { console.log('setExtraOptions success'); }).catch((err: BusinessError) => { console.log('setExtraOptions fail'); }); }); ``` ### on('message') on(type: 'message', callback: Callback): void Subscribes to **message** events of the TCP socket connection. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------------------------------------ | ---- | ----------------------------------------- | | type | string | Yes | Event type.
**message**: message receiving event.| | callback | Callback<[SocketMessageInfo](#socketmessageinfo11)> | Yes | Callback used to return the result. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); class SocketInfo { message: ArrayBuffer = new ArrayBuffer(1); remoteInfo: socket.SocketRemoteInfo = {} as socket.SocketRemoteInfo; } let messageView = ''; tcp.on('message', (value: SocketInfo) => { for (let i: number = 0; i < value.message.byteLength; i++) { let uint8Array = new Uint8Array(value.message) let messages = uint8Array[i] let message = String.fromCharCode(messages); messageView += message; } console.log('on message message: ' + JSON.stringify(messageView)); console.log('remoteInfo: ' + JSON.stringify(value.remoteInfo)); }); ``` ### off('message') off(type: 'message', callback?: Callback): void Unsubscribes from **message** events of the TCP socket connection. This API uses an asynchronous callback to return the result. > **NOTE** > 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------------------------------------ | ---- | ----------------------------------------- | | type | string | Yes | Event type.
**message**: message receiving event.| | callback | Callback<[SocketMessageInfo](#socketmessageinfo11)> | No | Callback used to return the result. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); class SocketInfo { message: ArrayBuffer = new ArrayBuffer(1); remoteInfo: socket.SocketRemoteInfo = {} as socket.SocketRemoteInfo; } let messageView = ''; let callback = (value: SocketInfo) => { for (let i: number = 0; i < value.message.byteLength; i++) { let uint8Array = new Uint8Array(value.message) let messages = uint8Array[i] let message = String.fromCharCode(messages); messageView += message; } console.log('on message message: ' + JSON.stringify(messageView)); console.log('remoteInfo: ' + JSON.stringify(value.remoteInfo)); } tcp.on('message', callback); // 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. tcp.off('message', callback); tcp.off('message'); ``` ### on('connect' | 'close') on(type: 'connect' | 'close', callback: Callback\): void Subscribes to connection or close events of the TCP socket connection. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------- | ---- | ------------------------------------------------------------ | | type | string | Yes | Event type.
- **connect**: connection event.
- **close**: close event.| | callback | Callback\ | Yes | Callback used to return the result. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); tcp.on('connect', () => { console.log("on connect success") }); tcp.on('close', () => { console.log("on close success") }); ``` ### off('connect' | 'close') off(type: 'connect' | 'close', callback?: Callback\): void Unsubscribes from connection or close events of the TCP socket connection. This API uses an asynchronous callback to return the result. > **NOTE** > 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------- | ---- | ------------------------------------------------------------ | | type | string | Yes | Event type.
- **connect**: connection event.
- **close**: close event.| | callback | Callback\ | No | Callback used to return the result. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); let callback1 = () => { console.log("on connect success"); } tcp.on('connect', callback1); // 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. tcp.off('connect', callback1); tcp.off('connect'); let callback2 = () => { console.log("on close success"); } tcp.on('close', callback2); // 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. tcp.off('close', callback2); tcp.off('close'); ``` ### on('error') on(type: 'error', callback: ErrorCallback): void Subscribes to **error** events of the TCP socket connection. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------- | ---- | ------------------------------------ | | type | string | Yes | Event type.
**error**: error event.| | callback | ErrorCallback | Yes | Callback used to return the result. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); tcp.on('error', (err: BusinessError) => { console.log("on error, err:" + JSON.stringify(err)) }); ``` ### off('error') off(type: 'error', callback?: ErrorCallback): void Unsubscribes from **error** events of the TCP socket connection. This API uses an asynchronous callback to return the result. > **NOTE** > 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------- | ---- | ------------------------------------ | | type | string | Yes | Event type.
**error**: error event.| | callback | ErrorCallback | No | Callback used to return the result. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); let callback = (err: BusinessError) => { console.log("on error, err:" + JSON.stringify(err)); } tcp.on('error', callback); // 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. tcp.off('error', callback); tcp.off('error'); ``` ## TCPConnectOptions Defines TCP socket connection parameters. **System capability**: SystemCapability.Communication.NetStack | Name | Type | Mandatory| Description | | ------- | ---------------------------------- | ---- | -------------------------- | | address | [NetAddress](#netaddress) | Yes | Bound IP address and port number. | | timeout | number | No | Timeout duration of the TCP socket connection, in ms.| ## TCPSendOptions Defines the parameters for sending data over a TCP socket connection. **System capability**: SystemCapability.Communication.NetStack | Name | Type | Mandatory| Description | | -------- | ------ | ---- | ------------------------------------------------------------ | | data | string\| ArrayBuffer | Yes | Data to send. | | 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**.| ## TCPExtraOptions Defines other properties of the TCP socket connection. **System capability**: SystemCapability.Communication.NetStack | Name | Type | Mandatory| Description | | ----------------- | ------- | ---- | ------------------------------------------------------------ | | keepAlive | boolean | No | Whether to keep the connection alive. The default value is **false**. | | OOBInline | boolean | No | Whether to enable OOBInline. The default value is **false**. | | TCPNoDelay | boolean | No | Whether to enable no-delay on the TCP socket connection. The default value is **false**. | | socketLinger | Object | Yes | Socket linger.
- **on**: whether to enable socket linger. The value true means to enable socket linger and false means the opposite.
- **linger**: linger time, in ms. The value ranges from **0** to **65535**.
Specify this parameter only when **on** is set to **true**.| | receiveBufferSize | number | No | Size of the receive buffer, in bytes. The default value is **0**. | | sendBufferSize | number | No | Size of the send buffer, in bytes. The default value is **0**. | | reuseAddress | boolean | No | Whether to reuse addresses. The default value is **false**. | | socketTimeout | number | No | Timeout duration of the UDP socket connection, in ms. The default value is **0**. | ## socket.constructTCPSocketServerInstance10+ constructTCPSocketServerInstance(): TCPSocketServer Creates a **TCPSocketServer** object. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | ---------------------------------- | ---------------------------- | | [TCPSocketServer](#tcpsocketserver10) | **TCPSocketServer** object.| **Example** ```ts import socket from "@ohos.net.socket"; let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); ``` ## TCPSocketServer10+ Defines a TCP socket server connection. Before calling TCPSocketServer APIs, you need to call [socket.constructTCPSocketServerInstance](#socketconstructtcpsocketserverinstance10) to create a **TCPSocketServer** object. ### listen10+ listen(address: NetAddress, callback: AsyncCallback\): void Binds 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. > **NOTE**
> 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. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------- | ---- | --------------------------------------------- | | address | [NetAddress](#netaddress) | Yes | Destination address.| | callback | AsyncCallback\ | Yes | Callback used to return the result. | **Error codes** | ID| Error Message | | -------- | ------------------------------------------- | | 401 | Parameter error. | | 201 | Permission denied. | | 2300002 | System internal error. | | 2303109 | Bad file number. | | 2303111 | Resource temporarily unavailable try again. | | 2303198 | Address already in use. | | 2303199 | Cannot assign requested address. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); let listenAddr: socket.NetAddress = { address: '192.168.xx.xxx', port: 8080, family: 1 } tcpServer.listen(listenAddr, (err: BusinessError) => { if (err) { console.log("listen fail"); return; } console.log("listen success"); }) ``` ### listen10+ listen(address: NetAddress): Promise\ Binds 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. > **NOTE**
> 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. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ------- | ------------------------- | ---- | --------------------------------------------- | | address | [NetAddress](#netaddress) | Yes | Destination address.| **Return value** | Type | Description | | -------------- | ----------------------------------------------------------- | | Promise\ | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | -------- | ------------------------------------------- | | 401 | Parameter error. | | 201 | Permission denied. | | 2300002 | System internal error. | | 2303109 | Bad file number. | | 2303111 | Resource temporarily unavailable try again. | | 2303198 | Address already in use. | | 2303199 | Cannot assign requested address. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); let listenAddr: socket.NetAddress = { address: '192.168.xx.xxx', port: 8080, family: 1 } tcpServer.listen(listenAddr).then(() => { console.log('listen success'); }).catch((err: BusinessError) => { console.log('listen fail'); }); ``` ### getState10+ getState(callback: AsyncCallback\): void Obtains the status of a TCP socket server connection. This API uses an asynchronous callback to return the result. > **NOTE** > This API can be called only after **listen** is successfully called. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | -------------------------------------------------- | ---- | ---------- | | callback | AsyncCallback<[SocketStateBase](#socketstatebase)> | Yes | Callback used to return the result.| **Error codes** | ID| Error Message | | -------- | ------------------------------- | | 401 | Parameter error. | | 201 | Permission denied. | | 2300002 | System internal error. | | 2303188 | Socket operation on non-socket. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); let listenAddr: socket.NetAddress = { address: '192.168.xx.xxx', port: 8080, family: 1 } tcpServer.listen(listenAddr, (err: BusinessError) => { if (err) { console.log("listen fail"); return; } console.log("listen success"); }) tcpServer.getState((err: BusinessError, data: socket.SocketStateBase) => { if (err) { console.log('getState fail'); return; } console.log('getState success:' + JSON.stringify(data)); }) ``` ### getState10+ getState(): Promise\ Obtains the status of a TCP socket server connection. This API uses a promise to return the result. > **NOTE** > This API can be called only after **listen** is successfully called. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | ------------------------------------------- | ----------------------------------------- | | Promise<[SocketStateBase](#socketstatebase)> | Promise used to return the result.| **Error codes** | ID| Error Message | | -------- | ------------------------------- | | 201 | Permission denied. | | 2300002 | System internal error. | | 2303188 | Socket operation on non-socket. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); let listenAddr: socket.NetAddress = { address: '192.168.xx.xxx', port: 8080, family: 1 } tcpServer.listen(listenAddr, (err: BusinessError) => { if (err) { console.log("listen fail"); return; } console.log("listen success"); }) tcpServer.getState().then((data: socket.SocketStateBase) => { console.log('getState success' + JSON.stringify(data)); }).catch((err: BusinessError) => { console.log('getState fail'); }); ``` ### setExtraOptions10+ setExtraOptions(options: TCPExtraOptions, callback: AsyncCallback\): void Sets other properties of a TCP socket server connection. This API uses an asynchronous callback to return the result. > **NOTE** > This API can be called only after **listen** is successfully called. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ----------------------------------- | ---- | ------------------------------------------------------------ | | options | [TCPExtraOptions](#tcpextraoptions) | Yes | Other properties of a TCP socket server connection.| | callback | AsyncCallback\ | Yes | Callback used to return the result. | **Error codes** | ID| Error Message | | -------- | ------------------------------- | | 401 | Parameter error. | | 201 | Permission denied. | | 2300002 | System internal error. | | 2303188 | Socket operation on non-socket. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); let listenAddr: socket.NetAddress = { address: '192.168.xx.xxx', port: 8080, family: 1 } tcpServer.listen(listenAddr, (err: BusinessError) => { if (err) { console.log("listen fail"); return; } console.log("listen success"); }) let tcpExtraOptions: socket.TCPExtraOptions = { keepAlive: true, OOBInline: true, TCPNoDelay: true, socketLinger: { on: true, linger: 10 }, receiveBufferSize: 1000, sendBufferSize: 1000, reuseAddress: true, socketTimeout: 3000 } tcpServer.setExtraOptions(tcpExtraOptions, (err: BusinessError) => { if (err) { console.log('setExtraOptions fail'); return; } console.log('setExtraOptions success'); }); ``` ### setExtraOptions10+ setExtraOptions(options: TCPExtraOptions): Promise\ Sets other properties of a TCP socket server connection. This API uses a promise to return the result. > **NOTE** > This API can be called only after **listen** is successfully called. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ------- | ----------------------------------- | ---- | ------------------------------------------------------------ | | options | [TCPExtraOptions](#tcpextraoptions) | Yes | Other properties of a TCP socket server connection.| **Return value** | Type | Description | | -------------- | --------------------------------------------------------- | | Promise\ | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | -------- | ------------------------------- | | 401 | Parameter error. | | 201 | Permission denied. | | 2300002 | System internal error. | | 2303188 | Socket operation on non-socket. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); let listenAddr: socket.NetAddress = { address: '192.168.xx.xxx', port: 8080, family: 1 } tcpServer.listen(listenAddr, (err: BusinessError) => { if (err) { console.log("listen fail"); return; } console.log("listen success"); }) let tcpExtraOptions: socket.TCPExtraOptions = { keepAlive: true, OOBInline: true, TCPNoDelay: true, socketLinger: { on: true, linger: 10 }, receiveBufferSize: 1000, sendBufferSize: 1000, reuseAddress: true, socketTimeout: 3000 } tcpServer.setExtraOptions(tcpExtraOptions).then(() => { console.log('setExtraOptions success'); }).catch((err: BusinessError) => { console.log('setExtraOptions fail'); }); ``` ### on('connect')10+ on(type: 'connect', callback: Callback\): void Subscribes to **connect** events of a **TCPSocketServer** object. This API uses an asynchronous callback to return the result. > **NOTE** > This API can be called only after **listen** is successfully called. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------- | ---- | ------------------------------------- | | type | string | Yes | Event type.
**connect**: connection event.| | callback | Callback<[TCPSocketConnection](#tcpsocketconnection10)> | Yes | Callback used to return the result. | **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); tcpServer.on('connect', (data: socket.TCPSocketConnection) => { console.log(JSON.stringify(data)) }); ``` ### off('connect')10+ off(type: 'connect', callback?: Callback\): void Unsubscribes from **connect** events of a **TCPSocketServer** object. This API uses an asynchronous callback to return the result. > **NOTE** > 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------- | ---- | ------------------------------------- | | type | string | Yes | Event type.
**connect**: connection event.| | callback | Callback<[TCPSocketConnection](#tcpsocketconnection10)> | No | Callback used to return the result. | **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); let callback = (data: socket.TCPSocketConnection) => { console.log('on connect message: ' + JSON.stringify(data)); } tcpServer.on('connect', callback); // 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. tcpServer.off('connect', callback); tcpServer.off('connect'); ``` ### on('error')10+ on(type: 'error', callback: ErrorCallback): void Subscribes to **error** events of a **TCPSocketServer** object. This API uses an asynchronous callback to return the result. > **NOTE** > This API can be called only after **listen** is successfully called. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------- | ---- | ------------------------------------ | | type | string | Yes | Event type.
**error**: error event.| | callback | ErrorCallback | Yes | Callback used to return the result. | **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); tcpServer.on('error', (err: BusinessError) => { console.log("on error, err:" + JSON.stringify(err)) }); ``` ### off('error')10+ off(type: 'error', callback?: ErrorCallback): void Unsubscribes from **error** events of a **TCPSocketServer** object. This API uses an asynchronous callback to return the result. > **NOTE** > 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------- | ---- | ------------------------------------ | | type | string | Yes | Event type.
**error**: error event.| | callback | ErrorCallback | No | Callback used to return the result. | **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); let callback = (err: BusinessError) => { console.log("on error, err:" + JSON.stringify(err)); } tcpServer.on('error', callback); // 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. tcpServer.off('error', callback); tcpServer.off('error'); ``` ## TCPSocketConnection10+ Defines 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. > **NOTE** > 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. **System capability**: SystemCapability.Communication.NetStack ### Attributes | Name | Type | Mandatory| Description | | -------- | ------ | ---- | ----------------------------------------- | | clientId | number | Yes | ID of the connection between the client and TCPSocketServer.| ### send10+ send(options: TCPSendOptions, callback: AsyncCallback\): void Sends data over a **TCPSocketConnection** object. This API uses an asynchronous callback to return the result. > **NOTE** > This API can be called only after a connection with the client is set up. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | --------------------------------- | ---- | ------------------------------------------------------------ | | options | [TCPSendOptions](#tcpsendoptions) | Yes | Defines the parameters for sending data over a TCP socket connection.| | callback | AsyncCallback\ | Yes | Callback used to return the result. | **Error codes** | ID| Error Message | | -------- | ---------------------- | | 401 | Parameter error. | | 201 | Permission denied. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); tcpServer.on('connect', (client: socket.TCPSocketConnection) => { let tcpSendOption: socket.TCPSendOptions = { data: 'Hello, client!' } client.send(tcpSendOption, () => { console.log('send success'); }); }); ``` ### send10+ send(options: TCPSendOptions): Promise\ Sends data over a **TCPSocketConnection** object. This API uses a promise to return the result. > **NOTE** > This API can be called only after a connection with the client is set up. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ------- | --------------------------------- | ---- | ------------------------------------------------------------ | | options | [TCPSendOptions](#tcpsendoptions) | Yes | Defines the parameters for sending data over a TCP socket connection.| **Return value** | Type | Description | | -------------- | ----------------------------------------------------------- | | Promise\ | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | -------- | ---------------------- | | 401 | Parameter error. | | 201 | Permission denied. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); tcpServer.on('connect', (client: socket.TCPSocketConnection) => { let tcpSendOption: socket.TCPSendOptions = { data: 'Hello, client!' } client.send(tcpSendOption).then(() => { console.log('send success'); }).catch((err: BusinessError) => { console.log('send fail'); }); }); ``` ### close10+ close(callback: AsyncCallback\): void Closes a TCP socket connection. This API uses an asynchronous callback to return the result. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | --------------------- | ---- | ---------- | | callback | AsyncCallback\ | Yes | Callback used to return the result.| **Error codes** | ID| Error Message | | -------- | ---------------------- | | 401 | Parameter error. | | 201 | Permission denied. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); tcpServer.on('connect', (client: socket.TCPSocketConnection) => { client.close((err: BusinessError) => { if (err) { console.log('close fail'); return; } console.log('close success'); }); }); ``` ### close10+ close(): Promise\ Closes a TCP socket connection. This API uses a promise to return the result. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | -------------- | ------------------------------------------- | | Promise\ | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | -------- | ---------------------- | | 201 | Permission denied. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); tcpServer.on('connect', (client: socket.TCPSocketConnection) => { client.close().then(() => { console.log('close success'); }).catch((err: BusinessError) => { console.log('close fail'); }); }); ``` ### getRemoteAddress10+ getRemoteAddress(callback: AsyncCallback\): void Obtains the remote address of a socket connection. This API uses an asynchronous callback to return the result. > **NOTE** > This API can be called only after a connection with the client is set up. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------------------------------- | ---- | ---------- | | callback | AsyncCallback<[NetAddress](#netaddress)> | Yes | Callback used to return the result.| **Error codes** | ID| Error Message | | -------- | ------------------------------- | | 401 | Parameter error. | | 201 | Permission denied. | | 2300002 | System internal error. | | 2303188 | Socket operation on non-socket. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); tcpServer.on('connect', (client: socket.TCPSocketConnection) => { client.getRemoteAddress((err: BusinessError, data: socket.NetAddress) => { if (err) { console.log('getRemoteAddress fail'); return; } console.log('getRemoteAddress success:' + JSON.stringify(data)); }); }); ``` ### getRemoteAddress10+ getRemoteAddress(): Promise\ Obtains the remote address of a socket connection. This API uses a promise to return the result. > **NOTE** > This API can be called only after a connection with the client is set up. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | --------------------------------- | ------------------------------------------ | | Promise<[NetAddress](#netaddress)> | Promise used to return the result.| **Error codes** | ID| Error Message | | -------- | ------------------------------- | | 201 | Permission denied. | | 2300002 | System internal error. | | 2303188 | Socket operation on non-socket. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); tcpServer.on('connect', (client: socket.TCPSocketConnection) => { client.getRemoteAddress().then(() => { console.log('getRemoteAddress success'); }).catch((err: BusinessError) => { console.log('getRemoteAddress fail'); }); }); ``` ### on('message')10+ on(type: 'message', callback: Callback): void Subscribes to **message** events of a **TCPSocketConnection** object. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------------------------------------ | ---- | ----------------------------------------- | | type | string | Yes | Event type.
**message**: message receiving event.| | callback | Callback<[SocketMessageInfo](#socketmessageinfo11)> | Yes | Callback used to return the result. | **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); class SocketInfo { message: ArrayBuffer = new ArrayBuffer(1); remoteInfo: socket.SocketRemoteInfo = {} as socket.SocketRemoteInfo; } tcpServer.on('connect', (client: socket.TCPSocketConnection) => { client.on('message', (value: SocketInfo) => { let messageView = ''; for (let i: number = 0; i < value.message.byteLength; i++) { let uint8Array = new Uint8Array(value.message) let messages = uint8Array[i] let message = String.fromCharCode(messages); messageView += message; } console.log('on message message: ' + JSON.stringify(messageView)); console.log('remoteInfo: ' + JSON.stringify(value.remoteInfo)); }); }); ``` ### off('message')10+ off(type: 'message', callback?: Callback<[SocketMessageInfo]\>): void Unsubscribes from **message** events of a **TCPSocketConnection** object. This API uses an asynchronous callback to return the result. > **NOTE** > 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------------------------------------ | ---- | ----------------------------------------- | | type | string | Yes | Event type.
**message**: message receiving event.| | callback | Callback<[SocketMessageInfo](#socketmessageinfo11)> | No | Callback used to return the result. | **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); class SocketInfo { message: ArrayBuffer = new ArrayBuffer(1); remoteInfo: socket.SocketRemoteInfo = {} as socket.SocketRemoteInfo; } let callback = (value: SocketInfo) => { let messageView = ''; for (let i: number = 0; i < value.message.byteLength; i++) { let uint8Array = new Uint8Array(value.message) let messages = uint8Array[i]] let message = String.fromCharCode(messages); messageView += message; } console.log('on message message: ' + JSON.stringify(messageView)); console.log('remoteInfo: ' + JSON.stringify(value.remoteInfo)); } tcpServer.on('connect', (client: socket.TCPSocketConnection) => { client.on('message', callback); // 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. client.off('message', callback); client.off('message'); }); ``` ### on('close')10+ on(type: 'close', callback: Callback\): void Subscribes to **close** events of a **TCPSocketConnection** object. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------- | ---- | ----------------------------------- | | type | string | Yes | Event type.
**close**: close event.| | callback | Callback\ | Yes | Callback used to return the result. | **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); tcpServer.on('connect', (client: socket.TCPSocketConnection) => { client.on('close', () => { console.log("on close success") }); }); ``` ### off('close')10+ off(type: 'close', callback?: Callback\): void Unsubscribes from **close** events of a **TCPSocketConnection** object. This API uses an asynchronous callback to return the result. > **NOTE** > 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------- | ---- | ----------------------------------- | | type | string | Yes | Event type.
**close**: close event.| | callback | Callback\ | No | Callback used to return the result. | **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); let callback = () => { console.log("on close success"); } tcpServer.on('connect', (client: socket.TCPSocketConnection) => { client.on('close', callback); // 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. client.off('close', callback); client.off('close'); }); ``` ### on('error')10+ on(type: 'error', callback: ErrorCallback): void Subscribes to **error** events of a **TCPSocketConnection** object. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------- | ---- | ------------------------------------ | | type | string | Yes | Event type.
**error**: error event.| | callback | ErrorCallback | Yes | Callback used to return the result. | **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); tcpServer.on('connect', (client: socket.TCPSocketConnection) => { client.on('error', (err: BusinessError) => { console.log("on error, err:" + JSON.stringify(err)) }); }); ``` ### off('error')10+ off(type: 'error', callback?: ErrorCallback): void Unsubscribes from **error** events of a **TCPSocketConnection** object. This API uses an asynchronous callback to return the result. > **NOTE** > 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------- | ---- | ------------------------------------ | | type | string | Yes | Event type.
**error**: error event.| | callback | ErrorCallback | No | Callback used to return the result. | **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let callback = (err: BusinessError) => { console.log("on error, err:" + JSON.stringify(err)); } let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); tcpServer.on('connect', (client: socket.TCPSocketConnection) => { client.on('error', callback); // 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. client.off('error', callback); client.off('error'); }); ``` ## Description of TCP Error Codes The TCP error code mapping is in the format of 2301000 + Linux kernel error code. For details about error codes, see [Socket Error Codes](../errorcodes/errorcode-net-socket.md). ## socket.constructLocalSocketInstance11+ constructLocalSocketInstance(): LocalSocket Creates a **LocalSocket** object. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | :--------------------------------- | :---------------------- | | [LocalSocket](#localsocket11) | **LocalSocket** object.| **Example** ```ts import socket from "@ohos.net.socket"; let client: socket.LocalSocket = socket.constructLocalSocketInstance(); ``` ## LocalSocket11+ Defines a **LocalSocket** object. Before calling LocalSocket APIs, you need to call [socket.constructLocalSocketInstance](#socketconstructlocalsocketinstance11) to create a **LocalSocket** object. ### bind11+ bind(address: LocalAddress): Promise\; Binds the address of a local socket file. This API uses a promise to return the result. > **NOTE** > This API explicitly binds the client to a local socket file based on the specified address. > It is not mandatory in local socket communication. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------------------------- | ---- | ------------------------------------------------------ | | address | [LocalAddress](#localaddress11) | Yes | Destination address. For details, see [LocalAddress](#localaddress11).| **Error codes** | ID| Error Message | | ------- | -------------------------- | | 401 | Parameter error. | | 2301013 | Insufficient permissions. | | 2301022 | Invalid argument. | | 2301098 | Address already in use. | **Example** ```ts import socket from "@ohos.net.socket"; let client = socket.constructLocalSocketInstance() let address : socket.LocalAddress = { address: '/tmp/testSocket' } client.bind(address).then(() => { console.log('bind success') }).catch((err: Object) => { console.error('failed to bind: ' + JSON.stringify(err)) }) ``` ### connect11+ connect(options: LocalConnectOptions): Promise\ Connects to the specified socket file. This API uses a promise to return the result. > **NOTE** > This API allows you to connect to the TCP server without first executing **localsocket.bind**. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ------- | ---------------------------------------- | ---- | ------------------------------------------------------------ | | options | [LocalConnectOptions](#localconnectoptions11) | Yes | Local socket connection parameters. For details, see [LocalConnectOptions](#localconnectoptions11).| **Return value** | Type | Description | | :-------------- | :---------------------------------------- | | Promise\ | Promise used to return the result.| **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 2301013 | Insufficient permissions. | | 2301022 | Invalid argument. | | 2301111 | Connection refused. | | 2301099 | Cannot assign requested address. | **Example** ```ts import socket from "@ohos.net.socket"; let client = socket.constructLocalSocketInstance(); let connectOpt: socket.LocalConnectOptions = { address: { address: '/tmp/testSocket' }, timeout: 6000 } client.connect(connectOpt).then(() => { console.log('connect success') }).catch((err: Object) => { console.error('connect fail: ' + JSON.stringify(err)); }); ``` ### send11+ send(options: LocalSendOptions): Promise\ Sends data over a local socket connection. This API uses a promise to return the result. > **NOTE** > This API can be called only after **connect** is successfully called. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ------- | --------------------------------------- | ---- | ------------------------------------------------------------ | | options | [LocalSendOptions](#localsendoptions11) | Yes | Parameters for sending data over a local socket connection. For details, see [LocalSendOptions](#localsendoptions11).| **Return value** | Type | Description | | :-------------- | :------------------------------------------ | | Promise\ | Promise used to return the result.| **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 2301011 | Operation would block. | **Example** ```ts import socket from "@ohos.net.socket" let client: socket.LocalSocket = socket.constructLocalSocketInstance() let connectOpt: socket.LocalConnectOptions = { address: { address: '/tmp/testSocket' }, timeout: 6000 } client.connect(connectOpt).then(() => { console.log('connect success') }).catch((err: Object) => { console.error('connect failed: ' + JSON.stringify(err)) }) let sendOpt: socket.LocalSendOptions = { data: 'Hello world!' } client.send(sendOpt).then(() => { console.log('send success') }).catch((err: Object) => { console.error('send fail: ' + JSON.stringify(err)) }) ``` ### close11+ close(): Promise\ Closes a local socket connection. This API uses a promise to return the result. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | :-------------- | :----------------------------------------- | | Promise\ | Promise used to return the result.| **Error codes** | ID| Error Message | | ------- | ----------------------- | | 2301009 | Bad file descriptor. | **Example** ```ts import socket from "@ohos.net.socket"; let client: socket.LocalSocket = socket.constructLocalSocketInstance(); client.close().then(() => { console.log('close success'); }).catch((err: Object) => { console.error('close fail: ' + JSON.stringify(err)); }); ``` ### getState11+ getState(): Promise\ Obtains the local socket connection status. This API uses a promise to return the result. > **NOTE** > This API can be called only after **bind** or **connect** is successfully called. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | :------------------------------------------- | :--------------------------------------- | | Promise<[SocketStateBase](#socketstatebase)> | Promise used to return the result.| **Example** ```ts import socket from "@ohos.net.socket"; let client: socket.LocalSocket = socket.constructLocalSocketInstance(); let connectOpt: socket.LocalConnectOptions = { address: { address: '/tmp/testSocket' }, timeout: 6000 } client.connect(connectOpt).then(() => { console.log('connect success'); client.getState().then(() => { console.log('getState success'); }).catch((err: Object) => { console.error('getState fail: ' + JSON.stringify(err)) }); }).catch((err: Object) => { console.error('connect fail: ' + JSON.stringify(err)); }); ``` ### getSocketFd11+ getSocketFd(): Promise\ Obtains the file descriptor of the **LocalSocket** object. This API uses a promise to return the result. > **NOTE** > This API can be called only after **bind** or **connect** is successfully called. > The file descriptor is allocated by the system kernel to uniquely identify the local socket in use. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | :---------------- | :-------------------------------- | | Promise\ | Promise used to return the result.| **Example** ```ts import socket from "@ohos.net.socket"; let client: socket.LocalSocket = socket.constructLocalSocketInstance(); let connectOpt: socket.LocalConnectOptions = { address: { address: '/tmp/testSocket' }, timeout: 6000 } client.connect(connectOpt).then(() => { console.log('connect ok') }).catch((err: Object) => { console.error('connect fail: ' + JSON.stringify(err)) }) client.getSocketFd().then((data: number) => { console.info("fd: " + data); }).catch((err: Object) => { console.error("getSocketFd faile: " + JSON.stringify(err)); }) ``` ### setExtraOptions11+ setExtraOptions(options: ExtraOptionsBase): Promise\ Sets other properties of the local socket connection. This API uses a promise to return the result. > **NOTE** > This API can be called only after **bind** or **connect** is successfully called. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | | options | [ExtraOptionsBase](#extraoptionsbase10) | Yes | Other properties of the local socket connection. For details, see [ExtraOptionsBase](#extraoptionsbase10).| **Return value** | Type | Description | | :-------------- | :-------------------------------------------- | | Promise\ | Promise used to return the result.| **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 201 | Permission denied. | **Example** ```ts import socket from "@ohos.net.socket"; let client: socket.LocalSocket = socket.constructLocalSocketInstance(); let connectOpt: socket.LocalConnectOptions = { address: { address: '/tmp/testSocket' }, timeout: 6000 } client.connect(connectOpt).then(() => { console.log('connect success'); let options: socket.ExtraOptionsBase = { receiveBufferSize: 8000, sendBufferSize: 8000, socketTimeout: 3000 } client.setExtraOptions(options).then(() => { console.log('setExtraOptions success'); }).catch((err: Object) => { console.error('setExtraOptions fail: ' + JSON.stringify(err)); }); }).catch((err: Object) => { console.error('connect fail: ' + JSON.stringify(err)); }); ``` ### getExtraOptions11+ getExtraOptions(): Promise\; Obtains other properties of the local socket connection. This API uses a promise to return the result. > **NOTE** > This API can be called only after **bind** or **connect** is successfully called. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | :-------------------------- | :---------------------------------------- | | Promise\<[ExtraOptionsBase](#extraoptionsbase10)\> | Promise used to return the result.| **Error codes** | ID| Error Message | | ------- | ----------------------- | | 2301009 | Bad file descriptor. | **Example** ```ts import socket from "@ohos.net.socket"; let client: socket.LocalSocket = socket.constructLocalSocketInstance(); let connectOpt: socket.LocalConnectOptions = { address: { address: '/tmp/testSocket' }, timeout: 6000 } client.connect(connectOpt).then(() => { console.log('connect success'); client.getExtraOptions().then((options : socket.ExtraOptionsBase) => { console.log('options: ' + JSON.stringify(options)); }).catch((err: Object) => { console.error('setExtraOptions fail: ' + JSON.stringify(err)); }); }).catch((err: Object) => { console.error('connect fail: ' + JSON.stringify(err)); }); ``` ### on('message')11+ on(type: 'message', callback: Callback\): void Subscribes to **message** events of a **LocalSocket** object. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ----------------------------------------------- | ---- | ----------------------------------- | | type | string | Yes | Event type.
**message**: message receiving event.| | callback | Callback\<[SocketMessageInfo](#socketmessageinfo11)\> | Yes | Callback used to return the result.| **Example** ```ts import socket from "@ohos.net.socket"; let client: socket.LocalSocket = socket.constructLocalSocketInstance(); client.on('message', (value: socket.LocalSocketMessageInfo) => { const uintArray = new Uint8Array(value.message) let messageView = ''; for (let i = 0; i < uintArray.length; i++) { messageView = String.fromCharCode(uintArray[i]); } console.log('total: ' + JSON.stringify(value)); console.log('message infomation: ' + messageView); }); ``` ### off('message')11+ off(type: 'message', callback?: Callback\): void Unsubscribes from **message** events of a **LocalSocket** object. This API uses an asynchronous callback to return the result. > **NOTE** > 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------------------------ | ---- | ----------------------------------- | | type | string | Yes | Event type.
**message**: message receiving event.| | callback | Callback\<[SocketMessageInfo](#socketmessageinfo11)\> | No | Callback passed to the **on** function.| **Example** ```ts import socket from "@ohos.net.socket"; let client: socket.LocalSocket = socket.constructLocalSocketInstance(); let messageView = ''; let callback = (value: socket.LocalSocketMessageInfo) => { const uintArray = new Uint8Array(value.message) let messageView = ''; for (let i = 0; i < uintArray.length; i++) { messageView = String.fromCharCode(uintArray[i]); } console.log('total: ' + JSON.stringify(value)); console.log('message infomation: ' + messageView); } client.on('message', callback); client.off('message'); ``` ### on('connect')11+ on(type: 'connect', callback: Callback\): void; Subscribes to **connect** events of a **LocalSocket** object. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------- | ---- | --------------------------------------------------------- | | type | string | Yes | Event type.
| | callback | Callback\ | Yes | Callback used to return the result. | **Example** ```ts import socket from "@ohos.net.socket"; let client: socket.LocalSocket = socket.constructLocalSocketInstance(); client.on('connect', () => { console.log("on connect success") }); ``` ### off('connect')11+ off(type: 'connect', callback?: Callback\): void; Unsubscribes from **connect** events of a **LocalSocket** object. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------- | ---- | --------------------------------------------------------- | | type | string | Yes | Event type.
| | callback | Callback\ | Yes | Callback passed to the **on** function. | **Example** ```ts import socket from "@ohos.net.socket"; let client: socket.LocalSocket = socket.constructLocalSocketInstance(); let callback = () => { console.log("on connect success"); } client.on('connect', callback); // 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. client.off('connect', callback); client.off('connect'); ``` ### on('close')11+ on(type: 'close', callback: Callback\): void; Subscribes to **close** events of a **LocalSocket** object. This API uses an asynchronous callback to return the result. > **NOTE** > 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------- | ---- | ------------------------ | | type | string | Yes | Event type.| | callback | Callback\ | No | Callback used to return the result.| **Example** ```ts import socket from "@ohos.net.socket"; let client: socket.LocalSocket = socket.constructLocalSocketInstance(); let callback = () => { console.log("on close success"); } client.on('close', callback); ``` ### off('close')11+ off(type: 'close', callback?: Callback\): void; Unsubscribes from **close** events of a **LocalSocket** object. This API uses an asynchronous callback to return the result. > **NOTE** > 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------- | ---- | ------------------------ | | type | string | Yes | Event type.| | callback | Callback\ | No | Callback passed to the **on** function.| **Example** ```ts import socket from "@ohos.net.socket"; let client: socket.LocalSocket = socket.constructLocalSocketInstance(); let callback = () => { console.log("on close success"); } client.on('close', callback); // 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. client.off('close', callback); client.off('close'); ``` ### on('error')11+ on(type: 'error', callback: ErrorCallback): void Subscribes to **error** events of a **LocalSocket** object. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------- | ---- | ---------------------------- | | type | string | Yes | Event type. | | callback | ErrorCallback | Yes | Callback used to return the result.| **Example** ```ts import socket from "@ohos.net.socket"; let client: socket.LocalSocket = socket.constructLocalSocketInstance(); client.on('error', (err) => { console.log("on error, err:" + JSON.stringify(err)) }); ``` ### off('error')11+ off(type: 'error', callback?: ErrorCallback): void; Unsubscribes from **error** events of a **LocalSocket** object. This API uses an asynchronous callback to return the result. > **NOTE** > 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------- | ---- | ----------------------------- | | type | string | Yes | Event type.| | callback | ErrorCallback | No | Callback passed to the **on** function.| **Example** ```ts import socket from "@ohos.net.socket"; let client: socket.LocalSocket = socket.constructLocalSocketInstance(); let callback = (err) => { console.log("on error, err:" + JSON.stringify(err)); } client.on('error', callback); // 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. client.off('error', callback); client.off('error'); ``` ## LocalSocketMessageInfo11+ Defines the data received by the client over a local socket connection. **System capability**: SystemCapability.Communication.NetStack | Name | Type | Mandatory| Description | | ------- | --------------- | --- | ------------------ | | message | ArrayBuffer | Yes | Data received. | | address | string | Yes | Local socket connection address.| | size | number | Yes | Data length. | ## LocalAddress11+ Defines the address of a local socket file. When the address is passed for binding, a socket file is created at this address. **System capability**: SystemCapability.Communication.NetStack | Name | Type | Mandatory| Description | | ------- | ---------- | --- | ------------------ | | address | string | Yes | Address of the local socket file. | ## LocalConnectOptions11+ Defines local socket connection parameters. **System capability**: SystemCapability.Communication.NetStack | Name | Type | Mandatory| Description | | ------- | ---------- | --- | ------------------------------ | | address | string | Yes | Address of the local socket file. | | timeout | number | No | Timeout duration of the local socket connection, in ms. | ## LocalSendOptions11+ Defines the parameters for sending data over a local socket connection. **System capability**: SystemCapability.Communication.NetStack | Name | Type | Mandatory| Description | | ------- | ---------- | --- | ------------------- | | address | string | Yes | Address of the local socket file.| | timeout | encoding | No | Encoding format of the string. | ## ExtraOptionsBase10+ Defines other properties of the local socket connection. **System capability**: SystemCapability.Communication.NetStack | Name | Type | Mandatory| Description | | ----------------- | ------- | ---- | ----------------------------- | | receiveBufferSize | number | No | Size of the receive buffer, in bytes. | | sendBufferSize | number | No | Size of the send buffer, in bytes. | | reuseAddress | boolean | No | Whether to reuse addresses. | | socketTimeout | number | No | Timeout duration of the local socket connection, in ms. | ## socket.constructLocalSocketServerInstance11+ constructLocalSocketServerInstance(): LocalSocketServer Creates a **LocalSocketServer** object. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | :---------------------------------- | :---------------------------- | | [LocalSocketServer](#localsocketserver11) | **LocalSocketServer** object.| **Example** ```ts import socket from "@ohos.net.socket"; let server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance(); ``` ## LocalSocketServer11+ Defines a local socket server connection. Before calling LocalSocketServer APIs, you need to call [socket.constructLocalSocketServerInstance](#socketconstructlocalsocketserverinstance11) to create a **LocalSocketServer** object. ### listen11+ listen(address: LocalAddress): Promise\ Binds 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. > **NOTE** > The server uses this API to perform the **bind**, **listen**, and **accept** operations. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ------- | ------------------------- | ---- | --------------------------------------------- | | address | [LocalAddress](#localaddress11) | Yes | Destination address.| **Return value** | Type | Description | | :-------------- | :---------------------------------------------------- | | Promise\ | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | -------- | --------------------------- | | 401 | Parameter error. | | 2303109 | Bad file number. | | 2301013 | Insufficient permissions. | | 2301022 | Invalid argument. | | 2303198 | Address already in use. | **Example** ```ts import socket from "@ohos.net.socket"; let server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance(); let addr: socket.LocalAddress = { address: '/tmp/testSocket' } server.listen(addr).then(() => { console.log('listen success'); }).catch((err: Object) => { console.error('listen fail: ' + JSON.stringify(err)); }); ``` ### getState11+ getState(): Promise\ Obtains the status of a local socket server connection. This API uses a promise to return the result. > **NOTE** > This API can be called only after **listen** is successfully called. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | :------------------------------------------- | :--------------------------------------------- | | Promise\<[SocketStateBase](#socketstatebase)\> | Promise used to return the result.| **Example** ```ts import socket from "@ohos.net.socket"; let server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance(); let listenAddr: socket.LocalAddress = { address: '/tmp/testSocket' } server.listen(listenAddr).then(() => { console.log("listen success"); }).catch((err: Object) => { console.error("listen fail: " + JSON.stringify(err)); }) server.getState().then((data: socket.SocketStateBase) => { console.log('getState success: ' + JSON.stringify(data)); }).catch((err: Object) => { console.error('getState fail: ' + JSON.stringify(err)); }); ``` ### setExtraOptions11+ setExtraOptions(options: ExtraOptionsBase): Promise\ Sets other properties of the local socket server connection. This API uses a promise to return the result. > **NOTE** > This API can be called only after **listen** is successfully called. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ------- | --------------------------------------- | ---- | ------------------------------ | | options | [ExtraOptionsBase](#extraoptionsbase10) | Yes | Other properties of a local socket server connection.| **Return value** | Type | Description | | :-------------- | :---------------------------------------------- | | Promise\ | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | -------- | ------------------------------- | | 401 | Parameter error. | | 2301009 | Bad file descriptor. | **Example** ```ts import socket from "@ohos.net.socket"; let server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance(); let listenAddr: socket.NetAddress = { address: '/tmp/testSocket' } server.listen(listenAddr).then(() => { console.log("listen success"); }).catch((err: Object) => { console.error("listen fail: " + JSON.stringify(err)); }) let options: socket.ExtraOptionsBase = { receiveBufferSize: 6000, sendBufferSize: 6000, socketTimeout: 3000 } server.setExtraOptions(options).then(() => { console.log('setExtraOptions success'); }).catch((err: Object) => { console.error('setExtraOptions fail: ' + JSON.stringify(err)); }); ``` ### getExtraOptions11+ getExtraOptions(): Promise\; Obtains other properties of a local socket server connection. This API uses a promise to return the result. > **NOTE** > This API can be called only after **listen** is successfully called. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | :-------------------------- | :-------------------------- | | Promise\<[ExtraOptionsBase](#extraoptionsbase10)\> | Promise used to return the result.| **Error codes** | ID| Error Message | | -------- | -------------------- | | 2301009 | Bad file descriptor. | **Example** ```ts import socket from "@ohos.net.socket"; let server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance(); let listenAddr: socket.NetAddress = { address: '/tmp/testSocket' } server.listen(listenAddr).then(() => { console.log("listen success"); }).catch((err: Object) => { console.error("listen fail: " + JSON.stringify(err)); }) server.getExtraOptions().then((options) => { console.log('options: ' + JSON.stringify(options)); }).catch((err: Object) => { console.error('getExtraOptions fail: ' + JSON.stringify(err)); }); ``` ### on('connect')11+ on(type: 'connect', callback: Callback\): void Subscribes to **connect** events of a **LocalSocketServer** object. This API uses an asynchronous callback to return the result. > **NOTE** > This API can be called only after **listen** is successfully called. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------- | ---- | ------------------------------------- | | type | string | Yes | Event type.
**connect**: connection event.| | callback | Callback\<[LocalSocketConnection](#localsocketconnection11)\> | Yes | Callback used to return the result.| **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; let server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance(); server.on('connect', (connection: socket.LocalSocketConnection) => { if (connection) { console.log('accept a client') } }); ``` ### off('connect')11+ off(type: 'connect', callback?: Callback\): void Unsubscribes from **connect** events of a **LocalSocketServer** object. This API uses an asynchronous callback to return the result. > **NOTE** > 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------- | ---- | ------------------------------------- | | type | string | Yes | Event type.
**connect**: connection event.| | callback | Callback\<[LocalSocketConnection](#localsocketconnection11)\> | No | Callback passed to the **on** function.| **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; let server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance(); let callback = (connection: socket.LocalSocketConnection) => { if (connection) { console.log('accept a client') } } server.on('connect', callback); // 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. server.off('connect', callback); server.off('connect'); ``` ### on('error')11+ on(type: 'error', callback: ErrorCallback): void Subscribes to **error** events of a **LocalSocketServer** object. This API uses an asynchronous callback to return the result. > **NOTE** > This API can be called only after **listen** is successfully called. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------- | ---- | ------------------------------------ | | type | string | Yes | Event type.
**error**: error event.| | callback | ErrorCallback | Yes | Callback used to return the result.| **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; let server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance(); server.on('error', (err) => { console.error("on error, err:" + JSON.stringify(err)) }); ``` ### off('error')11+ off(type: 'error', callback?: ErrorCallback): void Unsubscribes from **error** events of a **LocalSocketServer** object. This API uses an asynchronous callback to return the result. > **NOTE** > 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------- | ---- | ------------------------------------ | | type | string | Yes | Event type.
**error**: error event.| | callback | ErrorCallback | No | Callback passed to the **on** function. | **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; let server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance(); let callback = (err) => { console.error("on error, err:" + JSON.stringify(err)); } server.on('error', callback); // 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. server.off('error', callback); server.off('error'); ``` ## LocalSocketConnection11+ Defines 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. > **NOTE** > 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. **System capability**: SystemCapability.Communication.NetStack ### Attributes | Name | Type | Mandatory| Description | | -------- | ------ | ---- | ---------------------------- | | clientId | number | Yes | ID of the session between the client and the server.| ### send11+ send(options: LocalSendOptions): Promise\ Sends data through a local socket connection. This API uses a promise to return the result. > **NOTE** > This API can be used only after the server obtains a **LocalSocketConnection** object through the **callback** of the **connect** event. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ------- | --------------------------------- | ---- | -------------------------------------- | | options | [LocalSendOptions](#localsendoptions11) | Yes | Defines the parameters for sending data over a local socket connection.| **Return value** | Type | Description | | :-------------- | :---------------------------------------------- | | Promise\ | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | -------- | ---------------------- | | 401 | Parameter error. | | 2301011 | Operation would block. | **Example** ```ts import socket from "@ohos.net.socket"; let server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance(); server.on('connect', (connection: socket.LocalSocketConnection) => { let sendOptions: socket.LocalSendOptions = { data: 'Hello, client!' } connection.send(sendOptions).then(() => { console.log('send success'); }).catch((err: Object) => { console.error('send fail: ' + JSON.stringify(err)); }); }); ``` ### close11+ close(): Promise\ Closes a local socket connection. This API uses a promise to return the result. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | :-------------- | :------------------------------------------- | | Promise\ | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | -------- | -------------------- | | 201 | Permission denied. | | 2301009 | Bad file descriptor. | **Example** ```ts import socket from "@ohos.net.socket"; let server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance(); server.on('connect', (connection: socket.LocalSocketConnection) => { connection.close().then(() => { console.log('close success'); }).catch((err: Object) => { console.error('close fail: ' + JSON.stringify(err)); }); }); ``` ### on('message')11+ on(type: 'message', callback: Callback\): void; Subscribes to **message** events of a **LocalSocketConnection** object. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ----------------------------------------------- | ---- | --------------------------------------- | | type | string | Yes | Event type.
**message**: message receiving event. | | callback | Callback\<[SocketMessageInfo](#socketmessageinfo11)\> | Yes | Callback used to return the result.| **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; let server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance(); let listenAddr: socket.LocalAddress = { address: '/tmp/testSocket' } server.listen(listenAddr).then(() => { console.log("listen success"); }).catch((err: Object) => { console.error("listen fail: " + JSON.stringify(err)); }); server.on('connect', (connection: socket.LocalSocketConnection) => { connection.on('message', (value: socket.LocalSocketMessageInfo) => { const uintArray = new Uint8Array(value.message); let messageView = ''; for (let i = 0; i < uintArray.length; i++) { messageView = String.fromCharCode(uintArray[i]); } console.log('total: ' + JSON.stringify(value)); console.log('message infomation: ' + messageView); }); }); ``` ### off('message')11+ off(type: 'message', callback?: Callback\): void Unsubscribes from **message** events of a **LocalSocketConnection** object. This API uses an asynchronous callback to return the result. > **NOTE** > 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ----------------------------------------------- | ---- | ----------------------------------- | | type | string | Yes | Event type.
**message**: message receiving event.| | callback | Callback\<[SocketMessageInfo](#socketmessageinfo11)\> | No | Callback passed to the **on** function.| **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; let server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance(); let callback = (value: socket.LocalSocketMessageInfo) => { const uintArray = new Uint8Array(value.message) let messageView = ''; for (let i = 0; i < uintArray.length; i++) { messageView = String.fromCharCode(uintArray[i]); } console.log('total: ' + JSON.stringify(value)); console.log('message infomation: ' + messageView); } server.on('connect', (connection: socket.LocalSocketConnection) => { connection.on('message', callback); // 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. connection.off('message', callback); connection.off('message'); }); ``` ### on('close')11+ on(type: 'close', callback: Callback\): void Unsubscribes from **close** events of a **LocalSocketConnection** object. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------- | ---- | ----------------------------------- | | type | string | Yes | Event type.
**close**: close event.| | callback | Callback\ | Yes | Callback used to return the result.| **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; let server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance(); server.on('connect', (connection: socket.LocalSocketConnection) => { connection.on('close', () => { console.log("on close success") }); }); ``` ### off('close')11+ off(type: 'close', callback?: Callback\): void Unsubscribes from **close** events of a **LocalSocketConnection** object. This API uses an asynchronous callback to return the result. > **NOTE** > 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------- | ---- | ----------------------------------- | | type | string | Yes | Event type.
**close**: close event.| | callback | Callback\ | No | Callback passed to the **on** function.| **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; let server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance(); let callback = () => { console.log("on close success"); } server.on('connect', (connection: socket.LocalSocketConnection) => { connection.on('close', callback); // 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. connection.off('close', callback); connection.off('close'); }); ``` ### on('error')11+ on(type: 'error', callback: ErrorCallback): void Subscribes to **error** events of a **LocalSocketConnection** object. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------- | ---- | ------------------------------------ | | type | string | Yes | Event type.
**error**: error event.| | callback | ErrorCallback | Yes | Callback used to return the result.| **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; let server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance(); server.on('connect', (connection: socket.LocalSocketConnection) => { connection.on('error', (err) => { console.error("on error, err:" + JSON.stringify(err)) }); }); ``` ### off('error')11+ off(type: 'error', callback?: ErrorCallback): void Unsubscribes from **error** events of a **LocalSocketConnection** object. This API uses an asynchronous callback to return the result. > **NOTE** > 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------- | ---- | ------------------------------------ | | type | string | Yes | Event type.
**error**: error event.| | callback | ErrorCallback | No | Callback passed to the **on** function. | **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; let callback = (err) => { console.error("on error, err: " + JSON.stringify(err)); } let server: socket.LocalSocketServer = socket.constructLocalSocketServerInstance(); server.on('connect', (connection: socket.LocalSocketConnection) => { connection.on('error', callback); // 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. connection.off('error', callback); connection.off('error'); }); ``` ## Description of LocalSocket Error Codes The LocalSocket error code mapping is in the format of 2301000 + Linux kernel error code. For details about error codes, see [Socket Error Codes](../errorcodes/errorcode-net-socket.md). ## socket.constructTLSSocketInstance9+ constructTLSSocketInstance(): TLSSocket Creates a **TLSSocket** object. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | --------------------------------- | ---------------------- | | [TLSSocket](#tlssocket9) | **TLSSocket** object.| **Example** ```ts import socket from "@ohos.net.socket"; let tls: socket.TLSSocket = socket.constructTLSSocketInstance(); ``` ## TLSSocket9+ Defines a TLS socket connection. Before calling TLSSocket APIs, you need to call [socket.constructTLSSocketInstance](#socketconstructtlssocketinstance9) to create a **TLSSocket** object. ### bind9+ bind(address: NetAddress, callback: AsyncCallback\): void Binds the IP address and port number. This API uses an asynchronous callback to return the result. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------------------------- | ---- | ------------------------------------------------------ | | address | [NetAddress](#netaddress) | Yes | Destination address. For details, see [NetAddress](#netaddress).| | callback | AsyncCallback\ | 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.| **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 201 | Permission denied. | | 2303198 | Address already in use. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tls: socket.TLSSocket = socket.constructTLSSocketInstance(); let bindAddr: socket.NetAddress = { address: '192.168.xx.xxx', port: 8080 } tls.bind(bindAddr, (err: BusinessError) => { if (err) { console.log('bind fail'); return; } console.log('bind success'); }); ``` ### bind9+ bind(address: NetAddress): Promise\ Binds the IP address and port number. This API uses a promise to return the result. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ------- | ---------------------------------- | ---- | ------------------------------------------------------ | | address | [NetAddress](#netaddress) | Yes | Destination address. For details, see [NetAddress](#netaddress).| **Return value** | Type | Description | | -------------- | ------------------------------------------------------- | | Promise\ | Promise used to return the result. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | ------- | ----------------------- | | 401 | Parameter error. | | 201 | Permission denied. | | 2303198 | Address already in use. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tls: socket.TLSSocket = socket.constructTLSSocketInstance(); let bindAddr: socket.NetAddress = { address: '192.168.xx.xxx', port: 8080 } tls.bind(bindAddr).then(() => { console.log('bind success'); }).catch((err: BusinessError) => { console.log('bind fail'); }); ``` ### getState9+ getState(callback: AsyncCallback\): void Obtains the status of the TLS socket connection. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------------------------------ | ---- | ---------- | | 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.| **Error codes** | ID| Error Message | | ------- | ------------------------------ | | 2303188 | Socket operation on non-socket.| | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tls: socket.TLSSocket = socket.constructTLSSocketInstance(); let bindAddr: socket.NetAddress = { address: '192.168.xx.xxx', port: 8080 } tls.bind(bindAddr, (err: BusinessError) => { if (err) { console.log('bind fail'); return; } console.log('bind success'); }); tls.getState((err: BusinessError, data: socket.SocketStateBase) => { if (err) { console.log('getState fail'); return; } console.log('getState success:' + JSON.stringify(data)); }); ``` ### getState9+ getState(): Promise\ Obtains the status of the TLS socket connection. This API uses a promise to return the result. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | ----------------------------------------------- | ----------------------------------------- | | Promise\<[SocketStateBase](#socketstatebase)> | Promise used to return the result. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | ------- | ------------------------------ | | 2303188 | Socket operation on non-socket.| | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tls: socket.TLSSocket = socket.constructTLSSocketInstance(); let bindAddr: socket.NetAddress = { address: '192.168.xx.xxx', port: 8080 } tls.bind(bindAddr, (err: BusinessError) => { if (err) { console.log('bind fail'); return; } console.log('bind success'); }); tls.getState().then(() => { console.log('getState success'); }).catch((err: BusinessError) => { console.log('getState fail'); }); ``` ### setExtraOptions9+ setExtraOptions(options: TCPExtraOptions, callback: AsyncCallback\): void Sets other properties of the TCP socket connection after **bind** is successfully called. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | | options | [TCPExtraOptions](#tcpextraoptions) | Yes | Other properties of the TCP socket connection. For details, see [TCPExtraOptions](#tcpextraoptions).| | callback | AsyncCallback\ | 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.| **Error codes** | ID| Error Message | | ------- | ----------------------------- | | 401 | Parameter error. | | 2303188 | Socket operation on non-socket.| | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tls: socket.TLSSocket = socket.constructTLSSocketInstance(); let bindAddr: socket.NetAddress = { address: '192.168.xx.xxx', port: 8080 } tls.bind(bindAddr, (err: BusinessError) => { if (err) { console.log('bind fail'); return; } console.log('bind success'); }); let tcpExtraOptions: socket.TCPExtraOptions = { keepAlive: true, OOBInline: true, TCPNoDelay: true, socketLinger: { on: true, linger: 10 }, receiveBufferSize: 1000, sendBufferSize: 1000, reuseAddress: true, socketTimeout: 3000 } tls.setExtraOptions(tcpExtraOptions, (err: BusinessError) => { if (err) { console.log('setExtraOptions fail'); return; } console.log('setExtraOptions success'); }); ``` ### setExtraOptions9+ setExtraOptions(options: TCPExtraOptions): Promise\ Sets other properties of the TCP socket connection after **bind** is successfully called. This API uses a promise to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | | options | [TCPExtraOptions](#tcpextraoptions) | Yes | Other properties of the TCP socket connection. For details, see [TCPExtraOptions](#tcpextraoptions).| **Return value** | Type | Description | | -------------- | --------------------------------------------------- | | Promise\ | Promise used to return the result. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | ------- | ------------------------------ | | 401 | Parameter error. | | 2303188 | Socket operation on non-socket.| | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tls: socket.TLSSocket = socket.constructTLSSocketInstance(); let bindAddr: socket.NetAddress = { address: '192.168.xx.xxx', port: 8080 } tls.bind(bindAddr, (err: BusinessError) => { if (err) { console.log('bind fail'); return; } console.log('bind success'); }); let tcpExtraOptions: socket.TCPExtraOptions = { keepAlive: true, OOBInline: true, TCPNoDelay: true, socketLinger: { on: true, linger: 10 }, receiveBufferSize: 1000, sendBufferSize: 1000, reuseAddress: true, socketTimeout: 3000 } tls.setExtraOptions(tcpExtraOptions).then(() => { console.log('setExtraOptions success'); }).catch((err: BusinessError) => { console.log('setExtraOptions fail'); }); ``` ### on('message')9+ on(type: 'message', callback: Callback\): void; Subscribes to **message** events of the TLS socket connection. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------------------------------------ | ---- | ----------------------------------------- | | type | string | Yes | Event type.
**message**: message receiving event.| | callback | Callback\<[SocketMessageInfo](#socketmessageinfo11)\> | Yes | Callback used to return the result.| **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tls: socket.TLSSocket = socket.constructTLSSocketInstance(); class SocketInfo { message: ArrayBuffer = new ArrayBuffer(1); remoteInfo: socket.SocketRemoteInfo = {} as socket.SocketRemoteInfo; } let messageView = ''; tls.on('message', (value: SocketInfo) => { for (let i: number = 0; i < value.message.byteLength; i++) { let uint8Array = new Uint8Array(value.message) let messages = uint8Array[i] let message = String.fromCharCode(messages); messageView += message; } console.log('on message message: ' + JSON.stringify(messageView)); console.log('remoteInfo: ' + JSON.stringify(value.remoteInfo)); }); ``` ### off('message')9+ off(type: 'message', callback?: Callback\): void Unsubscribes from **message** events of a **TLSSocket** object. This API uses an asynchronous callback to return the result. > **NOTE** > 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------------------------------------ | ---- | ----------------------------------------- | | type | string | Yes | Event type.
**message**: message receiving event.| | callback | Callback<[SocketMessageInfo](#socketmessageinfo11)> | No | Callback used to return the result.| **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tls: socket.TLSSocket = socket.constructTLSSocketInstance(); class SocketInfo { message: ArrayBuffer = new ArrayBuffer(1); remoteInfo: socket.SocketRemoteInfo = {} as socket.SocketRemoteInfo; } let messageView = ''; let callback = (value: SocketInfo) => { for (let i: number = 0; i < value.message.byteLength; i++) { let uint8Array = new Uint8Array(value.message) let messages = uint8Array[i] let message = String.fromCharCode(messages); messageView += message; } console.log('on message message: ' + JSON.stringify(messageView)); console.log('remoteInfo: ' + JSON.stringify(value.remoteInfo)); } tls.on('message', callback); // 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. tls.off('message', callback); ``` ### on('connect' | 'close')9+ on(type: 'connect' | 'close', callback: Callback\): void Subscribes to **connect** or **close** events of the TLS socket connection. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------- | ---- | ------------------------------------------------------------ | | type | string | Yes | Event type.
- **connect**: connection event.
- **close**: close event.| | callback | Callback\ | Yes | Callback used to return the result. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tls: socket.TLSSocket = socket.constructTLSSocketInstance(); tls.on('connect', () => { console.log("on connect success") }); tls.on('close', () => { console.log("on close success") }); ``` ### off('connect' | 'close')9+ off(type: 'connect' | 'close', callback?: Callback\): void Unsubscribes from **connect** or **close** events of a **TLSSocket** object. This API uses an asynchronous callback to return the result. > **NOTE** > 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------- | ---- | ------------------------------------------------------------ | | type | string | Yes | Event type.
- **connect**: connection event.
- **close**: close event.| | callback | Callback\ | No | Callback used to return the result. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tls: socket.TLSSocket = socket.constructTLSSocketInstance(); let callback1 = () => { console.log("on connect success"); } tls.on('connect', callback1); // 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. tls.off('connect', callback1); tls.off('connect'); let callback2 = () => { console.log("on close success"); } tls.on('close', callback2); // 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. tls.off('close', callback2); ``` ### on('error')9+ on(type: 'error', callback: ErrorCallback): void Subscribes to **error** events of the TLS socket connection. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------- | ---- | ------------------------------------ | | type | string | Yes | Event type.
**error**: error event.| | callback | ErrorCallback | Yes | Callback used to return the result. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tls: socket.TLSSocket = socket.constructTLSSocketInstance(); tls.on('error', (err: BusinessError) => { console.log("on error, err:" + JSON.stringify(err)) }); ``` ### off('error')9+ off(type: 'error', callback?: ErrorCallback): void Unsubscribes from **error** events of a **TLSSocket** object. This API uses an asynchronous callback to return the result. > **NOTE** > 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------- | ---- | ------------------------------------ | | type | string | Yes | Event type.
**error**: error event.| | callback | ErrorCallback | No | Callback used to return the result. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tls: socket.TLSSocket = socket.constructTLSSocketInstance(); let callback = (err: BusinessError) => { console.log("on error, err:" + JSON.stringify(err)); } tls.on('error', callback); // 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. tls.off('error', callback); ``` ### connect9+ connect(options: TLSConnectOptions, callback: AsyncCallback\): void Sets 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-----". **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description| | -------- | ---------------------------------------| ----| --------------- | | options | [TLSConnectOptions](#tlsconnectoptions9) | Yes | Parameters required for the TLS socket connection.| | callback | AsyncCallback\ | 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.| **Error codes** | ID| Error Message | | ------- | -------------------------------------------- | | 401 | Parameter error. | | 2303104 | Interrupted system call. | | 2303109 | Bad file number. | | 2303111 | Resource temporarily unavailable try again. | | 2303188 | Socket operation on non-socket. | | 2303191 | Protocol wrong type for socket. | | 2303198 | Address already in use. | | 2303199 | Cannot assign requested address. | | 2303210 | Connection timed out. | | 2303501 | SSL is null. | | 2303502 | Error in tls reading. | | 2303503 | Error in tls writing | | 2303505 | Error occurred in the tls system call. | | 2303506 | Error clearing tls connection. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsTwoWay: socket.TLSSocket = socket.constructTLSSocketInstance(); // Two way authentication let bindAddr: socket.NetAddress = { address: '0.0.0.0', } tlsTwoWay.bind(bindAddr, (err: BusinessError) => { if (err) { console.log('bind fail'); return; } console.log('bind success'); }); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsTwoWay.connect(tlsConnectOptions, (err: BusinessError) => { console.error("connect callback error" + err); }); let tlsOneWay: socket.TLSSocket = socket.constructTLSSocketInstance(); // One way authentication tlsOneWay.bind(bindAddr, (err: BusinessError) => { if (err) { console.log('bind fail'); return; } console.log('bind success'); }); let tlsOneWayConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { ca: ["xxxx", "xxxx"], cipherSuite: "AES256-SHA256" } } tlsOneWay.connect(tlsOneWayConnectOptions, (err: BusinessError) => { console.error("connect callback error" + err); }); ``` ### connect9+ connect(options: TLSConnectOptions): Promise\ Sets 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-----". **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description| | -------- | --------------------------------------| ----| --------------- | | options | [TLSConnectOptions](#tlsconnectoptions9) | Yes | Parameters required for the connection.| **Return value** | Type | Description | | ------------------------------------------- | ----------------------------- | | Promise\ | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | ------- | -------------------------------------------- | | 401 | Parameter error. | | 2303104 | Interrupted system call. | | 2303109 | Bad file number. | | 2303111 | Resource temporarily unavailable try again. | | 2303188 | Socket operation on non-socket. | | 2303191 | Protocol wrong type for socket. | | 2303198 | Address already in use. | | 2303199 | Cannot assign requested address. | | 2303210 | Connection timed out. | | 2303501 | SSL is null. | | 2303502 | Error in tls reading. | | 2303503 | Error in tls writing | | 2303505 | Error occurred in the tls system call. | | 2303506 | Error clearing tls connection. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsTwoWay: socket.TLSSocket = socket.constructTLSSocketInstance(); // Two way authentication let bindAddr: socket.NetAddress = { address: '0.0.0.0', } tlsTwoWay.bind(bindAddr, (err: BusinessError) => { if (err) { console.log('bind fail'); return; } console.log('bind success'); }); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsTwoWay.connect(tlsConnectOptions).then(() => { console.log("connect successfully"); }).catch((err: BusinessError) => { console.log("connect failed " + JSON.stringify(err)); }); let tlsOneWay: socket.TLSSocket = socket.constructTLSSocketInstance(); // One way authentication tlsOneWay.bind(bindAddr, (err: BusinessError) => { if (err) { console.log('bind fail'); return; } console.log('bind success'); }); let tlsOneWayConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { ca: ["xxxx", "xxxx"], cipherSuite: "AES256-SHA256" } } tlsOneWay.connect(tlsOneWayConnectOptions).then(() => { console.log("connect successfully"); }).catch((err: BusinessError) => { console.log("connect failed " + JSON.stringify(err)); }); ``` ### getRemoteAddress9+ getRemoteAddress(callback: AsyncCallback\): void Obtains the remote address of a TLS socket connection. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------------------------- | ---- | ---------- | | 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.| **Error codes** | ID| Error Message | | ------- | ----------------------------- | | 2303188 | Socket operation on non-socket.| | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tls: socket.TLSSocket = socket.constructTLSSocketInstance(); tls.getRemoteAddress((err: BusinessError, data: socket.NetAddress) => { if (err) { console.log('getRemoteAddress fail'); return; } console.log('getRemoteAddress success:' + JSON.stringify(data)); }); ``` ### getRemoteAddress9+ getRemoteAddress(): Promise\ Obtains the remote address of a TLS socket connection. This API uses a promise to return the result. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | ------------------------------------------ | ------------------------------------------ | | Promise\<[NetAddress](#netaddress)> | Promise used to return the result. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | ------- | ------------------------------ | | 2303188 | Socket operation on non-socket.| | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tls: socket.TLSSocket = socket.constructTLSSocketInstance(); tls.getRemoteAddress().then(() => { console.log('getRemoteAddress success'); }).catch((err: BusinessError) => { console.log('getRemoteAddress fail'); }); ``` ### getCertificate9+ getCertificate(callback: AsyncCallback\<[X509CertRawData](#x509certrawdata9)\>): void Obtains 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description| | -------- | ----------------------------------------| ---- | ---------------| | 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.| **Error codes** | ID| Error Message | | ------- | ------------------------------ | | 2303501 | SSL is null. | | 2303504 | Error looking up x509. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tls: socket.TLSSocket = socket.constructTLSSocketInstance(); tls.getCertificate((err: BusinessError, data: socket.X509CertRawData) => { if (err) { console.log("getCertificate callback error = " + err); } else { console.log("getCertificate callback = " + data); } }); ``` ### getCertificate9+ getCertificate():Promise\<[X509CertRawData](#x509certrawdata9)\> Obtains 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. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | -------------- | -------------------- | | Promise\<[X509CertRawData](#x509certrawdata9)\> | Promise used to return the result. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | ------- | ------------------------------ | | 2303501 | SSL is null. | | 2303504 | Error looking up x509. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; import util from "@ohos.util"; let tls: socket.TLSSocket = socket.constructTLSSocketInstance(); tls.getCertificate().then((data: socket.X509CertRawData) => { const decoder = util.TextDecoder.create(); const str = decoder.decodeWithStream(data.data); console.log("getCertificate: " + str); }).catch((err: BusinessError) => { console.error("failed" + err); }); ``` ### getRemoteCertificate9+ getRemoteCertificate(callback: AsyncCallback\<[X509CertRawData](#x509certrawdata9)\>): void Obtains the digital certificate of the server after a TLS socket connection is established. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------------------------| ---- | ---------------| | callback | AsyncCallback\<[X509CertRawData](#x509certrawdata9)\> | Yes | Callback used to return the result. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | ------- | ------------------------------ | | 2303501 | SSL is null. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tls: socket.TLSSocket = socket.constructTLSSocketInstance(); tls.getRemoteCertificate((err: BusinessError, data: socket.X509CertRawData) => { if (err) { console.log("getRemoteCertificate callback error = " + err); } else { console.log("getRemoteCertificate callback = " + data); } }); ``` ### getRemoteCertificate9+ getRemoteCertificate():Promise\<[X509CertRawData](#x509certrawdata9)\> Obtains the digital certificate of the server after a TLS socket connection is established. This API uses a promise to return the result. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | -------------- | -------------------- | | Promise\<[X509CertRawData](#x509certrawdata9)\> | Promise used to return the result. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | ------- | ------------------------------ | | 2303501 | SSL is null. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tls: socket.TLSSocket = socket.constructTLSSocketInstance(); tls.getRemoteCertificate().then((data: socket.X509CertRawData) => { console.log(data); }).catch((err: BusinessError) => { console.error("failed" + err); }); ``` ### getProtocol9+ getProtocol(callback: AsyncCallback\): void Obtains the communication protocol version after a TLS socket connection is established. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ----------------------------------------| ---- | ---------------| | callback | AsyncCallback\ | Yes | Callback used to return the result. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | ------- | ----------------------------- | | 2303501 | SSL is null. | | 2303505 | Error occurred in the tls system call. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tls: socket.TLSSocket = socket.constructTLSSocketInstance(); tls.getProtocol((err: BusinessError, data: string) => { if (err) { console.log("getProtocol callback error = " + err); } else { console.log("getProtocol callback = " + data); } }); ``` ### getProtocol9+ getProtocol():Promise\ Obtains the communication protocol version after a TLS socket connection is established. This API uses a promise to return the result. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | -------------- | -------------------- | | Promise\ | Promise used to return the result. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | ------- | ------------------------------ | | 2303501 | SSL is null. | | 2303505 | Error occurred in the tls system call. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tls: socket.TLSSocket = socket.constructTLSSocketInstance(); tls.getProtocol().then((data: string) => { console.log(data); }).catch((err: BusinessError) => { console.error("failed" + err); }); ``` ### getCipherSuite9+ getCipherSuite(callback: AsyncCallback\\>): void Obtains 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description| | -------- | ----------------------------------------| ---- | ---------------| | callback | AsyncCallback\\> | Yes | Callback used to return the result. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | ------- | ------------------------------ | | 2303501 | SSL is null. | | 2303502 | Error in tls reading. | | 2303505 | Error occurred in the tls system call. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tls: socket.TLSSocket = socket.constructTLSSocketInstance(); tls.getCipherSuite((err: BusinessError, data: Array) => { if (err) { console.log("getCipherSuite callback error = " + err); } else { console.log("getCipherSuite callback = " + data); } }); ``` ### getCipherSuite9+ getCipherSuite(): Promise\\> Obtains the cipher suite negotiated by both communication parties after a TLS socket connection is established. This API uses a promise to return the result. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | ---------------------- | --------------------- | | Promise\\> | Promise used to return the result. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | ------- | ------------------------------ | | 2303501 | SSL is null. | | 2303502 | Error in tls reading. | | 2303505 | Error occurred in the tls system call. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tls: socket.TLSSocket = socket.constructTLSSocketInstance(); tls.getCipherSuite().then((data: Array) => { console.log('getCipherSuite success:' + JSON.stringify(data)); }).catch((err: BusinessError) => { console.error("failed" + err); }); ``` ### getSignatureAlgorithms9+ getSignatureAlgorithms(callback: AsyncCallback\\>): void Obtains 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | -------------------------------------| ---- | ---------------| | callback | AsyncCallback\\> | Yes | Callback used to return the result. | **Error codes** | ID| Error Message | | ------- | ------------------------------ | | 2303501 | SSL is null. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tls: socket.TLSSocket = socket.constructTLSSocketInstance(); tls.getSignatureAlgorithms((err: BusinessError, data: Array) => { if (err) { console.log("getSignatureAlgorithms callback error = " + err); } else { console.log("getSignatureAlgorithms callback = " + data); } }); ``` ### getSignatureAlgorithms9+ getSignatureAlgorithms(): Promise\\> Obtains 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. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | ---------------------- | -------------------- | | Promise\\> | Promise used to return the result.| **Error codes** | ID| Error Message | | ------- | ------------------------------ | | 2303501 | SSL is null. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tls: socket.TLSSocket = socket.constructTLSSocketInstance(); tls.getSignatureAlgorithms().then((data: Array) => { console.log("getSignatureAlgorithms success" + data); }).catch((err: BusinessError) => { console.error("failed" + err); }); ``` ### send9+ send(data: string, callback: AsyncCallback\): void Sends a message to the server after a TLS socket connection is established. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | -----------------------------| ---- | ---------------| | data | string | Yes | Data content of the message to send. | | callback | AsyncCallback\ | Yes | Callback used to return the result. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | ------- | -------------------------------------------- | | 401 | Parameter error. | | 2303501 | SSL is null. | | 2303503 | Error in tls writing. | | 2303505 | Error occurred in the tls system call. | | 2303506 | Error clearing tls connection. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tls: socket.TLSSocket = socket.constructTLSSocketInstance(); tls.send("xxxx", (err: BusinessError) => { if (err) { console.log("send callback error = " + err); } else { console.log("send success"); } }); ``` ### send9+ send(data: string): Promise\ Sends a message to the server after a TLS socket connection is established. This API uses a promise to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | -----------------------------| ---- | ---------------| | data | string | Yes | Data content of the message to send. | **Error codes** | ID| Error Message | | ------- | -------------------------------------------- | | 401 | Parameter error. | | 2303501 | SSL is null. | | 2303503 | Error in tls writing. | | 2303505 | Error occurred in the tls system call. | | 2303506 | Error clearing tls connection. | | 2300002 | System internal error. | **Return value** | Type | Description | | -------------- | -------------------- | | Promise\ | Promise used to return the result. If the operation fails, an error message is returned.| **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tls: socket.TLSSocket = socket.constructTLSSocketInstance(); tls.send("xxxx").then(() => { console.log("send success"); }).catch((err: BusinessError) => { console.error("failed" + err); }); ``` ### close9+ close(callback: AsyncCallback\): void Closes a TLS socket connection. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | -----------------------------| ---- | ---------------| | callback | AsyncCallback\ | Yes | Callback used to return the result. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | ------- | -------------------------------------------- | | 401 | Parameter error. | | 2303501 | SSL is null. | | 2303505 | Error occurred in the tls system call. | | 2303506 | Error clearing tls connection. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tls: socket.TLSSocket = socket.constructTLSSocketInstance(); tls.close((err: BusinessError) => { if (err) { console.log("close callback error = " + err); } else { console.log("close success"); } }); ``` ### close9+ close(): Promise\ Closes a TLS socket connection. This API uses a promise to return the result. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | -------------- | -------------------- | | Promise\ | Promise used to return the result. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | ------- | -------------------------------------------- | | 401 | Parameter error. | | 2303501 | SSL is null. | | 2303505 | Error occurred in the tls system call. | | 2303506 | Error clearing tls connection. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tls: socket.TLSSocket = socket.constructTLSSocketInstance(); tls.close().then(() => { console.log("close success"); }).catch((err: BusinessError) => { console.error("failed" + err); }); ``` ## TLSConnectOptions9+ Defines TLS connection options. **System capability**: SystemCapability.Communication.NetStack | Name | Type | Mandatory| Description | | -------------- | ------------------------------------- | --- |-------------- | | address | [NetAddress](#netaddress) | Yes | Gateway address. | | secureOptions | [TLSSecureOptions](#tlssecureoptions9) | Yes| TLS security options.| | ALPNProtocols | Array\ | No| ALPN protocol. The value range is ["spdy/1", "http/1.1"]. The default value is **[]**. | ## TLSSecureOptions9+ Defines TLS security options. The CA certificate is mandatory, and other parameters are optional. 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. **System capability**: SystemCapability.Communication.NetStack | Name | Type | Mandatory| Description | | --------------------- | ------------------------------------------------------ | --- |----------------------------------- | | ca | string \| Array\ | Yes| CA certificate of the server, which is used to authenticate the digital certificate of the server.| | cert | string | No| Digital certificate of the local client. | | key | string | No| Private key of the local digital certificate. | | password | string | No| Password for reading the private key. | | protocols | [Protocol](#protocol9) \|Array\<[Protocol](#protocol9)\> | No| TLS protocol version. The default value is **TLSv1.2**. | | useRemoteCipherPrefer | boolean | No| Whether to use the remote cipher suite preferentially. | | signatureAlgorithms | string | No| Signing algorithm used during communication. The default value is **""**. | | cipherSuite | string | No| Cipher suite used during communication. The default value is **""**. | ## Protocol9+ Enumerates TLS protocol versions. **System capability**: SystemCapability.Communication.NetStack | Name | Value | Description | | --------- | --------- |------------------ | | TLSv12 | "TLSv1.2" | TLSv1.2.| | TLSv13 | "TLSv1.3" | TLSv1.3.| ## X509CertRawData9+ Defines the certificate raw data. **System capability**: SystemCapability.Communication.NetStack ## socket.constructTLSSocketServerInstance10+ constructTLSSocketServerInstance(): TLSSocketServer Creates a **TLSSocketServer** object. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | ------------------------------------ | ---------------------------- | | [TLSSocketServer](#tlssocketserver10) | **TLSSocketServer** object.| **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); ``` ## TLSSocketServer10+ Defines a TLS socket server connection. Before calling TLSSocketServer APIs, you need to call [socket.constructTLSSocketServerInstance](#socketconstructtlssocketserverinstance10) to create a **TLSSocketServer** object. ### listen10+ listen(options: TLSConnectOptions, callback: AsyncCallback\): void Listens 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. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------------------------------- | ---- | ------------------------------------------------ | | options | [TLSConnectOptions](#tlsconnectoptions9) | Yes | Parameters required for the connection. | | callback | AsyncCallback\ | 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.| **Error codes** | ID| Error Message | | -------- | ------------------------------------------- | | 401 | Parameter error. | | 201 | Permission denied. | | 2300002 | System internal error. | | 2303109 | Bad file number. | | 2303111 | Resource temporarily unavailable try again. | | 2303198 | Address already in use. | | 2303199 | Cannot assign requested address. | | 2303501 | SSL is null. | | 2303502 | Error in tls reading. | | 2303503 | Error in tls writing | | 2303505 | Error occurred in the tls system call. | | 2303506 | Error clearing tls connection. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions, (err: BusinessError) => { console.log("listen callback error" + err); }); ``` ### listen10+ listen(options: TLSConnectOptions): Promise\ Listens 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. **Required permissions**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ------- | ---------------------------------------- | ---- | ------------------ | | options | [TLSConnectOptions](#tlsconnectoptions9) | Yes | Parameters required for the connection.| **Return value** | Type | Description | | --------------- | --------------------------------------------------------- | | Promise\ | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | -------- | ------------------------------------------- | | 401 | Parameter error. | | 201 | Permission denied. | | 2300002 | System internal error. | | 2303109 | Bad file number. | | 2303111 | Resource temporarily unavailable try again. | | 2303198 | Address already in use. | | 2303199 | Cannot assign requested address. | | 2303501 | SSL is null. | | 2303502 | Error in tls reading. | | 2303503 | Error in tls writing | | 2303505 | Error occurred in the tls system call. | | 2303506 | Error clearing tls connection. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed: " + JSON.stringify(err)); }); ``` ### getState10+ getState(callback: AsyncCallback\): void Obtains the status of the TLS socket server connection upon successful listening. This API uses an asynchronous callback to return the result. > **NOTE** > This API can be called only after **listen** is successfully called. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------------------------------------------- | ---- | ------------------------------------------------------------ | | 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.| **Error codes** | ID| Error Message | | -------- | ------------------------------- | | 401 | Parameter error. | | 2303188 | Socket operation on non-socket. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed: " + JSON.stringify(err)); }); tlsServer.getState((err: BusinessError, data: socket.SocketStateBase) => { if (err) { console.log('getState fail'); return; } console.log('getState success:' + JSON.stringify(data)); }); ``` ### getState10+ getState(): Promise\ Obtains the status of the TLS socket server connection upon successful listening. This API uses a promise to return the result. > **NOTE** > This API can be called only after **listen** is successfully called. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | --------------------------------------------- | ----------------------------------------------------------- | | Promise\<[SocketStateBase](#socketstatebase)> | Promise used to return the result. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | -------- | ------------------------------- | | 2303188 | Socket operation on non-socket. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed: " + JSON.stringify(err)); }); tlsServer.getState().then(() => { console.log('getState success'); }).catch((err: BusinessError) => { console.log('getState fail'); }); ``` ### setExtraOptions10+ setExtraOptions(options: TCPExtraOptions, callback: AsyncCallback\): void Sets other properties of the TLS socket server connection upon successful listening. This API uses an asynchronous callback to return the result. > **NOTE** > This API can be called only after **listen** is successfully called. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------------ | ---- | ------------------------------------------------ | | options | [TCPExtraOptions](#tcpextraoptions) | Yes | Other properties of the TLS socket server connection. | | callback | AsyncCallback\ | 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.| **Error codes** | ID| Error Message | | -------- | ------------------------------- | | 401 | Parameter error. | | 2303188 | Socket operation on non-socket. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed: " + JSON.stringify(err)); }); let tcpExtraOptions: socket.TCPExtraOptions = { keepAlive: true, OOBInline: true, TCPNoDelay: true, socketLinger: { on: true, linger: 10 }, receiveBufferSize: 1000, sendBufferSize: 1000, reuseAddress: true, socketTimeout: 3000 } tlsServer.setExtraOptions(tcpExtraOptions, (err: BusinessError) => { if (err) { console.log('setExtraOptions fail'); return; } console.log('setExtraOptions success'); }); ``` ### setExtraOptions10+ setExtraOptions(options: TCPExtraOptions): Promise\ Sets other properties of the TLS socket server connection upon successful listening. This API uses a promise to return the result. > **NOTE** > This API can be called only after **listen** is successfully called. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | ------- | ------------------------------------ | ---- | ------------------------------- | | options | [TCPExtraOptions](#tcpextraoptions) | Yes | Other properties of the TLS socket server connection.| **Return value** | Type | Description | | -------------- | -------------------------------------------------------- | | Promise\ | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | -------- | ------------------------------- | | 401 | Parameter error. | | 2303188 | Socket operation on non-socket. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed: " + JSON.stringify(err)); }); let tcpExtraOptions: socket.TCPExtraOptions = { keepAlive: true, OOBInline: true, TCPNoDelay: true, socketLinger: { on: true, linger: 10 }, receiveBufferSize: 1000, sendBufferSize: 1000, reuseAddress: true, socketTimeout: 3000 } tlsServer.setExtraOptions(tcpExtraOptions).then(() => { console.log('setExtraOptions success'); }).catch((err: BusinessError) => { console.log('setExtraOptions fail'); }); ``` ### getCertificate10+ getCertificate(callback: AsyncCallback\<[X509CertRawData](#x509certrawdata9)\>): void Obtains the local digital certificate after a TLS socket server connection is established. This API uses an asynchronous callback to return the result. > **NOTE** > This API can be called only after **listen** is successfully called. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ----------------------------------------------------- | ---- | -------------------------------------------------------- | | 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.| **Error codes** | ID| Error Message | | -------- | ---------------------- | | 401 | Parameter error. | | 2303501 | SSL is null. | | 2303504 | Error looking up x509. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; import util from "@ohos.util"; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed: " + JSON.stringify(err)); }); tlsServer.getCertificate((err: BusinessError, data: socket.X509CertRawData) => { if (err) { console.log("getCertificate callback error = " + err); } else { const decoder = util.TextDecoder.create(); const str = decoder.decodeWithStream(data.data); console.log("getCertificate callback: " + str); } }); ``` ### getCertificate10+ getCertificate():Promise\<[X509CertRawData](#x509certrawdata9)\> Obtains the local digital certificate after a TLS socket server connection is established. This API uses a promise to return the result. > **NOTE** > This API can be called only after **listen** is successfully called. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | ----------------------------------------------- | ------------------------------------------------------------ | | Promise\<[X509CertRawData](#x509certrawdata9)\> | Promise used to return the result. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | -------- | ---------------------- | | 2303501 | SSL is null. | | 2303504 | Error looking up x509. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; import util from "@ohos.util"; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed: " + JSON.stringify(err)); }); tlsServer.getCertificate().then((data: socket.X509CertRawData) => { const decoder = util.TextDecoder.create(); const str = decoder.decodeWithStream(data.data); console.log("getCertificate: " + str); }).catch((err: BusinessError) => { console.error("failed" + err); }); ``` ### getProtocol10+ getProtocol(callback: AsyncCallback\): void Obtains the communication protocol version after a TLS socket server connection is established. This API uses an asynchronous callback to return the result. > **NOTE** > This API can be called only after **listen** is successfully called. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ----------------------- | ---- | ---------------------------------------------------- | | callback | AsyncCallback\ | Yes | Callback used to return the result. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | -------- | -------------------------------------- | | 401 | Parameter error. | | 2303501 | SSL is null. | | 2303505 | Error occurred in the tls system call. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed: " + JSON.stringify(err)); }); tlsServer.getProtocol((err: BusinessError, data: string) => { if (err) { console.log("getProtocol callback error = " + err); } else { console.log("getProtocol callback = " + data); } }); ``` ### getProtocol10+ getProtocol():Promise\ Obtains the communication protocol version after a TLS socket server connection is established. This API uses a promise to return the result. > **NOTE** > This API can be called only after **listen** is successfully called. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | ----------------- | ------------------------------------------------------- | | Promise\ | Promise used to return the result. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | -------- | -------------------------------------- | | 2303501 | SSL is null. | | 2303505 | Error occurred in the tls system call. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed: " + JSON.stringify(err)); }); tlsServer.getProtocol().then((data: string) => { console.log(data); }).catch((err: BusinessError) => { console.error("failed" + err); }); ``` ### on('connect')10+ on(type: 'connect', callback: Callback\): void Subscribes to TLS socket server connection events. This API uses an asynchronous callback to return the result. > **NOTE** > This API can be called only after **listen** is successfully called. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------------------------------- | ---- | ------------------------------------- | | type | string | Yes | Event type.
**connect**: connection event.| | callback | Callback<[TLSSocketConnection](#tlssocketconnection10)> | Yes | Callback used to return the result. | **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed: " + JSON.stringify(err)); }); tlsServer.on('connect', (data: socket.TLSSocketConnection) => { console.log(JSON.stringify(data)) }); ``` ### off('connect')10+ off(type: 'connect', callback?: Callback\): void Unsubscribes from **connect** events of a **TLSSocketServer** object. This API uses an asynchronous callback to return the result. > **NOTE** > This API can be called only after **listen** is successfully called. > 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------------------------------- | ---- | ------------------------------------- | | type | string | Yes | Event type.
**connect**: connection event.| | callback | Callback<[TLSSocketConnection](#tlssocketconnection10)> | No | Callback used to return the result. | **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed: " + JSON.stringify(err)); }); let callback = (data: socket.TLSSocketConnection) => { console.log('on connect message: ' + JSON.stringify(data)); } tlsServer.on('connect', callback); // 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. tlsServer.off('connect', callback); tlsServer.off('connect'); ``` ### on('error')10+ on(type: 'error', callback: ErrorCallback): void Subscribes to **error** events of a **TLSSocketServer** object. This API uses an asynchronous callback to return the result. > **NOTE** > This API can be called only after **listen** is successfully called. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------- | ---- | ------------------------------------ | | type | string | Yes | Event type.
**error**: error event.| | callback | ErrorCallback | Yes | Callback used to return the result. | **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed: " + JSON.stringify(err)); }); tlsServer.on('error', (err: BusinessError) => { console.log("on error, err:" + JSON.stringify(err)) }); ``` ### off('error')10+ off(type: 'error', callback?: ErrorCallback): void Unsubscribes from **error** events of a **TLSSocketServer** object. This API uses an asynchronous callback to return the result. > **NOTE** > This API can be called only after **listen** is successfully called. > 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------- | ---- | ------------------------------------ | | type | string | Yes | Event type.
**error**: error event.| | callback | ErrorCallback | No | Callback used to return the result. | **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed: " + JSON.stringify(err)); }); let callback = (err: BusinessError) => { console.log("on error, err:" + JSON.stringify(err)); } tlsServer.on('error', callback); // 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. tlsServer.off('error', callback); tlsServer.off('error'); ``` ## TLSSocketConnection10+ Defines 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. > **NOTE** > 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. **System capability**: SystemCapability.Communication.NetStack ### Attributes | Name | Type | Mandatory| Description | | -------- | ------ | ---- | ------------------------------------- | | clientId | number | Yes | ID of the connection between the client and TLSSocketServer.| ### send10+ send(data: string, callback: AsyncCallback\): void Sends a message to the client after a TLS socket server connection is established. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | --------------------- | ---- | ------------------------------------------------ | | data | string | Yes | Parameters for sending data over a TLS socket server connection. | | callback | AsyncCallback\ | 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.| **Error codes** | ID| Error Message | | -------- | -------------------------------------- | | 401 | Parameter error. | | 2303501 | SSL is null. | | 2303503 | Error in tls writing. | | 2303505 | Error occurred in the tls system call. | | 2303506 | Error clearing tls connection. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed" + err); }); tlsServer.on('connect', (client: socket.TLSSocketConnection) => { client.send('Hello, client!', (err: BusinessError) => { if (err) { console.log('send fail'); return; } console.log('send success'); }); }); ``` ## SocketMessageInfo11+ Defines the socket connection information. **System capability**: SystemCapability.Communication.NetStack | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ------------------------------------- | | message | ArrayBuffer | Yes | Received **message** event.| | remoteInfo | [SocketRemoteInfo](#socketremoteinfo) | Yes | Socket connection information.| ### send10+ send(data: string): Promise\ Sends a message to the server after a TLS socket server connection is established. This API uses a promise to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ------------------------------------- | | data | string | Yes | Parameters for sending data over a TLS socket server connection.| **Return value** | Type | Description | | --------------- | --------------------------------------------------------- | | Promise\ | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | -------- | -------------------------------------- | | 401 | Parameter error. | | 2303501 | SSL is null. | | 2303503 | Error in tls writing. | | 2303505 | Error occurred in the tls system call. | | 2303506 | Error clearing tls connection. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed" + err); }); tlsServer.on('connect', (client: socket.TLSSocketConnection) => { client.send('Hello, client!').then(() => { console.log('send success'); }).catch((err: BusinessError) => { console.log('send fail'); }); }); ``` ### close10+ close(callback: AsyncCallback\): void Closes a TLS socket server connection. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | --------------------- | ---- | ------------------------------------------------ | | callback | AsyncCallback\ | 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.| **Error codes** | ID| Error Message | | -------- | -------------------------------------- | | 401 | Parameter error. | | 2303501 | SSL is null. | | 2303505 | Error occurred in the tls system call. | | 2303506 | Error clearing tls connection. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed" + err); }); tlsServer.on('connect', (client: socket.TLSSocketConnection) => { client.close((err: BusinessError) => { if (err) { console.log('close fail'); return; } console.log('close success'); }); }); ``` ### close10+ close(): Promise\ Closes a TLS socket server connection. This API uses a promise to return the result. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | --------------- | --------------------------------------------------------- | | Promise\ | 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.| **Error codes** | ID| Error Message | | -------- | -------------------------------------- | | 2303501 | SSL is null. | | 2303505 | Error occurred in the tls system call. | | 2303506 | Error clearing tls connection. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed" + err); }); tlsServer.on('connect', (client: socket.TLSSocketConnection) => { client.close().then(() => { console.log('close success'); }).catch((err: BusinessError) => { console.log('close fail'); }); }); ``` ### getRemoteAddress10+ getRemoteAddress(callback: AsyncCallback\): void Obtains the remote address of a TLS socket server connection. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | | 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.| **Error codes** | ID| Error Message | | -------- | ------------------------------- | | 401 | Parameter error. | | 2303188 | Socket operation on non-socket. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed" + err); }); tlsServer.on('connect', (client: socket.TLSSocketConnection) => { client.getRemoteAddress((err: BusinessError, data: socket.NetAddress) => { if (err) { console.log('getRemoteAddress fail'); return; } console.log('getRemoteAddress success:' + JSON.stringify(data)); }); }); ``` ### getRemoteAddress10+ getRemoteAddress(): Promise\ Obtains the remote address of a TLS socket server connection. This API uses a promise to return the result. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | ----------------------------------- | ----------------------------------------------------------- | | Promise\<[NetAddress](#netaddress)> | Promise used to return the result. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | -------- | ------------------------------- | | 2303188 | Socket operation on non-socket. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed" + err); }); tlsServer.on('connect', (client: socket.TLSSocketConnection) => { client.getRemoteAddress().then((data: socket.NetAddress) => { console.log('getRemoteAddress success:' + JSON.stringify(data)); }).catch((err: BusinessError) => { console.error("failed" + err); }); }); ``` ### getRemoteCertificate10+ getRemoteCertificate(callback: AsyncCallback\<[X509CertRawData](#x509certrawdata9)\>): void Obtains 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ----------------------------------------------------- | ---- | ---------------------------------------------------- | | callback | AsyncCallback\<[X509CertRawData](#x509certrawdata9)\> | Yes | Callback used to return the result. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | -------- | ---------------------- | | 401 | Parameter error. | | 2303501 | SSL is null. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; import util from "@ohos.util"; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed" + err); }); tlsServer.on('connect', (client: socket.TLSSocketConnection) => { client.getRemoteCertificate((err: BusinessError, data: socket.X509CertRawData) => { if (err) { console.log("getRemoteCertificate callback error: " + err); } else { const decoder = util.TextDecoder.create(); const str = decoder.decodeWithStream(data.data); console.log("getRemoteCertificate callback: " + str); } }); }); ``` ### getRemoteCertificate10+ getRemoteCertificate():Promise\<[X509CertRawData](#x509certrawdata9)\> Obtains 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. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | ----------------------------------------------- | ------------------------------------------------------------ | | Promise\<[X509CertRawData](#x509certrawdata9)\> | Promise used to return the result. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | -------- | ---------------------- | | 2303501 | SSL is null. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; import util from "@ohos.util"; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed" + err); }); tlsServer.on('connect', (client: socket.TLSSocketConnection) => { client.getRemoteCertificate().then((data: socket.X509CertRawData) => { const decoder = util.TextDecoder.create(); const str = decoder.decodeWithStream(data.data); console.log("getRemoteCertificate success: " + str); }).catch((err: BusinessError) => { console.error("failed" + err); }); }); ``` ### getCipherSuite10+ getCipherSuite(callback: AsyncCallback\\>): void Obtains 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | -------------------------------- | ---- | ------------------------------------------------------------ | | callback | AsyncCallback\\> | Yes | Callback used to return the result. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | -------- | -------------------------------------- | | 401 | Parameter error. | | 2303501 | SSL is null. | | 2303502 | Error in tls reading. | | 2303505 | Error occurred in the tls system call. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed" + err); }); tlsServer.on('connect', (client: socket.TLSSocketConnection) => { client.getCipherSuite((err: BusinessError, data: Array) => { if (err) { console.log("getCipherSuite callback error = " + err); } else { console.log("getCipherSuite callback = " + data); } }); }); ``` ### getCipherSuite10+ getCipherSuite(): Promise\\> Obtains 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. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | -------------------------- | ------------------------------------------------------------ | | Promise\\> | Promise used to return the result. If the operation fails, an error message is returned.| **Error codes** | ID| Error Message | | -------- | -------------------------------------- | | 2303501 | SSL is null. | | 2303502 | Error in tls reading. | | 2303505 | Error occurred in the tls system call. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed" + err); }); tlsServer.on('connect', (client: socket.TLSSocketConnection) => { client.getCipherSuite().then((data: Array) => { console.log('getCipherSuite success:' + JSON.stringify(data)); }).catch((err: BusinessError) => { console.error("failed" + err); }); }); ``` ### getSignatureAlgorithms10+ getSignatureAlgorithms(callback: AsyncCallback\\>): void Obtains 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | -------------------------------- | ---- | ---------------------------------- | | callback | AsyncCallback\\> | Yes | Callback used to return the result. | **Error codes** | ID| Error Message | | -------- | ---------------------- | | 401 | Parameter error. | | 2303501 | SSL is null. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed" + err); }); tlsServer.on('connect', (client: socket.TLSSocketConnection) => { client.getSignatureAlgorithms((err: BusinessError, data: Array) => { if (err) { console.log("getSignatureAlgorithms callback error = " + err); } else { console.log("getSignatureAlgorithms callback = " + data); } }); }); ``` ### getSignatureAlgorithms10+ getSignatureAlgorithms(): Promise\\> Obtains 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. **System capability**: SystemCapability.Communication.NetStack **Return value** | Type | Description | | -------------------------- | --------------------------------------------- | | Promise\\> | Promise used to return the result.| **Error codes** | ID| Error Message | | -------- | ---------------------- | | 2303501 | SSL is null. | | 2300002 | System internal error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed" + err); }); tlsServer.on('connect', (client: socket.TLSSocketConnection) => { client.getSignatureAlgorithms().then((data: Array) => { console.log("getSignatureAlgorithms success" + data); }).catch((err: BusinessError) => { console.error("failed" + err); }); }); ``` ### on('message')10+ on(type: 'message', callback: Callback\): void Subscribes to **message** events of a **TLSSocketConnection** object. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------------------------------------ | ---- | ----------------------------------------- | | type | string | Yes | Event type.
**message**: message receiving event.| | callback | Callback<[SocketMessageInfo](#socketmessageinfo11)> | Yes | Callback used to return the result. | **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed" + err); }); class SocketInfo { message: ArrayBuffer = new ArrayBuffer(1); remoteInfo: socket.SocketRemoteInfo = {} as socket.SocketRemoteInfo; } tlsServer.on('connect', (client: socket.TLSSocketConnection) => { client.on('message', (value: SocketInfo) => { let messageView = ''; for (let i: number = 0; i < value.message.byteLength; i++) { let uint8Array = new Uint8Array(value.message) let messages = uint8Array[i] let message = String.fromCharCode(messages); messageView += message; } console.log('on message message: ' + JSON.stringify(messageView)); console.log('remoteInfo: ' + JSON.stringify(value.remoteInfo)); }); }); ``` ### off('message')10+ off(type: 'message', callback?: Callback\): void Unsubscribes from **message** events of a **TLSSocketConnection** object. This API uses an asynchronous callback to return the result. > **NOTE** > 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------------------------------------ | ---- | ----------------------------------------- | | type | string | Yes | Event type.
**message**: message receiving event.| | callback | Callback<[SocketMessageInfo](#socketmessageinfo11)> | No | Callback used to return the result. | **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed" + err); }); class SocketInfo { message: ArrayBuffer = new ArrayBuffer(1); remoteInfo: socket.SocketRemoteInfo = {} as socket.SocketRemoteInfo; } let callback = (value: SocketInfo) => { let messageView = ''; for (let i: number = 0; i < value.message.byteLength; i++) { let uint8Array = new Uint8Array(value.message) let messages = uint8Array[i] let message = String.fromCharCode(messages); messageView += message; } console.log('on message message: ' + JSON.stringify(messageView)); console.log('remoteInfo: ' + JSON.stringify(value.remoteInfo)); } tlsServer.on('connect', (client: socket.TLSSocketConnection) => { client.on('message', callback); // 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. client.off('message', callback); client.off('message'); }); ``` ### on('close')10+ on(type: 'close', callback: Callback\): void Subscribes to **close** events of a **TLSSocketConnection** object. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------- | ---- | ----------------------------------- | | type | string | Yes | Event type.
**close**: close event.| | callback | Callback\ | Yes | Callback used to return the result. | **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed" + err); }); tlsServer.on('connect', (client: socket.TLSSocketConnection) => { client.on('close', () => { console.log("on close success") }); }); ``` ### off('close')10+ off(type: 'close', callback?: Callback\): void Unsubscribes from **close** events of a **TLSSocketConnection** object. This API uses an asynchronous callback to return the result. > **NOTE** > 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------- | ---- | ----------------------------------- | | type | string | Yes | Event type.
**close**: close event.| | callback | Callback\ | No | Callback used to return the result. | **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed" + err); }); let callback = () => { console.log("on close success"); } tlsServer.on('connect', (client: socket.TLSSocketConnection) => { client.on('close', callback); // 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. client.off('close', callback); client.off('close'); }); ``` ### on('error')10+ on(type: 'error', callback: ErrorCallback): void Subscribes to **error** events of a **TLSSocketConnection** object. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------- | ---- | ------------------------------------ | | type | string | Yes | Event type.
**error**: error event.| | callback | ErrorCallback | Yes | Callback used to return the result. | **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed" + err); }); tlsServer.on('connect', (client: socket.TLSSocketConnection) => { client.on('error', (err: BusinessError) => { console.log("on error, err:" + JSON.stringify(err)) }); }); ``` ### off('error')10+ off(type: 'error', callback?: ErrorCallback): void Unsubscribes from **error** events of a **TLSSocketConnection** object. This API uses an asynchronous callback to return the result. > **NOTE** > 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. **System capability**: SystemCapability.Communication.NetStack **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------- | ---- | ------------------------------------ | | type | string | Yes | Event type.
**error**: error event.| | callback | ErrorCallback | No | Callback used to return the result. | **Error codes** | ID| Error Message | | -------- | ---------------- | | 401 | Parameter error. | **Example** ```ts import socket from "@ohos.net.socket"; import { BusinessError } from '@ohos.base'; let tlsServer: socket.TLSSocketServer = socket.constructTLSSocketServerInstance(); let tlsConnectOptions: socket.TLSConnectOptions = { address: { address: '192.168.xx.xxx', port: 8080 }, secureOptions: { key: "xxxx", cert: "xxxx", ca: ["xxxx"], password: "xxxx", protocols: socket.Protocol.TLSv12, useRemoteCipherPrefer: true, signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", cipherSuite: "AES256-SHA256" }, ALPNProtocols: ["spdy/1", "http/1.1"] } tlsServer.listen(tlsConnectOptions).then(() => { console.log("listen callback success"); }).catch((err: BusinessError) => { console.log("failed" + err); }); let callback = (err: BusinessError) => { console.log("on error, err:" + JSON.stringify(err)); } tlsServer.on('connect', (client: socket.TLSSocketConnection) => { client.on('error', callback); // 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. client.off('error', callback); client.off('error'); }); ```