1# Socket Connection 2 3 4## Use Cases 5 6Your application can transmit data through Socket connections. Currently, the TCP and UDP protocols are supported. 7 8 9## Available APIs 10 11The Socket connection function is mainly implemented by the Socket module. The following table describes the related APIs. 12 13| API| Description | 14| -------- | -------- | 15| constructUDPSocketInstance() | Creates a **UDPSocket** object. | 16| constructTCPSocketInstance() | Creates a **TCPSocket** object. | 17| bind() | Binds the IP address and port number. | 18| send() | Sends data.| 19| close() | Closes a Socket connection. | 20| getState() | Obtains the Socket connection status. | 21| connect() | Connects to the specified IP address and port. This function is supported only for TCP. | 22| getRemoteAddress() | Obtains the peer address of the Socket connection. This function is supported only for TCP. The **connect** API must have been called before you use this API. | 23| on(type: 'message') | Enables listening for **message** events of the Socket connection. | 24| off(type: 'message') | Disables listening for **message** events of the Socket connection. | 25| on(type: 'close') | Enables listening for **close** events of the Socket connection. | 26| off(type: 'close') | Disables listening for **close** events of the Socket connection. | 27| on(type: 'error') | Enables listening for **error** events of the Socket connection. | 28| off(type: 'error') | Disables listening for **error** events of the Socket connection. | 29| on(type: 'listening') | Enables listening for **listening** events of the UDPSocket connection. | 30| off(type: 'listening') | Disables listening for **listening** events of the UDPSocket connection. | 31| on(type: 'connect') | Enables listening for **connect** events of the TCPSocket connection. | 32| off(type: 'connect') | Disables listening for **connect** events of the TCPSocket connection. | 33 34 35## How to Develop 36 37The implementation is similar for UDPSocket and TCPSocket. The following uses the TCPSocket as an example. 38 391. Import the required Socket module. 40 412. Create a **TCPSocket** object. 42 433. (Optional) Enable listening for TCPSocket events. 44 454. Bind the IP address and port number. The port number can be specified or randomly allocated by the system. 46 475. Set up a connection to the specified IP address and port number. 48 496. Send data. 50 517. Enable the TCPSocket connection to be automatically closed after use. 52 53 ```js 54 import socket from '@ohos.net.socket' 55 56 // Create a TCPSocket object. 57 let tcp = socket.constructTCPSocketInstance(); 58 59 // Enable listening for TCPSocket events. 60 tcp.on('message', value => { 61 console.log("on message") 62 let buffer = value.message 63 let dataView = new DataView(buffer) 64 let str = "" 65 for (let i = 0;i < dataView.byteLength; ++i) { 66 str += String.fromCharCode(dataView.getUint8(i)) 67 } 68 console.log("on connect received:" + str) 69 }); 70 tcp.on('connect', () => { 71 console.log("on connect") 72 }); 73 tcp.on('close', () => { 74 console.log("on close") 75 }); 76 77 // Bind the IP address and port number. 78 let bindAddress = { 79 address: '192.168.xx.xx', 80 port: 1234, // Bound port, for example, 1234. 81 family: 1 82 }; 83 tcp.bind(bindAddress, err => { 84 if (err) { 85 console.log('bind fail'); 86 return; 87 } 88 console.log('bind success'); 89 // Set up a connection to the specified IP address and port number. 90 let connectAddress = { 91 address: '192.168.xx.xx', 92 port: 5678, // Connection port, for example, 5678. 93 family: 1 94 }; 95 tcp.connect({ 96 address: connectAddress, timeout: 6000 97 }, err => { 98 if (err) { 99 console.log('connect fail'); 100 return; 101 } 102 console.log('connect success'); 103 // Send data. 104 tcp.send({ 105 data: 'Hello, server!' 106 }, err => { 107 if (err) { 108 console.log('send fail'); 109 return; 110 } 111 console.log('send success'); 112 }) 113 }); 114 }); 115 // Enable the TCPSocket connection to be automatically closed after use. Then, disable listening for TCPSocket events. 116 setTimeout(() => { 117 tcp.close((err) => { 118 console.log('close socket.') 119 }); 120 tcp.off('message'); 121 tcp.off('connect'); 122 tcp.off('close'); 123 }, 30 * 1000); 124 ``` 125