5 The following shows net module APIs available for each platform.
7 | | Linux<br/>(Ubuntu) | Raspbian<br/>(Raspberry Pi) | Nuttx<br/>(STM32F4-Discovery) |
8 | :---: | :---: | :---: | :---: |
9 | net.createServer | O | O | O |
10 | net.connect | O | O | O |
11 | net.createConnection | O | O | O |
12 | net.Server.listen | O | O | O |
13 | net.Server.close | O | O | O |
14 | net.Socket.connect | O | O | O |
15 | net.Socket.write | O | O | O |
16 | net.Socket.end | O | O | O |
17 | net.Socket.destroy | O | O | O |
18 | net.Socket.pause | O | O | O |
19 | net.Socket.resume | O | O | O |
20 | net.Socket.setTimeout | O | O | O |
21 | net.Socket.setKeepAlive | X | X | X |
23 ※ When writable stream is finished but readable stream is still alive, IoT.js tries to shutdown the socket, not destroy.
24 However on `nuttx` due to lack of implementation, it does nothing inside.
26 IoT.js provides asynchronous networking through Net module.
28 You can use this module with `require('net')` and create both servers and clients.
32 #### net.connect(options[, connectListener])
33 #### net.connect(port[, host][, connectListener])
34 #### net.createConnection(options[, connectListener])
35 #### net.createConnection(port[, host][, connectListener])
38 * `host: String`, Default: `localhost`
39 * `connectListener: Function()`
41 Creates a `net.Socket` and connects to the supplied host.
43 It is equivalent to `new net.Socket()` followed by `socket.connect()`.
46 #### net.createServer([options][, connectionListener])
48 * `connectionListener: Function(connection: net.Socket)`
50 Creates a TCP server according to `options`.
52 `connectionListener` is automatically registered as `connection` event listener.
59 You can create `net.Server` instance with `net.createServer()`.
65 * `callback: Function()`
67 Emitted when server closed.
69 Note that this event will be emitted after all existing connections are closed.
72 #### `'connection(socket)'`
73 * `callback: Function(socket)`
74 * `socket: net.Socket`
76 Emitted when new connection is established.
80 * `callback: Function()`
82 Emitted when an error occurs.
86 * `callback: Function()`
88 Emitted when server has been started listening.
93 #### server.close([closeListener])
94 * `closeListener: Function()`
96 Stops listening new arriving connection.
98 Server socket will finally close when all existing connections are closed, then emit 'close' event.
100 `closeListener` is registered as `close` event listener.
103 #### server.listen(port[, host][, backlog][, listenListener])
104 #### server.listen(options[, listenListener])
108 * `listenListener: Function()`
110 Starts listening and accepting connections on specified port and host.
117 net.Socket inherits [`Stream.Duplex`](IoT.js-API-Stream.md)
121 #### new net.Socket([options])
124 Creates a new socket object.
126 `options` is an object specifying following information:
128 * `allowHalfOpen: Boolean`
136 * `callback: Function()`
138 Emitted after connection is established.
142 * `callback: Function()`
144 Emitted when the socket closed.
148 * `callback: Function(data)`
149 * `data: Buffer | String`
151 Emitted when data is received from the connection.
155 * `callback: Function()`
157 Emitted when the write buffer becomes empty.
161 * `callback: Function()`
163 Emitted when FIN packet received.
167 * `callback: Function()`
169 Emitted when an error occurs.
173 * `callback: Function(err, address, family)`
174 * `err: Error | Null`
176 * `family: String | Null`
178 Emitted after resolving hostname.
182 * `callback: Function()`
184 Emitted when the connection remains idle for specified timeout.
189 #### socket.connect(options[, connectListener])
190 #### socket.connect(port[, host][, connectListener])
193 * 'host: String`, Default: `'localhost'`
195 Opens the connection with supplied port and host.
197 `options` is an object specifying following information:
198 * `port: Number` - port connect to (required)
199 * `host: String` - host connect to (optional, default: `'127.0.0.1'`)
201 `connectionListener` is automatically registered as `connect` event listener which will be emitted when the connection is established.
204 #### socket.destroy()
209 #### socket.end([data][, callback])
211 * `data: String | Buffer`
212 * `callback: Function()`
214 Half-closes the socket.
216 If `data` is given it is equivalent to `socket.write(data)` followed by `socket.end()`.
218 * `data: String | Buffer`
228 Resumes reading data after a call to `pause()`.
231 #### socket.setKeepAlive([enable][, initialDelay])
234 * `initialDelay: Number`, Default: `0`
236 Enables or disables keep-alive functionality.
239 #### socket.setTimeout(timeout[, callback])
241 * `callback: Function()`
243 Sets timeout for the socket.
245 If the socket is inactive for `timeout` milliseconds, `'timeout'` event will emit.
247 `callback` is registered as `timeout` event listener.
250 #### socket.write(data[, callback])
251 * `data: String | Buffer`
252 * `callback: Function()`
254 Sends `data` on the socket.
256 `callback` function will be called after given data is flushed through the connection.