Skip to content

Latest commit

Β 

History

History
1185 lines (920 loc) Β· 37.2 KB

File metadata and controls

1185 lines (920 loc) Β· 37.2 KB
Β 
Feb 10, 2017
Feb 10, 2017
1
# Net
Nov 14, 2010
Nov 14, 2010
2
Aug 28, 2017
Aug 28, 2017
3
<!--introduced_in=v0.10.0-->
Feb 23, 2018
Feb 23, 2018
4
<!--lint disable maximum-line-length-->
Aug 28, 2017
Aug 28, 2017
5
Aug 4, 2016
Aug 4, 2016
6
> Stability: 2 - Stable
Mar 4, 2012
Mar 4, 2012
7
Mar 4, 2017
Mar 4, 2017
8
The `net` module provides an asynchronous network API for creating stream-based
Mar 11, 2017
Mar 11, 2017
9
TCP or [IPC][] servers ([`net.createServer()`][]) and clients
10
([`net.createConnection()`][]).
11
12
It can be accessed using:
Mar 4, 2017
Mar 4, 2017
13
14
```js
15
const net = require('net');
16
```
Nov 14, 2010
Nov 14, 2010
17
Mar 11, 2017
Mar 11, 2017
18
## IPC Support
19
20
The `net` module supports IPC with named pipes on Windows, and UNIX domain
21
sockets on other operating systems.
22
23
### Identifying paths for IPC connections
24
25
[`net.connect()`][], [`net.createConnection()`][], [`server.listen()`][] and
26
[`socket.connect()`][] take a `path` parameter to identify IPC endpoints.
27
28
On UNIX, the local domain is also known as the UNIX domain. The path is a
Apr 6, 2018
Apr 6, 2018
29
filesystem pathname. It gets truncated to `sizeof(sockaddr_un.sun_path) - 1`,
Mar 11, 2017
Mar 11, 2017
30
which varies on different operating system between 91 and 107 bytes.
Apr 4, 2017
Apr 4, 2017
31
The typical values are 107 on Linux and 103 on macOS. The path is
Mar 11, 2017
Mar 11, 2017
32
subject to the same naming conventions and permissions checks as would be done
Apr 6, 2018
Apr 6, 2018
33
on file creation. If the UNIX domain socket (that is visible as a file system
34
path) is created and used in conjunction with one of Node.js' API abstractions
35
such as [`net.createServer()`][], it will be unlinked as part of
36
[`server.close()`][]. On the other hand, if it is created and used outside of
37
these abstractions, the user will need to manually remove it. The same applies
38
when the path was created by a Node.js API but the program crashes abruptly.
39
In short, a UNIX domain socket once successfully created will be visible in the
40
filesystem, and will persist until unlinked.
Mar 11, 2017
Mar 11, 2017
41
42
On Windows, the local domain is implemented using a named pipe. The path *must*
43
refer to an entry in `\\?\pipe\` or `\\.\pipe\`. Any characters are permitted,
44
but the latter may do some processing of pipe names, such as resolving `..`
Apr 6, 2018
Apr 6, 2018
45
sequences. Despite how it might look, the pipe namespace is flat. Pipes will
46
*not persist*. They are removed when the last reference to them is closed.
47
Unlike UNIX domain sockets, Windows will close and remove the pipe when the
48
owning process exits.
49
50
JavaScript string escaping requires paths to be specified with extra backslash
51
escaping such as:
Mar 11, 2017
Mar 11, 2017
52
53
```js
54
net.createServer().listen(
Jun 29, 2017
Jun 29, 2017
55
path.join('\\\\?\\pipe', process.cwd(), 'myctl'));
Mar 11, 2017
Mar 11, 2017
56
```
57
Nov 13, 2015
Nov 13, 2015
58
## Class: net.Server
Jun 3, 2016
Jun 3, 2016
59
<!-- YAML
60
added: v0.1.90
61
-->
Mar 2, 2011
Mar 2, 2011
62
Mar 11, 2017
Mar 11, 2017
63
This class is used to create a TCP or [IPC][] server.
Mar 7, 2011
Mar 7, 2011
64
Aug 23, 2017
Aug 23, 2017
65
### new net.Server([options][, connectionListener])
Mar 4, 2017
Mar 4, 2017
66
Jul 15, 2018
Jul 15, 2018
67
* `options` {Object} See
68
[`net.createServer([options][, connectionListener])`][`net.createServer()`].
69
* `connectionListener` {Function} Automatically set as a listener for the
70
[`'connection'`][] event.
Jun 14, 2017
Jun 14, 2017
71
* Returns: {net.Server}
72
Dec 3, 2015
Dec 3, 2015
73
`net.Server` is an [`EventEmitter`][] with the following events:
Oct 27, 2014
Oct 27, 2014
74
Nov 13, 2015
Nov 13, 2015
75
### Event: 'close'
Jun 3, 2016
Jun 3, 2016
76
<!-- YAML
77
added: v0.5.0
78
-->
Nov 14, 2010
Nov 14, 2010
79
Nov 13, 2015
Nov 13, 2015
80
Emitted when the server closes. Note that if connections exist, this
81
event is not emitted until all connections are ended.
Nov 14, 2010
Nov 14, 2010
82
Nov 13, 2015
Nov 13, 2015
83
### Event: 'connection'
Jun 3, 2016
Jun 3, 2016
84
<!-- YAML
85
added: v0.1.90
86
-->
Nov 14, 2010
Nov 14, 2010
87
Feb 9, 2016
Feb 9, 2016
88
* {net.Socket} The connection object
Nov 14, 2010
Nov 14, 2010
89
Nov 13, 2015
Nov 13, 2015
90
Emitted when a new connection is made. `socket` is an instance of
91
`net.Socket`.
Nov 14, 2010
Nov 14, 2010
92
Nov 13, 2015
Nov 13, 2015
93
### Event: 'error'
Jun 3, 2016
Jun 3, 2016
94
<!-- YAML
95
added: v0.1.90
96
-->
Nov 14, 2010
Nov 14, 2010
97
Feb 9, 2016
Feb 9, 2016
98
* {Error}
Nov 14, 2010
Nov 14, 2010
99
Feb 20, 2017
Feb 20, 2017
100
Emitted when an error occurs. Unlike [`net.Socket`][], the [`'close'`][]
101
event will **not** be emitted directly following this event unless
102
[`server.close()`][] is manually called. See the example in discussion of
Mar 4, 2017
Mar 4, 2017
103
[`server.listen()`][].
Jun 28, 2011
Jun 28, 2011
104
Nov 13, 2015
Nov 13, 2015
105
### Event: 'listening'
Jun 3, 2016
Jun 3, 2016
106
<!-- YAML
107
added: v0.1.90
108
-->
Nov 14, 2010
Nov 14, 2010
109
Mar 4, 2017
Mar 4, 2017
110
Emitted when the server has been bound after calling [`server.listen()`][].
Aug 6, 2014
Aug 6, 2014
111
Nov 13, 2015
Nov 13, 2015
112
### server.address()
Jun 3, 2016
Jun 3, 2016
113
<!-- YAML
114
added: v0.1.90
115
-->
Oct 6, 2011
Oct 6, 2011
116
Aug 3, 2018
Aug 3, 2018
117
* Returns: {Object|string}
Apr 12, 2018
Apr 12, 2018
118
May 2, 2018
May 2, 2018
119
Returns the bound `address`, the address `family` name, and `port` of the server
120
as reported by the operating system if listening on an IP socket
121
(useful to find which port was assigned when getting an OS-assigned address):
122
`{ port: 12346, family: 'IPv4', address: '127.0.0.1' }`.
Oct 6, 2011
Oct 6, 2011
123
May 10, 2017
May 10, 2017
124
For a server listening on a pipe or UNIX domain socket, the name is returned
125
as a string.
126
Jan 21, 2016
Jan 21, 2016
127
```js
Mar 14, 2017
Mar 14, 2017
128
const server = net.createServer((socket) => {
Jan 21, 2016
Jan 21, 2016
129
socket.end('goodbye\n');
Feb 25, 2016
Feb 25, 2016
130
}).on('error', (err) => {
131
// handle errors here
132
throw err;
Jan 21, 2016
Jan 21, 2016
133
});
134
Mar 4, 2017
Mar 4, 2017
135
// grab an arbitrary unused port.
Feb 25, 2016
Feb 25, 2016
136
server.listen(() => {
Aug 5, 2016
Aug 5, 2016
137
console.log('opened server on', server.address());
Jan 21, 2016
Jan 21, 2016
138
});
139
```
Nov 29, 2010
Nov 29, 2010
140
Nov 13, 2015
Nov 13, 2015
141
Don't call `server.address()` until the `'listening'` event has been emitted.
Nov 29, 2010
Nov 29, 2010
142
Nov 13, 2015
Nov 13, 2015
143
### server.close([callback])
Jun 3, 2016
Jun 3, 2016
144
<!-- YAML
145
added: v0.1.90
146
-->
Nov 29, 2010
Nov 29, 2010
147
Jul 15, 2018
Jul 15, 2018
148
* `callback` {Function} Called when the server is closed
Jun 14, 2017
Jun 14, 2017
149
* Returns: {net.Server}
150
Nov 13, 2015
Nov 13, 2015
151
Stops the server from accepting new connections and keeps existing
Apr 12, 2018
Apr 12, 2018
152
connections. This function is asynchronous, the server is finally closed
153
when all connections are ended and the server emits a [`'close'`][] event.
Nov 13, 2015
Nov 13, 2015
154
The optional `callback` will be called once the `'close'` event occurs. Unlike
May 2, 2018
May 2, 2018
155
that event, it will be called with an `Error` as its only argument if the server
Nov 13, 2015
Nov 13, 2015
156
was not open when it was closed.
Oct 28, 2010
Oct 28, 2010
157
Nov 13, 2015
Nov 13, 2015
158
### server.connections
Jun 3, 2016
Jun 3, 2016
159
<!-- YAML
160
added: v0.2.0
161
deprecated: v0.9.7
162
-->
Dec 19, 2013
Dec 19, 2013
163
Aug 4, 2016
Aug 4, 2016
164
> Stability: 0 - Deprecated: Use [`server.getConnections()`][] instead.
Oct 28, 2010
Oct 28, 2010
165
Nov 13, 2015
Nov 13, 2015
166
The number of concurrent connections on the server.
Oct 28, 2010
Oct 28, 2010
167
Nov 13, 2015
Nov 13, 2015
168
This becomes `null` when sending a socket to a child with
Dec 3, 2015
Dec 3, 2015
169
[`child_process.fork()`][]. To poll forks and get current number of active
Jul 30, 2017
Jul 30, 2017
170
connections use asynchronous [`server.getConnections()`][] instead.
Dec 19, 2013
Dec 19, 2013
171
Nov 13, 2015
Nov 13, 2015
172
### server.getConnections(callback)
Jun 3, 2016
Jun 3, 2016
173
<!-- YAML
174
added: v0.9.7
175
-->
Dec 19, 2013
Dec 19, 2013
176
Jul 15, 2018
Jul 15, 2018
177
* `callback` {Function}
Sep 28, 2017
Sep 28, 2017
178
* Returns: {net.Server}
Jun 14, 2017
Jun 14, 2017
179
Nov 13, 2015
Nov 13, 2015
180
Asynchronously get the number of concurrent connections on the server. Works
181
when sockets were sent to forks.
182
183
Callback should take two arguments `err` and `count`.
Dec 19, 2013
Dec 19, 2013
184
Mar 4, 2017
Mar 4, 2017
185
### server.listen()
186
187
Start a server listening for connections. A `net.Server` can be a TCP or
May 2, 2018
May 2, 2018
188
an [IPC][] server depending on what it listens to.
Mar 4, 2017
Mar 4, 2017
189
190
Possible signatures:
191
192
* [`server.listen(handle[, backlog][, callback])`][`server.listen(handle)`]
193
* [`server.listen(options[, callback])`][`server.listen(options)`]
194
* [`server.listen(path[, backlog][, callback])`][`server.listen(path)`]
Mar 11, 2017
Mar 11, 2017
195
for [IPC][] servers
May 2, 2018
May 2, 2018
196
* <a href="#net_server_listen_port_host_backlog_callback">
197
<code>server.listen([port[, host[, backlog]]][, callback])</code></a>
Mar 4, 2017
Mar 4, 2017
198
for TCP servers
199
Apr 4, 2018
Apr 4, 2018
200
This function is asynchronous. When the server starts listening, the
201
[`'listening'`][] event will be emitted. The last parameter `callback`
Mar 4, 2017
Mar 4, 2017
202
will be added as a listener for the [`'listening'`][] event.
203
204
All `listen()` methods can take a `backlog` parameter to specify the maximum
205
length of the queue of pending connections. The actual length will be determined
206
by the OS through sysctl settings such as `tcp_max_syn_backlog` and `somaxconn`
207
on Linux. The default value of this parameter is 511 (not 512).
208
May 20, 2018
May 20, 2018
209
All [`net.Socket`][] are set to `SO_REUSEADDR` (see [`socket(7)`][] for
210
details).
Mar 4, 2017
Mar 4, 2017
211
Apr 12, 2018
Apr 12, 2018
212
The `server.listen()` method can be called again if and only if there was an
213
error during the first `server.listen()` call or `server.close()` has been
214
called. Otherwise, an `ERR_SERVER_ALREADY_LISTEN` error will be thrown.
Mar 4, 2017
Mar 4, 2017
215
216
One of the most common errors raised when listening is `EADDRINUSE`.
217
This happens when another server is already listening on the requested
Jun 7, 2018
Jun 7, 2018
218
`port`/`path`/`handle`. One way to handle this would be to retry
Mar 4, 2017
Mar 4, 2017
219
after a certain amount of time:
220
221
```js
222
server.on('error', (e) => {
223
if (e.code === 'EADDRINUSE') {
224
console.log('Address in use, retrying...');
225
setTimeout(() => {
226
server.close();
227
server.listen(PORT, HOST);
228
}, 1000);
229
}
230
});
231
```
232
233
#### server.listen(handle[, backlog][, callback])
Jun 3, 2016
Jun 3, 2016
234
<!-- YAML
235
added: v0.5.10
236
-->
Jun 13, 2012
Jun 13, 2012
237
238
* `handle` {Object}
Mar 4, 2017
Mar 4, 2017
239
* `backlog` {number} Common parameter of [`server.listen()`][] functions
240
* `callback` {Function} Common parameter of [`server.listen()`][] functions
Jun 14, 2017
Jun 14, 2017
241
* Returns: {net.Server}
Jun 13, 2012
Jun 13, 2012
242
Mar 4, 2017
Mar 4, 2017
243
Start a server listening for connections on a given `handle` that has
244
already been bound to a port, a UNIX domain socket, or a Windows named pipe.
Jun 13, 2012
Jun 13, 2012
245
Mar 4, 2017
Mar 4, 2017
246
The `handle` object can be either a server, a socket (anything with an
Jul 30, 2017
Jul 30, 2017
247
underlying `_handle` member), or an object with an `fd` member that is a
Mar 4, 2017
Mar 4, 2017
248
valid file descriptor.
Jun 13, 2012
Jun 13, 2012
249
Feb 8, 2018
Feb 8, 2018
250
Listening on a file descriptor is not supported on Windows.
Jun 13, 2012
Jun 13, 2012
251
Mar 4, 2017
Mar 4, 2017
252
#### server.listen(options[, callback])
Jun 3, 2016
Jun 3, 2016
253
<!-- YAML
254
added: v0.11.14
255
-->
Sep 3, 2014
Sep 3, 2014
256
Mar 4, 2017
Mar 4, 2017
257
* `options` {Object} Required. Supports the following properties:
Mar 11, 2017
Mar 11, 2017
258
* `port` {number}
259
* `host` {string}
260
* `path` {string} Will be ignored if `port` is specified. See
261
[Identifying paths for IPC connections][].
262
* `backlog` {number} Common parameter of [`server.listen()`][]
Oct 30, 2017
Oct 30, 2017
263
functions.
264
* `exclusive` {boolean} **Default:** `false`
May 24, 2018
May 24, 2018
265
* `readableAll` {boolean} For IPC servers makes the pipe readable
266
for all users. **Default:** `false`
267
* `writableAll` {boolean} For IPC servers makes the pipe writable
268
for all users. **Default:** `false`
Mar 11, 2017
Mar 11, 2017
269
* `callback` {Function} Common parameter of [`server.listen()`][]
Oct 30, 2017
Oct 30, 2017
270
functions.
Jun 14, 2017
Jun 14, 2017
271
* Returns: {net.Server}
Sep 3, 2014
Sep 3, 2014
272
Mar 4, 2017
Mar 4, 2017
273
If `port` is specified, it behaves the same as
May 2, 2018
May 2, 2018
274
<a href="#net_server_listen_port_host_backlog_callback">
275
<code>server.listen([port[, host[, backlog]]][, callback])</code></a>.
Mar 4, 2017
Mar 4, 2017
276
Otherwise, if `path` is specified, it behaves the same as
277
[`server.listen(path[, backlog][, callback])`][`server.listen(path)`].
278
If none of them is specified, an error will be thrown.
Sep 3, 2014
Sep 3, 2014
279
280
If `exclusive` is `false` (default), then cluster workers will use the same
281
underlying handle, allowing connection handling duties to be shared. When
282
`exclusive` is `true`, the handle is not shared, and attempted port sharing
283
results in an error. An example which listens on an exclusive port is
284
shown below.
285
Jan 21, 2016
Jan 21, 2016
286
```js
287
server.listen({
288
host: 'localhost',
289
port: 80,
290
exclusive: true
291
});
292
```
Sep 3, 2014
Sep 3, 2014
293
May 24, 2018
May 24, 2018
294
Starting an IPC server as root may cause the server path to be inaccessible for
295
unprivileged users. Using `readableAll` and `writableAll` will make the server
296
accessible for all users.
297
Mar 4, 2017
Mar 4, 2017
298
#### server.listen(path[, backlog][, callback])
Jun 3, 2016
Jun 3, 2016
299
<!-- YAML
300
added: v0.1.90
301
-->
Oct 28, 2010
Oct 28, 2010
302
Jan 12, 2018
Jan 12, 2018
303
* `path` {string} Path the server should listen to. See
Mar 11, 2017
Mar 11, 2017
304
[Identifying paths for IPC connections][].
Oct 30, 2017
Oct 30, 2017
305
* `backlog` {number} Common parameter of [`server.listen()`][] functions.
306
* `callback` {Function} Common parameter of [`server.listen()`][] functions.
Jun 14, 2017
Jun 14, 2017
307
* Returns: {net.Server}
Nov 16, 2010
Nov 16, 2010
308
May 2, 2018
May 2, 2018
309
Start an [IPC][] server listening for connections on the given `path`.
Nov 16, 2010
Nov 16, 2010
310
Apr 22, 2018
Apr 22, 2018
311
#### server.listen([port[, host[, backlog]]][, callback])
Jun 3, 2016
Jun 3, 2016
312
<!-- YAML
313
added: v0.1.90
314
-->
Mar 4, 2017
Mar 4, 2017
315
* `port` {number}
316
* `host` {string}
Oct 30, 2017
Oct 30, 2017
317
* `backlog` {number} Common parameter of [`server.listen()`][] functions.
318
* `callback` {Function} Common parameter of [`server.listen()`][] functions.
Jun 14, 2017
Jun 14, 2017
319
* Returns: {net.Server}
Mar 4, 2017
Mar 4, 2017
320
321
Start a TCP server listening for connections on the given `port` and `host`.
Nov 16, 2010
Nov 16, 2010
322
Mar 4, 2017
Mar 4, 2017
323
If `port` is omitted or is 0, the operating system will assign an arbitrary
324
unused port, which can be retrieved by using `server.address().port`
325
after the [`'listening'`][] event has been emitted.
326
327
If `host` is omitted, the server will accept connections on the
Feb 17, 2017
Feb 17, 2017
328
[unspecified IPv6 address][] (`::`) when IPv6 is available, or the
329
[unspecified IPv4 address][] (`0.0.0.0`) otherwise.
330
Feb 8, 2018
Feb 8, 2018
331
In most operating systems, listening to the [unspecified IPv6 address][] (`::`)
332
may cause the `net.Server` to also listen on the [unspecified IPv4 address][]
333
(`0.0.0.0`).
Feb 17, 2017
Feb 17, 2017
334
Jan 21, 2016
Jan 21, 2016
335
### server.listening
Jun 3, 2016
Jun 3, 2016
336
<!-- YAML
337
added: v5.7.0
338
-->
Jan 21, 2016
Jan 21, 2016
339
May 2, 2018
May 2, 2018
340
* {boolean} Indicates whether or not the server is listening for connections.
Jan 21, 2016
Jan 21, 2016
341
Feb 27, 2012
Feb 27, 2012
342
### server.maxConnections
Jun 3, 2016
Jun 3, 2016
343
<!-- YAML
344
added: v0.2.0
345
-->
Oct 28, 2010
Oct 28, 2010
346
Oct 6, 2011
Oct 6, 2011
347
Set this property to reject connections when the server's connection count gets
348
high.
Oct 28, 2010
Oct 28, 2010
349
May 14, 2012
May 14, 2012
350
It is not recommended to use this option once a socket has been sent to a child
Dec 3, 2015
Dec 3, 2015
351
with [`child_process.fork()`][].
May 14, 2012
May 14, 2012
352
Nov 13, 2015
Nov 13, 2015
353
### server.ref()
Jun 3, 2016
Jun 3, 2016
354
<!-- YAML
355
added: v0.9.1
356
-->
Oct 28, 2010
Oct 28, 2010
357
Jun 14, 2017
Jun 14, 2017
358
* Returns: {net.Server}
359
Apr 12, 2018
Apr 12, 2018
360
Opposite of `unref()`, calling `ref()` on a previously `unref`ed server will
361
*not* let the program exit if it's the only server left (the default behavior).
362
If the server is `ref`ed calling `ref()` again will have no effect.
Oct 28, 2010
Oct 28, 2010
363
Nov 13, 2015
Nov 13, 2015
364
### server.unref()
Jun 3, 2016
Jun 3, 2016
365
<!-- YAML
366
added: v0.9.1
367
-->
Sep 6, 2011
Sep 6, 2011
368
Jun 14, 2017
Jun 14, 2017
369
* Returns: {net.Server}
370
Apr 12, 2018
Apr 12, 2018
371
Calling `unref()` on a server will allow the program to exit if this is the only
372
active server in the event system. If the server is already `unref`ed calling
373
`unref()` again will have no effect.
Sep 6, 2011
Sep 6, 2011
374
Feb 27, 2012
Feb 27, 2012
375
## Class: net.Socket
Jun 3, 2016
Jun 3, 2016
376
<!-- YAML
377
added: v0.3.4
378
-->
Oct 28, 2010
Oct 28, 2010
379
Mar 11, 2017
Mar 11, 2017
380
This class is an abstraction of a TCP socket or a streaming [IPC][] endpoint
381
(uses named pipes on Windows, and UNIX domain sockets otherwise). A
382
`net.Socket` is also a [duplex stream][], so it can be both readable and
May 2, 2018
May 2, 2018
383
writable, and it is also an [`EventEmitter`][].
Mar 11, 2017
Mar 11, 2017
384
385
A `net.Socket` can be created by the user and used directly to interact with
386
a server. For example, it is returned by [`net.createConnection()`][],
387
so the user can use it to talk to the server.
388
Jul 30, 2017
Jul 30, 2017
389
It can also be created by Node.js and passed to the user when a connection
Mar 11, 2017
Mar 11, 2017
390
is received. For example, it is passed to the listeners of a
391
[`'connection'`][] event emitted on a [`net.Server`][], so the user can use
392
it to interact with the client.
Oct 28, 2010
Oct 28, 2010
393
Feb 27, 2012
Feb 27, 2012
394
### new net.Socket([options])
Jun 3, 2016
Jun 3, 2016
395
<!-- YAML
396
added: v0.3.4
397
-->
Mar 10, 2011
Mar 10, 2011
398
Mar 11, 2017
Mar 11, 2017
399
* `options` {Object} Available options are:
Apr 29, 2018
Apr 29, 2018
400
* `fd` {number} If specified, wrap around an existing socket with
Mar 11, 2017
Mar 11, 2017
401
the given file descriptor, otherwise a new socket will be created.
402
* `allowHalfOpen` {boolean} Indicates whether half-opened TCP connections
403
are allowed. See [`net.createServer()`][] and the [`'end'`][] event
Apr 4, 2018
Apr 4, 2018
404
for details. **Default:** `false`.
Jul 30, 2017
Jul 30, 2017
405
* `readable` {boolean} Allow reads on the socket when an `fd` is passed,
Apr 4, 2018
Apr 4, 2018
406
otherwise ignored. **Default:** `false`.
Jul 30, 2017
Jul 30, 2017
407
* `writable` {boolean} Allow writes on the socket when an `fd` is passed,
Apr 4, 2018
Apr 4, 2018
408
otherwise ignored. **Default:** `false`.
Mar 11, 2017
Mar 11, 2017
409
* Returns: {net.Socket}
Mar 10, 2011
Mar 10, 2011
410
Jul 15, 2018
Jul 15, 2018
411
Creates a new socket object.
412
Mar 11, 2017
Mar 11, 2017
413
The newly created socket can be either a TCP socket or a streaming [IPC][]
414
endpoint, depending on what it [`connect()`][`socket.connect()`] to.
Mar 1, 2015
Mar 1, 2015
415
Nov 13, 2015
Nov 13, 2015
416
### Event: 'close'
Jun 3, 2016
Jun 3, 2016
417
<!-- YAML
418
added: v0.1.90
419
-->
Mar 1, 2015
Mar 1, 2015
420
Apr 12, 2018
Apr 12, 2018
421
* `hadError` {boolean} `true` if the socket had a transmission error.
Oct 28, 2010
Oct 28, 2010
422
Apr 12, 2018
Apr 12, 2018
423
Emitted once the socket is fully closed. The argument `hadError` is a boolean
Nov 13, 2015
Nov 13, 2015
424
which says if the socket was closed due to a transmission error.
Mar 1, 2015
Mar 1, 2015
425
Nov 13, 2015
Nov 13, 2015
426
### Event: 'connect'
Jun 3, 2016
Jun 3, 2016
427
<!-- YAML
428
added: v0.1.90
429
-->
Mar 1, 2015
Mar 1, 2015
430
Nov 13, 2015
Nov 13, 2015
431
Emitted when a socket connection is successfully established.
Mar 11, 2017
Mar 11, 2017
432
See [`net.createConnection()`][].
Mar 1, 2015
Mar 1, 2015
433
Nov 13, 2015
Nov 13, 2015
434
### Event: 'data'
Jun 3, 2016
Jun 3, 2016
435
<!-- YAML
436
added: v0.1.90
437
-->
Mar 1, 2015
Mar 1, 2015
438
Apr 12, 2018
Apr 12, 2018
439
* {Buffer|string}
May 16, 2015
May 16, 2015
440
Apr 4, 2018
Apr 4, 2018
441
Emitted when data is received. The argument `data` will be a `Buffer` or
442
`String`. Encoding of data is set by [`socket.setEncoding()`][].
Mar 1, 2015
Mar 1, 2015
443
Sep 25, 2016
Sep 25, 2016
444
Note that the **data will be lost** if there is no listener when a `Socket`
Nov 13, 2015
Nov 13, 2015
445
emits a `'data'` event.
Mar 1, 2015
Mar 1, 2015
446
Nov 13, 2015
Nov 13, 2015
447
### Event: 'drain'
Jun 3, 2016
Jun 3, 2016
448
<!-- YAML
449
added: v0.1.90
450
-->
Oct 28, 2010
Oct 28, 2010
451
Nov 13, 2015
Nov 13, 2015
452
Emitted when the write buffer becomes empty. Can be used to throttle uploads.
Oct 28, 2010
Oct 28, 2010
453
May 2, 2018
May 2, 2018
454
See also: the return values of `socket.write()`.
Oct 28, 2010
Oct 28, 2010
455
Nov 13, 2015
Nov 13, 2015
456
### Event: 'end'
Jun 3, 2016
Jun 3, 2016
457
<!-- YAML
458
added: v0.1.90
459
-->
Dec 15, 2010
Dec 15, 2010
460
Mar 4, 2017
Mar 4, 2017
461
Emitted when the other end of the socket sends a FIN packet, thus ending the
462
readable side of the socket.
Mar 1, 2015
Mar 1, 2015
463
Mar 4, 2017
Mar 4, 2017
464
By default (`allowHalfOpen` is `false`) the socket will send a FIN packet
465
back and destroy its file descriptor once it has written out its pending
466
write queue. However, if `allowHalfOpen` is set to `true`, the socket will
467
not automatically [`end()`][`socket.end()`] its writable side, allowing the
468
user to write arbitrary amounts of data. The user must call
469
[`end()`][`socket.end()`] explicitly to close the connection (i.e. sending a
470
FIN packet back).
Nov 13, 2015
Nov 13, 2015
471
472
### Event: 'error'
Jun 3, 2016
Jun 3, 2016
473
<!-- YAML
474
added: v0.1.90
475
-->
Nov 13, 2015
Nov 13, 2015
476
Feb 9, 2016
Feb 9, 2016
477
* {Error}
Nov 13, 2015
Nov 13, 2015
478
Apr 4, 2018
Apr 4, 2018
479
Emitted when an error occurs. The `'close'` event will be called directly
Nov 13, 2015
Nov 13, 2015
480
following this event.
481
482
### Event: 'lookup'
Jun 3, 2016
Jun 3, 2016
483
<!-- YAML
484
added: v0.11.3
Feb 24, 2017
Feb 24, 2017
485
changes:
486
- version: v5.10.0
487
pr-url: https://github.com/nodejs/node/pull/5598
488
description: The `host` parameter is supported now.
Jun 3, 2016
Jun 3, 2016
489
-->
Nov 13, 2015
Nov 13, 2015
490
491
Emitted after resolving the hostname but before connecting.
492
Not applicable to UNIX sockets.
493
Apr 4, 2018
Apr 4, 2018
494
* `err` {Error|null} The error object. See [`dns.lookup()`][].
Mar 2, 2017
Mar 2, 2017
495
* `address` {string} The IP address.
Apr 4, 2018
Apr 4, 2018
496
* `family` {string|null} The address type. See [`dns.lookup()`][].
Mar 2, 2017
Mar 2, 2017
497
* `host` {string} The hostname.
Nov 13, 2015
Nov 13, 2015
498
Apr 12, 2018
Apr 12, 2018
499
### Event: 'ready'
500
<!-- YAML
501
added: v9.11.0
502
-->
503
504
Emitted when a socket is ready to be used.
505
506
Triggered immediately after `'connect'`.
507
Nov 13, 2015
Nov 13, 2015
508
### Event: 'timeout'
Jun 3, 2016
Jun 3, 2016
509
<!-- YAML
510
added: v0.1.90
511
-->
Nov 13, 2015
Nov 13, 2015
512
513
Emitted if the socket times out from inactivity. This is only to notify that
514
the socket has been idle. The user must manually close the connection.
515
May 2, 2018
May 2, 2018
516
See also: [`socket.setTimeout()`][].
Nov 13, 2015
Nov 13, 2015
517
518
### socket.address()
Jun 3, 2016
Jun 3, 2016
519
<!-- YAML
520
added: v0.1.90
521
-->
Nov 13, 2015
Nov 13, 2015
522
Apr 12, 2018
Apr 12, 2018
523
* Returns: {Object}
524
May 2, 2018
May 2, 2018
525
Returns the bound `address`, the address `family` name and `port` of the
526
socket as reported by the operating system:
Nov 13, 2015
Nov 13, 2015
527
`{ port: 12346, family: 'IPv4', address: '127.0.0.1' }`
Oct 28, 2010
Oct 28, 2010
528
Feb 27, 2012
Feb 27, 2012
529
### socket.bufferSize
Jun 3, 2016
Jun 3, 2016
530
<!-- YAML
531
added: v0.3.8
532
-->
Jan 31, 2011
Jan 31, 2011
533
534
`net.Socket` has the property that `socket.write()` always works. This is to
Sep 6, 2011
Sep 6, 2011
535
help users get up and running quickly. The computer cannot always keep up
Oct 6, 2011
Oct 6, 2011
536
with the amount of data that is written to a socket - the network connection
Aug 23, 2015
Aug 23, 2015
537
simply might be too slow. Node.js will internally queue up the data written to a
Oct 6, 2011
Oct 6, 2011
538
socket and send it out over the wire when it is possible. (Internally it is
539
polling on the socket's file descriptor for being writable).
Jan 31, 2011
Jan 31, 2011
540
541
The consequence of this internal buffering is that memory may grow. This
542
property shows the number of characters currently buffered to be written.
543
(Number of characters is approximately equal to the number of bytes to be
544
written, but the buffer may contain strings, and the strings are lazily
545
encoded, so the exact number of bytes is not known.)
546
547
Users who experience large or growing `bufferSize` should attempt to
Mar 4, 2017
Mar 4, 2017
548
"throttle" the data flows in their program with
549
[`socket.pause()`][] and [`socket.resume()`][].
Jan 31, 2011
Jan 31, 2011
550
Nov 13, 2015
Nov 13, 2015
551
### socket.bytesRead
Jun 3, 2016
Jun 3, 2016
552
<!-- YAML
553
added: v0.5.3
554
-->
Jan 31, 2011
Jan 31, 2011
555
Nov 13, 2015
Nov 13, 2015
556
The amount of received bytes.
Oct 28, 2010
Oct 28, 2010
557
Nov 13, 2015
Nov 13, 2015
558
### socket.bytesWritten
Jun 3, 2016
Jun 3, 2016
559
<!-- YAML
560
added: v0.5.3
561
-->
Oct 28, 2010
Oct 28, 2010
562
Nov 13, 2015
Nov 13, 2015
563
The amount of bytes sent.
Oct 28, 2010
Oct 28, 2010
564
Mar 11, 2017
Mar 11, 2017
565
### socket.connect()
566
567
Initiate a connection on a given socket.
568
569
Possible signatures:
570
May 2, 2018
May 2, 2018
571
* [`socket.connect(options[, connectListener])`][`socket.connect(options)`]
572
* [`socket.connect(path[, connectListener])`][`socket.connect(path)`]
Mar 11, 2017
Mar 11, 2017
573
for [IPC][] connections.
May 2, 2018
May 2, 2018
574
* [`socket.connect(port[, host][, connectListener])`][`socket.connect(port, host)`]
Mar 11, 2017
Mar 11, 2017
575
for TCP connections.
Jun 14, 2017
Jun 14, 2017
576
* Returns: {net.Socket} The socket itself.
Mar 11, 2017
Mar 11, 2017
577
578
This function is asynchronous. When the connection is established, the
579
[`'connect'`][] event will be emitted. If there is a problem connecting,
580
instead of a [`'connect'`][] event, an [`'error'`][] event will be emitted with
581
the error passed to the [`'error'`][] listener.
582
The last parameter `connectListener`, if supplied, will be added as a listener
583
for the [`'connect'`][] event **once**.
584
585
#### socket.connect(options[, connectListener])
Jun 3, 2016
Jun 3, 2016
586
<!-- YAML
587
added: v0.1.90
Feb 24, 2017
Feb 24, 2017
588
changes:
589
- version: v6.0.0
590
pr-url: https://github.com/nodejs/node/pull/6021
591
description: The `hints` option defaults to `0` in all cases now.
592
Previously, in the absence of the `family` option it would
593
default to `dns.ADDRCONFIG | dns.V4MAPPED`.
594
- version: v5.11.0
595
pr-url: https://github.com/nodejs/node/pull/6000
596
description: The `hints` option is supported now.
Jun 3, 2016
Jun 3, 2016
597
-->
Oct 28, 2010
Oct 28, 2010
598
Mar 11, 2017
Mar 11, 2017
599
* `options` {Object}
600
* `connectListener` {Function} Common parameter of [`socket.connect()`][]
601
methods. Will be added as a listener for the [`'connect'`][] event once.
602
* Returns: {net.Socket} The socket itself.
Nov 13, 2015
Nov 13, 2015
603
Mar 11, 2017
Mar 11, 2017
604
Initiate a connection on a given socket. Normally this method is not needed,
605
the socket should be created and opened with [`net.createConnection()`][]. Use
Apr 28, 2017
Apr 28, 2017
606
this only when implementing a custom Socket.
Nov 13, 2015
Nov 13, 2015
607
Mar 11, 2017
Mar 11, 2017
608
For TCP connections, available `options` are:
Nov 13, 2015
Nov 13, 2015
609
Mar 11, 2017
Mar 11, 2017
610
* `port` {number} Required. Port the socket should connect to.
Apr 4, 2018
Apr 4, 2018
611
* `host` {string} Host the socket should connect to. **Default:** `'localhost'`.
Mar 11, 2017
Mar 11, 2017
612
* `localAddress` {string} Local address the socket should connect from.
613
* `localPort` {number} Local port the socket should connect from.
Apr 4, 2018
Apr 4, 2018
614
* `family` {number}: Version of IP stack, can be either `4` or `6`.
615
**Default:** `4`.
Mar 11, 2017
Mar 11, 2017
616
* `hints` {number} Optional [`dns.lookup()` hints][].
Apr 4, 2018
Apr 4, 2018
617
* `lookup` {Function} Custom lookup function. **Default:** [`dns.lookup()`][].
Apr 1, 2016
Apr 1, 2016
618
Mar 11, 2017
Mar 11, 2017
619
For [IPC][] connections, available `options` are:
Nov 13, 2015
Nov 13, 2015
620
Mar 11, 2017
Mar 11, 2017
621
* `path` {string} Required. Path the client should connect to.
Sep 22, 2017
Sep 22, 2017
622
See [Identifying paths for IPC connections][]. If provided, the TCP-specific
623
options above are ignored.
Nov 13, 2015
Nov 13, 2015
624
Mar 11, 2017
Mar 11, 2017
625
#### socket.connect(path[, connectListener])
Nov 13, 2015
Nov 13, 2015
626
Mar 11, 2017
Mar 11, 2017
627
* `path` {string} Path the client should connect to. See
628
[Identifying paths for IPC connections][].
629
* `connectListener` {Function} Common parameter of [`socket.connect()`][]
630
methods. Will be added as a listener for the [`'connect'`][] event once.
631
* Returns: {net.Socket} The socket itself.
Nov 13, 2015
Nov 13, 2015
632
Mar 11, 2017
Mar 11, 2017
633
Initiate an [IPC][] connection on the given socket.
Nov 13, 2015
Nov 13, 2015
634
Mar 11, 2017
Mar 11, 2017
635
Alias to
636
[`socket.connect(options[, connectListener])`][`socket.connect(options)`]
637
called with `{ path: path }` as `options`.
Nov 13, 2015
Nov 13, 2015
638
Mar 11, 2017
Mar 11, 2017
639
#### socket.connect(port[, host][, connectListener])
Jun 3, 2016
Jun 3, 2016
640
<!-- YAML
641
added: v0.1.90
642
-->
Nov 13, 2015
Nov 13, 2015
643
Mar 11, 2017
Mar 11, 2017
644
* `port` {number} Port the client should connect to.
645
* `host` {string} Host the client should connect to.
646
* `connectListener` {Function} Common parameter of [`socket.connect()`][]
647
methods. Will be added as a listener for the [`'connect'`][] event once.
648
* Returns: {net.Socket} The socket itself.
649
650
Initiate a TCP connection on the given socket.
651
652
Alias to
653
[`socket.connect(options[, connectListener])`][`socket.connect(options)`]
654
called with `{port: port, host: host}` as `options`.
Nov 13, 2015
Nov 13, 2015
655
Apr 27, 2016
Apr 27, 2016
656
### socket.connecting
Jun 3, 2016
Jun 3, 2016
657
<!-- YAML
658
added: v6.1.0
659
-->
Apr 27, 2016
Apr 27, 2016
660
Mar 11, 2017
Mar 11, 2017
661
If `true` -
662
[`socket.connect(options[, connectListener])`][`socket.connect(options)`]
663
was called and haven't yet finished. Will be set to `false` before emitting
Apr 12, 2018
Apr 12, 2018
664
`'connect'` event and/or calling
Mar 11, 2017
Mar 11, 2017
665
[`socket.connect(options[, connectListener])`][`socket.connect(options)`]'s
666
callback.
Apr 27, 2016
Apr 27, 2016
667
Jun 22, 2016
Jun 22, 2016
668
### socket.destroy([exception])
Jun 3, 2016
Jun 3, 2016
669
<!-- YAML
670
added: v0.1.90
671
-->
Nov 13, 2015
Nov 13, 2015
672
Jul 15, 2018
Jul 15, 2018
673
* `exception` {Object}
Jun 14, 2017
Jun 14, 2017
674
* Returns: {net.Socket}
675
Nov 13, 2015
Nov 13, 2015
676
Ensures that no more I/O activity happens on this socket. Only necessary in
677
case of errors (parse error or so).
Dec 15, 2010
Dec 15, 2010
678
Jun 22, 2016
Jun 22, 2016
679
If `exception` is specified, an [`'error'`][] event will be emitted and any
680
listeners for that event will receive `exception` as an argument.
681
Jun 21, 2016
Jun 21, 2016
682
### socket.destroyed
683
May 2, 2018
May 2, 2018
684
* {boolean} Indicates if the connection is destroyed or not. Once a
685
connection is destroyed no further data can be transferred using it.
Jun 21, 2016
Jun 21, 2016
686
Sep 29, 2014
Sep 29, 2014
687
### socket.end([data][, encoding])
Jun 3, 2016
Jun 3, 2016
688
<!-- YAML
689
added: v0.1.90
690
-->
Oct 28, 2010
Oct 28, 2010
691
Jul 15, 2018
Jul 15, 2018
692
* `data` {string|Buffer|Uint8Array}
693
* `encoding` {string} Only used when data is `string`. **Default:** `'utf8'`.
Jun 14, 2017
Jun 14, 2017
694
* Returns: {net.Socket} The socket itself.
695
Sep 6, 2011
Sep 6, 2011
696
Half-closes the socket. i.e., it sends a FIN packet. It is possible the
Dec 9, 2010
Dec 9, 2010
697
server will still send some data.
Oct 28, 2010
Oct 28, 2010
698
Oct 6, 2011
Oct 6, 2011
699
If `data` is specified, it is equivalent to calling
Mar 4, 2017
Mar 4, 2017
700
`socket.write(data, encoding)` followed by [`socket.end()`][].
Oct 28, 2010
Oct 28, 2010
701
Nov 13, 2015
Nov 13, 2015
702
### socket.localAddress
Jun 3, 2016
Jun 3, 2016
703
<!-- YAML
704
added: v0.9.6
705
-->
Oct 28, 2010
Oct 28, 2010
706
Nov 13, 2015
Nov 13, 2015
707
The string representation of the local IP address the remote client is
Apr 28, 2017
Apr 28, 2017
708
connecting on. For example, in a server listening on `'0.0.0.0'`, if a client
709
connects on `'192.168.1.1'`, the value of `socket.localAddress` would be
710
`'192.168.1.1'`.
Nov 13, 2015
Nov 13, 2015
711
712
### socket.localPort
Jun 3, 2016
Jun 3, 2016
713
<!-- YAML
714
added: v0.9.6
715
-->
Nov 13, 2015
Nov 13, 2015
716
Aug 29, 2018
Aug 29, 2018
717
The numeric representation of the local port. For example, `80` or `21`.
Oct 28, 2010
Oct 28, 2010
718
Feb 27, 2012
Feb 27, 2012
719
### socket.pause()
Oct 28, 2010
Oct 28, 2010
720
Jun 14, 2017
Jun 14, 2017
721
* Returns: {net.Socket} The socket itself.
722
Dec 3, 2015
Dec 3, 2015
723
Pauses the reading of data. That is, [`'data'`][] events will not be emitted.
Nov 14, 2010
Nov 14, 2010
724
Useful to throttle back an upload.
Oct 28, 2010
Oct 28, 2010
725
Nov 13, 2015
Nov 13, 2015
726
### socket.ref()
Jun 3, 2016
Jun 3, 2016
727
<!-- YAML
728
added: v0.9.1
729
-->
Oct 28, 2010
Oct 28, 2010
730
Jun 14, 2017
Jun 14, 2017
731
* Returns: {net.Socket} The socket itself.
732
Apr 12, 2018
Apr 12, 2018
733
Opposite of `unref()`, calling `ref()` on a previously `unref`ed socket will
734
*not* let the program exit if it's the only socket left (the default behavior).
735
If the socket is `ref`ed calling `ref` again will have no effect.
Oct 28, 2010
Oct 28, 2010
736
Nov 13, 2015
Nov 13, 2015
737
### socket.remoteAddress
Jun 3, 2016
Jun 3, 2016
738
<!-- YAML
739
added: v0.5.10
740
-->
Oct 28, 2010
Oct 28, 2010
741
Nov 13, 2015
Nov 13, 2015
742
The string representation of the remote IP address. For example,
Dec 14, 2015
Dec 14, 2015
743
`'74.125.127.100'` or `'2001:4860:a005::68'`. Value may be `undefined` if
744
the socket is destroyed (for example, if the client disconnected).
Oct 28, 2010
Oct 28, 2010
745
Nov 13, 2015
Nov 13, 2015
746
### socket.remoteFamily
Jun 3, 2016
Jun 3, 2016
747
<!-- YAML
748
added: v0.11.14
749
-->
Oct 28, 2010
Oct 28, 2010
750
Nov 13, 2015
Nov 13, 2015
751
The string representation of the remote IP family. `'IPv4'` or `'IPv6'`.
Jan 26, 2011
Jan 26, 2011
752
Nov 13, 2015
Nov 13, 2015
753
### socket.remotePort
Jun 3, 2016
Jun 3, 2016
754
<!-- YAML
755
added: v0.5.10
756
-->
May 16, 2015
May 16, 2015
757
Aug 29, 2018
Aug 29, 2018
758
The numeric representation of the remote port. For example, `80` or `21`.
Oct 28, 2010
Oct 28, 2010
759
Nov 13, 2015
Nov 13, 2015
760
### socket.resume()
Oct 28, 2010
Oct 28, 2010
761
Jun 14, 2017
Jun 14, 2017
762
* Returns: {net.Socket} The socket itself.
763
Mar 4, 2017
Mar 4, 2017
764
Resumes reading after a call to [`socket.pause()`][].
Nov 13, 2015
Nov 13, 2015
765
766
### socket.setEncoding([encoding])
Jun 3, 2016
Jun 3, 2016
767
<!-- YAML
768
added: v0.1.90
769
-->
Nov 13, 2015
Nov 13, 2015
770
Jul 15, 2018
Jul 15, 2018
771
* `encoding` {string}
Jun 14, 2017
Jun 14, 2017
772
* Returns: {net.Socket} The socket itself.
773
Dec 3, 2015
Dec 3, 2015
774
Set the encoding for the socket as a [Readable Stream][]. See
Mar 25, 2018
Mar 25, 2018
775
[`readable.setEncoding()`][] for more information.
May 23, 2015
May 23, 2015
776
Sep 29, 2014
Sep 29, 2014
777
### socket.setKeepAlive([enable][, initialDelay])
Jun 3, 2016
Jun 3, 2016
778
<!-- YAML
779
added: v0.1.92
780
-->
Oct 28, 2010
Oct 28, 2010
781
Apr 4, 2018
Apr 4, 2018
782
* `enable` {boolean} **Default:** `false`
783
* `initialDelay` {number} **Default:** `0`
Jun 14, 2017
Jun 14, 2017
784
* Returns: {net.Socket} The socket itself.
785
Nov 14, 2010
Nov 14, 2010
786
Enable/disable keep-alive functionality, and optionally set the initial
Jan 5, 2011
Jan 5, 2011
787
delay before the first keepalive probe is sent on an idle socket.
Dec 27, 2011
Dec 27, 2011
788
Nov 14, 2010
Nov 14, 2010
789
Set `initialDelay` (in milliseconds) to set the delay between the last
May 2, 2018
May 2, 2018
790
data packet received and the first keepalive probe. Setting `0` for
791
`initialDelay` will leave the value unchanged from the default
Apr 4, 2018
Apr 4, 2018
792
(or previous) setting.
Oct 28, 2010
Oct 28, 2010
793
Nov 13, 2015
Nov 13, 2015
794
### socket.setNoDelay([noDelay])
Jun 3, 2016
Jun 3, 2016
795
<!-- YAML
796
added: v0.1.90
797
-->
May 9, 2011
May 9, 2011
798
Apr 4, 2018
Apr 4, 2018
799
* `noDelay` {boolean} **Default:** `true`
Jun 14, 2017
Jun 14, 2017
800
* Returns: {net.Socket} The socket itself.
801
Nov 13, 2015
Nov 13, 2015
802
Disables the Nagle algorithm. By default TCP connections use the Nagle
803
algorithm, they buffer data before sending it off. Setting `true` for
804
`noDelay` will immediately fire off data each time `socket.write()` is called.
805
806
### socket.setTimeout(timeout[, callback])
Jun 3, 2016
Jun 3, 2016
807
<!-- YAML
808
added: v0.1.90
809
-->
Nov 13, 2015
Nov 13, 2015
810
Jul 15, 2018
Jul 15, 2018
811
* `timeout` {number}
812
* `callback` {Function}
Jun 14, 2017
Jun 14, 2017
813
* Returns: {net.Socket} The socket itself.
814
Nov 13, 2015
Nov 13, 2015
815
Sets the socket to timeout after `timeout` milliseconds of inactivity on
816
the socket. By default `net.Socket` do not have a timeout.
817
Dec 3, 2015
Dec 3, 2015
818
When an idle timeout is triggered the socket will receive a [`'timeout'`][]
Mar 4, 2017
Mar 4, 2017
819
event but the connection will not be severed. The user must manually call
820
[`socket.end()`][] or [`socket.destroy()`][] to end the connection.
821
822
```js
823
socket.setTimeout(3000);
824
socket.on('timeout', () => {
825
console.log('socket timeout');
826
socket.end();
Mar 14, 2017
Mar 14, 2017
827
});
Mar 4, 2017
Mar 4, 2017
828
```
Nov 13, 2015
Nov 13, 2015
829
830
If `timeout` is 0, then the existing idle timeout is disabled.
831
Dec 7, 2017
Dec 7, 2017
832
The optional `callback` parameter will be added as a one-time listener for the
Dec 3, 2015
Dec 3, 2015
833
[`'timeout'`][] event.
Nov 13, 2015
Nov 13, 2015
834
Jul 23, 2012
Jul 23, 2012
835
### socket.unref()
Jun 3, 2016
Jun 3, 2016
836
<!-- YAML
837
added: v0.9.1
838
-->
Jul 23, 2012
Jul 23, 2012
839
Jun 14, 2017
Jun 14, 2017
840
* Returns: {net.Socket} The socket itself.
841
Apr 12, 2018
Apr 12, 2018
842
Calling `unref()` on a socket will allow the program to exit if this is the only
843
active socket in the event system. If the socket is already `unref`ed calling
844
`unref()` again will have no effect.
Jul 23, 2012
Jul 23, 2012
845
Nov 13, 2015
Nov 13, 2015
846
### socket.write(data[, encoding][, callback])
Jun 3, 2016
Jun 3, 2016
847
<!-- YAML
848
added: v0.1.90
849
-->
Jul 23, 2012
Jul 23, 2012
850
Apr 13, 2018
Apr 13, 2018
851
* `data` {string|Buffer|Uint8Array}
852
* `encoding` {string} Only used when data is `string`. **Default:** `utf8`.
853
* `callback` {Function}
Apr 12, 2018
Apr 12, 2018
854
* Returns: {boolean}
855
Nov 13, 2015
Nov 13, 2015
856
Sends data on the socket. The second parameter specifies the encoding in the
Apr 4, 2018
Apr 4, 2018
857
case of a string β€” it defaults to UTF8 encoding.
Jul 23, 2012
Jul 23, 2012
858
Nov 13, 2015
Nov 13, 2015
859
Returns `true` if the entire data was flushed successfully to the kernel
860
buffer. Returns `false` if all or part of the data was queued in user memory.
Dec 3, 2015
Dec 3, 2015
861
[`'drain'`][] will be emitted when the buffer is again free.
May 23, 2015
May 23, 2015
862
Nov 13, 2015
Nov 13, 2015
863
The optional `callback` parameter will be executed when the data is finally
864
written out - this may not be immediately.
Oct 28, 2010
Oct 28, 2010
865
May 2, 2018
May 2, 2018
866
See `Writable` stream [`write()`][stream_writable_write] method for more
Apr 13, 2018
Apr 13, 2018
867
information.
868
Mar 11, 2017
Mar 11, 2017
869
## net.connect()
Jul 23, 2014
Jul 23, 2014
870
Mar 11, 2017
Mar 11, 2017
871
Aliases to
872
[`net.createConnection()`][`net.createConnection()`].
Apr 13, 2011
Apr 13, 2011
873
Mar 11, 2017
Mar 11, 2017
874
Possible signatures:
Jan 5, 2013
Jan 5, 2013
875
Mar 11, 2017
Mar 11, 2017
876
* [`net.connect(options[, connectListener])`][`net.connect(options)`]
877
* [`net.connect(path[, connectListener])`][`net.connect(path)`] for [IPC][]
878
connections.
879
* [`net.connect(port[, host][, connectListener])`][`net.connect(port, host)`]
880
for TCP connections.
Jan 5, 2013
Jan 5, 2013
881
Mar 11, 2017
Mar 11, 2017
882
### net.connect(options[, connectListener])
883
<!-- YAML
884
added: v0.7.0
885
-->
Jul 15, 2018
Jul 15, 2018
886
* `options` {Object}
887
* `connectListener` {Function}
Mar 11, 2017
Mar 11, 2017
888
Alias to
889
[`net.createConnection(options[, connectListener])`][`net.createConnection(options)`].
Jan 5, 2013
Jan 5, 2013
890
Mar 11, 2017
Mar 11, 2017
891
### net.connect(path[, connectListener])
Jun 3, 2016
Jun 3, 2016
892
<!-- YAML
893
added: v0.1.90
894
-->
Jul 15, 2018
Jul 15, 2018
895
* `path` {string}
896
* `connectListener` {Function}
Jan 5, 2013
Jan 5, 2013
897
Mar 11, 2017
Mar 11, 2017
898
Alias to
899
[`net.createConnection(path[, connectListener])`][`net.createConnection(path)`].
Jul 25, 2011
Jul 25, 2011
900
Mar 11, 2017
Mar 11, 2017
901
### net.connect(port[, host][, connectListener])
Jun 3, 2016
Jun 3, 2016
902
<!-- YAML
903
added: v0.1.90
904
-->
Jul 15, 2018
Jul 15, 2018
905
* `port` {number}
906
* `host` {string}
907
* `connectListener` {Function}
Jul 25, 2011
Jul 25, 2011
908
Mar 11, 2017
Mar 11, 2017
909
Alias to
910
[`net.createConnection(port[, host][, connectListener])`][`net.createConnection(port, host)`].
911
912
## net.createConnection()
Jul 25, 2011
Jul 25, 2011
913
Mar 11, 2017
Mar 11, 2017
914
A factory function, which creates a new [`net.Socket`][],
915
immediately initiates connection with [`socket.connect()`][],
916
then returns the `net.Socket` that starts the connection.
Oct 28, 2010
Oct 28, 2010
917
Mar 11, 2017
Mar 11, 2017
918
When the connection is established, a [`'connect'`][] event will be emitted
919
on the returned socket. The last parameter `connectListener`, if supplied,
920
will be added as a listener for the [`'connect'`][] event **once**.
Oct 28, 2010
Oct 28, 2010
921
Mar 11, 2017
Mar 11, 2017
922
Possible signatures:
923
924
* [`net.createConnection(options[, connectListener])`][`net.createConnection(options)`]
925
* [`net.createConnection(path[, connectListener])`][`net.createConnection(path)`]
926
for [IPC][] connections.
927
* [`net.createConnection(port[, host][, connectListener])`][`net.createConnection(port, host)`]
928
for TCP connections.
929
Feb 8, 2018
Feb 8, 2018
930
The [`net.connect()`][] function is an alias to this function.
Mar 11, 2017
Mar 11, 2017
931
932
### net.createConnection(options[, connectListener])
Jun 3, 2016
Jun 3, 2016
933
<!-- YAML
934
added: v0.1.90
935
-->
May 15, 2013
May 15, 2013
936
Mar 11, 2017
Mar 11, 2017
937
* `options` {Object} Required. Will be passed to both the
938
[`new net.Socket([options])`][`new net.Socket(options)`] call and the
939
[`socket.connect(options[, connectListener])`][`socket.connect(options)`]
940
method.
941
* `connectListener` {Function} Common parameter of the
942
[`net.createConnection()`][] functions. If supplied, will be added as
943
a listener for the [`'connect'`][] event on the returned socket once.
944
* Returns: {net.Socket} The newly created socket used to start the connection.
May 15, 2013
May 15, 2013
945
Mar 11, 2017
Mar 11, 2017
946
For available options, see
947
[`new net.Socket([options])`][`new net.Socket(options)`]
948
and [`socket.connect(options[, connectListener])`][`socket.connect(options)`].
Sep 26, 2016
Sep 26, 2016
949
Mar 11, 2017
Mar 11, 2017
950
Additional options:
May 15, 2013
May 15, 2013
951
Mar 11, 2017
Mar 11, 2017
952
* `timeout` {number} If set, will be used to call
953
[`socket.setTimeout(timeout)`][] after the socket is created, but before
954
it starts the connection.
Oct 28, 2010
Oct 28, 2010
955
Apr 8, 2017
Apr 8, 2017
956
Following is an example of a client of the echo server described
957
in the [`net.createServer()`][] section:
Oct 28, 2010
Oct 28, 2010
958
Jan 21, 2016
Jan 21, 2016
959
```js
960
const net = require('net');
Jun 2, 2017
Jun 2, 2017
961
const client = net.createConnection({ port: 8124 }, () => {
Apr 1, 2018
Apr 1, 2018
962
// 'connect' listener
Jan 21, 2016
Jan 21, 2016
963
console.log('connected to server!');
964
client.write('world!\r\n');
965
});
966
client.on('data', (data) => {
967
console.log(data.toString());
968
client.end();
969
});
970
client.on('end', () => {
971
console.log('disconnected from server');
972
});
973
```
Oct 28, 2010
Oct 28, 2010
974
Nov 13, 2015
Nov 13, 2015
975
To connect on the socket `/tmp/echo.sock` the second line would just be
May 2, 2018
May 2, 2018
976
changed to:
Oct 28, 2010
Oct 28, 2010
977
Jan 21, 2016
Jan 21, 2016
978
```js
Jun 2, 2017
Jun 2, 2017
979
const client = net.createConnection({ path: '/tmp/echo.sock' });
Jan 21, 2016
Jan 21, 2016
980
```
Oct 28, 2010
Oct 28, 2010
981
Mar 11, 2017
Mar 11, 2017
982
### net.createConnection(path[, connectListener])
Jun 3, 2016
Jun 3, 2016
983
<!-- YAML
984
added: v0.1.90
985
-->
Feb 5, 2012
Feb 5, 2012
986
Mar 11, 2017
Mar 11, 2017
987
* `path` {string} Path the socket should connect to. Will be passed to
988
[`socket.connect(path[, connectListener])`][`socket.connect(path)`].
989
See [Identifying paths for IPC connections][].
990
* `connectListener` {Function} Common parameter of the
991
[`net.createConnection()`][] functions, an "once" listener for the
992
`'connect'` event on the initiating socket. Will be passed to
993
[`socket.connect(path[, connectListener])`][`socket.connect(path)`].
994
* Returns: {net.Socket} The newly created socket used to start the connection.
995
996
Initiates an [IPC][] connection.
Oct 28, 2010
Oct 28, 2010
997
Mar 11, 2017
Mar 11, 2017
998
This function creates a new [`net.Socket`][] with all options set to default,
999
immediately initiates connection with
1000
[`socket.connect(path[, connectListener])`][`socket.connect(path)`],