2017-02-08 00:56:07 +01:00
|
|
|
# Net
|
2010-11-14 11:12:04 +01:00
|
|
|
|
2017-01-23 04:16:21 +01:00
|
|
|
<!--introduced_in=v0.10.0-->
|
2018-02-12 08:31:55 +01:00
|
|
|
<!--lint disable maximum-line-length-->
|
2017-01-23 04:16:21 +01:00
|
|
|
|
2016-07-16 00:35:38 +02:00
|
|
|
> Stability: 2 - Stable
|
2012-03-03 00:14:03 +01:00
|
|
|
|
2017-03-01 09:02:29 +01:00
|
|
|
The `net` module provides an asynchronous network API for creating stream-based
|
2017-03-07 07:49:36 +01:00
|
|
|
TCP or [IPC][] servers ([`net.createServer()`][]) and clients
|
|
|
|
([`net.createConnection()`][]).
|
|
|
|
|
|
|
|
It can be accessed using:
|
2017-03-01 09:02:29 +01:00
|
|
|
|
|
|
|
```js
|
|
|
|
const net = require('net');
|
|
|
|
```
|
2010-11-14 11:12:04 +01:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
## IPC Support
|
|
|
|
|
|
|
|
The `net` module supports IPC with named pipes on Windows, and UNIX domain
|
|
|
|
sockets on other operating systems.
|
|
|
|
|
|
|
|
### Identifying paths for IPC connections
|
|
|
|
|
|
|
|
[`net.connect()`][], [`net.createConnection()`][], [`server.listen()`][] and
|
|
|
|
[`socket.connect()`][] take a `path` parameter to identify IPC endpoints.
|
|
|
|
|
|
|
|
On UNIX, the local domain is also known as the UNIX domain. The path is a
|
2018-03-20 09:11:37 +01:00
|
|
|
filesystem pathname. It gets truncated to `sizeof(sockaddr_un.sun_path) - 1`,
|
2017-03-07 07:49:36 +01:00
|
|
|
which varies on different operating system between 91 and 107 bytes.
|
2017-03-29 01:46:10 +02:00
|
|
|
The typical values are 107 on Linux and 103 on macOS. The path is
|
2017-03-07 07:49:36 +01:00
|
|
|
subject to the same naming conventions and permissions checks as would be done
|
2018-03-20 09:11:37 +01:00
|
|
|
on file creation. If the UNIX domain socket (that is visible as a file system
|
|
|
|
path) is created and used in conjunction with one of Node.js' API abstractions
|
|
|
|
such as [`net.createServer()`][], it will be unlinked as part of
|
|
|
|
[`server.close()`][]. On the other hand, if it is created and used outside of
|
|
|
|
these abstractions, the user will need to manually remove it. The same applies
|
|
|
|
when the path was created by a Node.js API but the program crashes abruptly.
|
|
|
|
In short, a UNIX domain socket once successfully created will be visible in the
|
|
|
|
filesystem, and will persist until unlinked.
|
2017-03-07 07:49:36 +01:00
|
|
|
|
|
|
|
On Windows, the local domain is implemented using a named pipe. The path *must*
|
|
|
|
refer to an entry in `\\?\pipe\` or `\\.\pipe\`. Any characters are permitted,
|
|
|
|
but the latter may do some processing of pipe names, such as resolving `..`
|
2018-03-20 09:11:37 +01:00
|
|
|
sequences. Despite how it might look, the pipe namespace is flat. Pipes will
|
|
|
|
*not persist*. They are removed when the last reference to them is closed.
|
|
|
|
Unlike UNIX domain sockets, Windows will close and remove the pipe when the
|
|
|
|
owning process exits.
|
|
|
|
|
|
|
|
JavaScript string escaping requires paths to be specified with extra backslash
|
|
|
|
escaping such as:
|
2017-03-07 07:49:36 +01:00
|
|
|
|
|
|
|
```js
|
|
|
|
net.createServer().listen(
|
2017-06-27 22:32:32 +02:00
|
|
|
path.join('\\\\?\\pipe', process.cwd(), 'myctl'));
|
2017-03-07 07:49:36 +01:00
|
|
|
```
|
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
## Class: net.Server
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2011-03-02 19:32:13 +01:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
This class is used to create a TCP or [IPC][] server.
|
2011-03-03 17:05:03 +01:00
|
|
|
|
2017-08-23 16:27:08 +02:00
|
|
|
### new net.Server([options][, connectionListener])
|
2017-03-01 09:02:29 +01:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `options` {Object} See
|
|
|
|
[`net.createServer([options][, connectionListener])`][`net.createServer()`].
|
|
|
|
* `connectionListener` {Function} Automatically set as a listener for the
|
|
|
|
[`'connection'`][] event.
|
2017-06-07 21:49:00 +02:00
|
|
|
* Returns: {net.Server}
|
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
`net.Server` is an [`EventEmitter`][] with the following events:
|
2014-10-18 03:45:40 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### Event: 'close'
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.5.0
|
|
|
|
-->
|
2010-11-14 11:12:04 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Emitted when the server closes. Note that if connections exist, this
|
|
|
|
event is not emitted until all connections are ended.
|
2010-11-14 11:12:04 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### Event: 'connection'
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2010-11-14 11:12:04 +01:00
|
|
|
|
2016-01-19 17:03:15 +01:00
|
|
|
* {net.Socket} The connection object
|
2010-11-14 11:12:04 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Emitted when a new connection is made. `socket` is an instance of
|
|
|
|
`net.Socket`.
|
2010-11-14 11:12:04 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### Event: 'error'
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2010-11-14 11:12:04 +01:00
|
|
|
|
2016-01-19 17:03:15 +01:00
|
|
|
* {Error}
|
2010-11-14 11:12:04 +01:00
|
|
|
|
2017-02-12 10:45:42 +01:00
|
|
|
Emitted when an error occurs. Unlike [`net.Socket`][], the [`'close'`][]
|
|
|
|
event will **not** be emitted directly following this event unless
|
|
|
|
[`server.close()`][] is manually called. See the example in discussion of
|
2017-03-01 09:02:29 +01:00
|
|
|
[`server.listen()`][].
|
2011-06-21 16:53:02 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### Event: 'listening'
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2010-11-14 11:12:04 +01:00
|
|
|
|
2017-03-01 09:02:29 +01:00
|
|
|
Emitted when the server has been bound after calling [`server.listen()`][].
|
2014-08-03 02:27:02 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### server.address()
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2011-10-06 07:56:50 +02:00
|
|
|
|
2018-07-31 11:48:46 +02:00
|
|
|
* Returns: {Object|string}
|
2018-04-11 20:07:14 +02:00
|
|
|
|
2018-04-29 13:16:44 +02:00
|
|
|
Returns the bound `address`, the address `family` name, and `port` of the server
|
|
|
|
as reported by the operating system if listening on an IP socket
|
|
|
|
(useful to find which port was assigned when getting an OS-assigned address):
|
|
|
|
`{ port: 12346, family: 'IPv4', address: '127.0.0.1' }`.
|
2011-10-06 07:56:50 +02:00
|
|
|
|
2017-05-08 22:25:28 +02:00
|
|
|
For a server listening on a pipe or UNIX domain socket, the name is returned
|
|
|
|
as a string.
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2017-03-10 11:07:15 +01:00
|
|
|
const server = net.createServer((socket) => {
|
2016-01-17 18:39:07 +01:00
|
|
|
socket.end('goodbye\n');
|
2016-02-21 19:07:27 +01:00
|
|
|
}).on('error', (err) => {
|
|
|
|
// handle errors here
|
|
|
|
throw err;
|
2016-01-17 18:39:07 +01:00
|
|
|
});
|
|
|
|
|
2017-03-01 09:02:29 +01:00
|
|
|
// grab an arbitrary unused port.
|
2016-02-21 19:07:27 +01:00
|
|
|
server.listen(() => {
|
2016-08-04 09:47:06 +02:00
|
|
|
console.log('opened server on', server.address());
|
2016-01-17 18:39:07 +01:00
|
|
|
});
|
|
|
|
```
|
2010-11-29 09:20:21 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Don't call `server.address()` until the `'listening'` event has been emitted.
|
2010-11-29 09:20:21 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### server.close([callback])
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2010-11-29 09:20:21 +01:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `callback` {Function} Called when the server is closed
|
2017-06-07 21:49:00 +02:00
|
|
|
* Returns: {net.Server}
|
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Stops the server from accepting new connections and keeps existing
|
2018-04-09 18:30:22 +02:00
|
|
|
connections. This function is asynchronous, the server is finally closed
|
|
|
|
when all connections are ended and the server emits a [`'close'`][] event.
|
2015-11-05 20:15:39 +01:00
|
|
|
The optional `callback` will be called once the `'close'` event occurs. Unlike
|
2018-04-29 19:46:41 +02:00
|
|
|
that event, it will be called with an `Error` as its only argument if the server
|
2015-11-05 20:15:39 +01:00
|
|
|
was not open when it was closed.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### server.connections
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.2.0
|
|
|
|
deprecated: v0.9.7
|
|
|
|
-->
|
2013-12-19 23:57:30 +01:00
|
|
|
|
2016-07-16 00:35:38 +02:00
|
|
|
> Stability: 0 - Deprecated: Use [`server.getConnections()`][] instead.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
The number of concurrent connections on the server.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
This becomes `null` when sending a socket to a child with
|
2015-11-28 00:30:32 +01:00
|
|
|
[`child_process.fork()`][]. To poll forks and get current number of active
|
2018-11-04 08:18:49 +01:00
|
|
|
connections, use asynchronous [`server.getConnections()`][] instead.
|
2013-12-19 23:57:30 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### server.getConnections(callback)
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.9.7
|
|
|
|
-->
|
2013-12-19 23:57:30 +01:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `callback` {Function}
|
2017-06-18 21:53:54 +02:00
|
|
|
* Returns: {net.Server}
|
2017-06-07 21:49:00 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Asynchronously get the number of concurrent connections on the server. Works
|
|
|
|
when sockets were sent to forks.
|
|
|
|
|
|
|
|
Callback should take two arguments `err` and `count`.
|
2013-12-19 23:57:30 +01:00
|
|
|
|
2017-03-01 09:02:29 +01:00
|
|
|
### server.listen()
|
|
|
|
|
|
|
|
Start a server listening for connections. A `net.Server` can be a TCP or
|
2018-04-29 19:46:41 +02:00
|
|
|
an [IPC][] server depending on what it listens to.
|
2017-03-01 09:02:29 +01:00
|
|
|
|
|
|
|
Possible signatures:
|
|
|
|
|
|
|
|
* [`server.listen(handle[, backlog][, callback])`][`server.listen(handle)`]
|
|
|
|
* [`server.listen(options[, callback])`][`server.listen(options)`]
|
|
|
|
* [`server.listen(path[, backlog][, callback])`][`server.listen(path)`]
|
2017-03-07 07:49:36 +01:00
|
|
|
for [IPC][] servers
|
2018-04-29 23:39:20 +02:00
|
|
|
* <a href="#net_server_listen_port_host_backlog_callback">
|
|
|
|
<code>server.listen([port[, host[, backlog]]][, callback])</code></a>
|
2017-03-01 09:02:29 +01:00
|
|
|
for TCP servers
|
|
|
|
|
2018-04-02 07:38:48 +02:00
|
|
|
This function is asynchronous. When the server starts listening, the
|
|
|
|
[`'listening'`][] event will be emitted. The last parameter `callback`
|
2017-03-01 09:02:29 +01:00
|
|
|
will be added as a listener for the [`'listening'`][] event.
|
|
|
|
|
|
|
|
All `listen()` methods can take a `backlog` parameter to specify the maximum
|
|
|
|
length of the queue of pending connections. The actual length will be determined
|
|
|
|
by the OS through sysctl settings such as `tcp_max_syn_backlog` and `somaxconn`
|
|
|
|
on Linux. The default value of this parameter is 511 (not 512).
|
|
|
|
|
2018-05-20 19:16:19 +02:00
|
|
|
All [`net.Socket`][] are set to `SO_REUSEADDR` (see [`socket(7)`][] for
|
|
|
|
details).
|
2017-03-01 09:02:29 +01:00
|
|
|
|
2018-04-09 18:30:22 +02:00
|
|
|
The `server.listen()` method can be called again if and only if there was an
|
|
|
|
error during the first `server.listen()` call or `server.close()` has been
|
|
|
|
called. Otherwise, an `ERR_SERVER_ALREADY_LISTEN` error will be thrown.
|
2017-03-01 09:02:29 +01:00
|
|
|
|
|
|
|
One of the most common errors raised when listening is `EADDRINUSE`.
|
|
|
|
This happens when another server is already listening on the requested
|
2018-06-04 21:32:54 +02:00
|
|
|
`port`/`path`/`handle`. One way to handle this would be to retry
|
2017-03-01 09:02:29 +01:00
|
|
|
after a certain amount of time:
|
|
|
|
|
|
|
|
```js
|
|
|
|
server.on('error', (e) => {
|
|
|
|
if (e.code === 'EADDRINUSE') {
|
|
|
|
console.log('Address in use, retrying...');
|
|
|
|
setTimeout(() => {
|
|
|
|
server.close();
|
|
|
|
server.listen(PORT, HOST);
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
#### server.listen(handle[, backlog][, callback])
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.5.10
|
|
|
|
-->
|
2012-06-12 19:02:52 +02:00
|
|
|
|
|
|
|
* `handle` {Object}
|
2017-03-01 09:02:29 +01:00
|
|
|
* `backlog` {number} Common parameter of [`server.listen()`][] functions
|
|
|
|
* `callback` {Function} Common parameter of [`server.listen()`][] functions
|
2017-06-07 21:49:00 +02:00
|
|
|
* Returns: {net.Server}
|
2012-06-12 19:02:52 +02:00
|
|
|
|
2017-03-01 09:02:29 +01:00
|
|
|
Start a server listening for connections on a given `handle` that has
|
|
|
|
already been bound to a port, a UNIX domain socket, or a Windows named pipe.
|
2012-06-12 19:02:52 +02:00
|
|
|
|
2017-03-01 09:02:29 +01:00
|
|
|
The `handle` object can be either a server, a socket (anything with an
|
2017-07-26 16:35:04 +02:00
|
|
|
underlying `_handle` member), or an object with an `fd` member that is a
|
2017-03-01 09:02:29 +01:00
|
|
|
valid file descriptor.
|
2012-06-12 19:02:52 +02:00
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
Listening on a file descriptor is not supported on Windows.
|
2012-06-12 19:02:52 +02:00
|
|
|
|
2017-03-01 09:02:29 +01:00
|
|
|
#### server.listen(options[, callback])
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.14
|
2018-10-21 09:59:38 +02:00
|
|
|
changes:
|
|
|
|
- version: REPLACEME
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/23798
|
|
|
|
description: The `ipv6Only` option is supported.
|
2016-06-03 02:15:22 +02:00
|
|
|
-->
|
2014-08-22 22:51:53 +02:00
|
|
|
|
2017-03-01 09:02:29 +01:00
|
|
|
* `options` {Object} Required. Supports the following properties:
|
2017-03-07 07:49:36 +01:00
|
|
|
* `port` {number}
|
|
|
|
* `host` {string}
|
|
|
|
* `path` {string} Will be ignored if `port` is specified. See
|
|
|
|
[Identifying paths for IPC connections][].
|
|
|
|
* `backlog` {number} Common parameter of [`server.listen()`][]
|
2017-10-27 21:51:28 +02:00
|
|
|
functions.
|
|
|
|
* `exclusive` {boolean} **Default:** `false`
|
2018-03-08 00:09:24 +01:00
|
|
|
* `readableAll` {boolean} For IPC servers makes the pipe readable
|
|
|
|
for all users. **Default:** `false`
|
|
|
|
* `writableAll` {boolean} For IPC servers makes the pipe writable
|
|
|
|
for all users. **Default:** `false`
|
2018-10-21 09:59:38 +02:00
|
|
|
* `ipv6Only` {boolean} For TCP servers, setting `ipv6Only` to `true` will
|
|
|
|
disable dual-stack support, i.e., binding to host `::` won't make
|
|
|
|
`0.0.0.0` be bound. **Default:** `false`.
|
2017-03-07 07:49:36 +01:00
|
|
|
* `callback` {Function} Common parameter of [`server.listen()`][]
|
2017-10-27 21:51:28 +02:00
|
|
|
functions.
|
2017-06-07 21:49:00 +02:00
|
|
|
* Returns: {net.Server}
|
2014-08-22 22:51:53 +02:00
|
|
|
|
2017-03-01 09:02:29 +01:00
|
|
|
If `port` is specified, it behaves the same as
|
2018-04-29 23:39:20 +02:00
|
|
|
<a href="#net_server_listen_port_host_backlog_callback">
|
|
|
|
<code>server.listen([port[, host[, backlog]]][, callback])</code></a>.
|
2017-03-01 09:02:29 +01:00
|
|
|
Otherwise, if `path` is specified, it behaves the same as
|
|
|
|
[`server.listen(path[, backlog][, callback])`][`server.listen(path)`].
|
|
|
|
If none of them is specified, an error will be thrown.
|
2014-08-22 22:51:53 +02:00
|
|
|
|
|
|
|
If `exclusive` is `false` (default), then cluster workers will use the same
|
|
|
|
underlying handle, allowing connection handling duties to be shared. When
|
|
|
|
`exclusive` is `true`, the handle is not shared, and attempted port sharing
|
|
|
|
results in an error. An example which listens on an exclusive port is
|
|
|
|
shown below.
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
server.listen({
|
|
|
|
host: 'localhost',
|
|
|
|
port: 80,
|
|
|
|
exclusive: true
|
|
|
|
});
|
|
|
|
```
|
2014-08-22 22:51:53 +02:00
|
|
|
|
2018-03-08 00:09:24 +01:00
|
|
|
Starting an IPC server as root may cause the server path to be inaccessible for
|
|
|
|
unprivileged users. Using `readableAll` and `writableAll` will make the server
|
|
|
|
accessible for all users.
|
|
|
|
|
2017-03-01 09:02:29 +01:00
|
|
|
#### server.listen(path[, backlog][, callback])
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2018-01-12 00:28:02 +01:00
|
|
|
* `path` {string} Path the server should listen to. See
|
2017-03-07 07:49:36 +01:00
|
|
|
[Identifying paths for IPC connections][].
|
2017-10-27 21:51:28 +02:00
|
|
|
* `backlog` {number} Common parameter of [`server.listen()`][] functions.
|
|
|
|
* `callback` {Function} Common parameter of [`server.listen()`][] functions.
|
2017-06-07 21:49:00 +02:00
|
|
|
* Returns: {net.Server}
|
2010-11-16 05:26:46 +01:00
|
|
|
|
2018-04-29 19:46:41 +02:00
|
|
|
Start an [IPC][] server listening for connections on the given `path`.
|
2010-11-16 05:26:46 +01:00
|
|
|
|
2018-04-22 11:32:52 +02:00
|
|
|
#### server.listen([port[, host[, backlog]]][, callback])
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2017-03-01 09:02:29 +01:00
|
|
|
* `port` {number}
|
|
|
|
* `host` {string}
|
2017-10-27 21:51:28 +02:00
|
|
|
* `backlog` {number} Common parameter of [`server.listen()`][] functions.
|
|
|
|
* `callback` {Function} Common parameter of [`server.listen()`][] functions.
|
2017-06-07 21:49:00 +02:00
|
|
|
* Returns: {net.Server}
|
2017-03-01 09:02:29 +01:00
|
|
|
|
|
|
|
Start a TCP server listening for connections on the given `port` and `host`.
|
2010-11-16 05:26:46 +01:00
|
|
|
|
2017-03-01 09:02:29 +01:00
|
|
|
If `port` is omitted or is 0, the operating system will assign an arbitrary
|
|
|
|
unused port, which can be retrieved by using `server.address().port`
|
|
|
|
after the [`'listening'`][] event has been emitted.
|
|
|
|
|
|
|
|
If `host` is omitted, the server will accept connections on the
|
2017-02-12 11:09:34 +01:00
|
|
|
[unspecified IPv6 address][] (`::`) when IPv6 is available, or the
|
|
|
|
[unspecified IPv4 address][] (`0.0.0.0`) otherwise.
|
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
In most operating systems, listening to the [unspecified IPv6 address][] (`::`)
|
|
|
|
may cause the `net.Server` to also listen on the [unspecified IPv4 address][]
|
|
|
|
(`0.0.0.0`).
|
2017-02-12 11:09:34 +01:00
|
|
|
|
2016-01-18 14:36:48 +01:00
|
|
|
### server.listening
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v5.7.0
|
|
|
|
-->
|
2016-01-18 14:36:48 +01:00
|
|
|
|
2018-04-29 19:46:41 +02:00
|
|
|
* {boolean} Indicates whether or not the server is listening for connections.
|
2016-01-18 14:36:48 +01:00
|
|
|
|
2012-02-27 20:09:34 +01:00
|
|
|
### server.maxConnections
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.2.0
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-10-06 07:56:50 +02:00
|
|
|
Set this property to reject connections when the server's connection count gets
|
|
|
|
high.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-04-12 09:23:07 +02:00
|
|
|
It is not recommended to use this option once a socket has been sent to a child
|
2015-11-28 00:30:32 +01:00
|
|
|
with [`child_process.fork()`][].
|
2012-04-12 09:23:07 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### server.ref()
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.9.1
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-06-07 21:49:00 +02:00
|
|
|
* Returns: {net.Server}
|
|
|
|
|
2018-04-09 18:30:22 +02:00
|
|
|
Opposite of `unref()`, calling `ref()` on a previously `unref`ed server will
|
|
|
|
*not* let the program exit if it's the only server left (the default behavior).
|
|
|
|
If the server is `ref`ed calling `ref()` again will have no effect.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### server.unref()
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.9.1
|
|
|
|
-->
|
2011-08-31 15:12:34 +02:00
|
|
|
|
2017-06-07 21:49:00 +02:00
|
|
|
* Returns: {net.Server}
|
|
|
|
|
2018-04-09 18:30:22 +02:00
|
|
|
Calling `unref()` on a server will allow the program to exit if this is the only
|
|
|
|
active server in the event system. If the server is already `unref`ed calling
|
|
|
|
`unref()` again will have no effect.
|
2011-08-31 15:12:34 +02:00
|
|
|
|
2012-02-27 20:09:34 +01:00
|
|
|
## Class: net.Socket
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.4
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
This class is an abstraction of a TCP socket or a streaming [IPC][] endpoint
|
|
|
|
(uses named pipes on Windows, and UNIX domain sockets otherwise). A
|
|
|
|
`net.Socket` is also a [duplex stream][], so it can be both readable and
|
2018-04-29 19:46:41 +02:00
|
|
|
writable, and it is also an [`EventEmitter`][].
|
2017-03-07 07:49:36 +01:00
|
|
|
|
|
|
|
A `net.Socket` can be created by the user and used directly to interact with
|
|
|
|
a server. For example, it is returned by [`net.createConnection()`][],
|
|
|
|
so the user can use it to talk to the server.
|
|
|
|
|
2017-07-26 16:35:04 +02:00
|
|
|
It can also be created by Node.js and passed to the user when a connection
|
2017-03-07 07:49:36 +01:00
|
|
|
is received. For example, it is passed to the listeners of a
|
|
|
|
[`'connection'`][] event emitted on a [`net.Server`][], so the user can use
|
|
|
|
it to interact with the client.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-02-27 20:09:34 +01:00
|
|
|
### new net.Socket([options])
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.4
|
|
|
|
-->
|
2011-03-10 18:15:05 +01:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
* `options` {Object} Available options are:
|
2018-04-26 02:15:59 +02:00
|
|
|
* `fd` {number} If specified, wrap around an existing socket with
|
2017-03-07 07:49:36 +01:00
|
|
|
the given file descriptor, otherwise a new socket will be created.
|
|
|
|
* `allowHalfOpen` {boolean} Indicates whether half-opened TCP connections
|
|
|
|
are allowed. See [`net.createServer()`][] and the [`'end'`][] event
|
2018-04-02 03:44:32 +02:00
|
|
|
for details. **Default:** `false`.
|
2017-07-26 16:35:04 +02:00
|
|
|
* `readable` {boolean} Allow reads on the socket when an `fd` is passed,
|
2018-04-02 03:44:32 +02:00
|
|
|
otherwise ignored. **Default:** `false`.
|
2017-07-26 16:35:04 +02:00
|
|
|
* `writable` {boolean} Allow writes on the socket when an `fd` is passed,
|
2018-04-02 03:44:32 +02:00
|
|
|
otherwise ignored. **Default:** `false`.
|
2017-03-07 07:49:36 +01:00
|
|
|
* Returns: {net.Socket}
|
2011-03-10 18:15:05 +01:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
Creates a new socket object.
|
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
The newly created socket can be either a TCP socket or a streaming [IPC][]
|
|
|
|
endpoint, depending on what it [`connect()`][`socket.connect()`] to.
|
2015-02-25 12:24:45 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### Event: 'close'
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2015-02-25 12:24:45 +01:00
|
|
|
|
2018-04-09 18:30:22 +02:00
|
|
|
* `hadError` {boolean} `true` if the socket had a transmission error.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2018-04-09 18:30:22 +02:00
|
|
|
Emitted once the socket is fully closed. The argument `hadError` is a boolean
|
2015-11-05 20:15:39 +01:00
|
|
|
which says if the socket was closed due to a transmission error.
|
2015-02-25 12:24:45 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### Event: 'connect'
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2015-02-25 12:24:45 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Emitted when a socket connection is successfully established.
|
2017-03-07 07:49:36 +01:00
|
|
|
See [`net.createConnection()`][].
|
2015-02-25 12:24:45 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### Event: 'data'
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2015-02-25 12:24:45 +01:00
|
|
|
|
2018-04-09 18:30:22 +02:00
|
|
|
* {Buffer|string}
|
2015-05-14 04:25:57 +02:00
|
|
|
|
2018-04-02 07:38:48 +02:00
|
|
|
Emitted when data is received. The argument `data` will be a `Buffer` or
|
|
|
|
`String`. Encoding of data is set by [`socket.setEncoding()`][].
|
2015-02-25 12:24:45 +01:00
|
|
|
|
2016-09-20 06:44:22 +02:00
|
|
|
Note that the **data will be lost** if there is no listener when a `Socket`
|
2015-11-05 20:15:39 +01:00
|
|
|
emits a `'data'` event.
|
2015-02-25 12:24:45 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### Event: 'drain'
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Emitted when the write buffer becomes empty. Can be used to throttle uploads.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2018-04-29 13:16:44 +02:00
|
|
|
See also: the return values of `socket.write()`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### Event: 'end'
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2010-12-16 00:57:13 +01:00
|
|
|
|
2017-03-01 09:02:29 +01:00
|
|
|
Emitted when the other end of the socket sends a FIN packet, thus ending the
|
|
|
|
readable side of the socket.
|
2015-02-25 12:24:45 +01:00
|
|
|
|
2017-03-01 09:02:29 +01:00
|
|
|
By default (`allowHalfOpen` is `false`) the socket will send a FIN packet
|
|
|
|
back and destroy its file descriptor once it has written out its pending
|
|
|
|
write queue. However, if `allowHalfOpen` is set to `true`, the socket will
|
|
|
|
not automatically [`end()`][`socket.end()`] its writable side, allowing the
|
|
|
|
user to write arbitrary amounts of data. The user must call
|
|
|
|
[`end()`][`socket.end()`] explicitly to close the connection (i.e. sending a
|
|
|
|
FIN packet back).
|
2015-11-05 20:15:39 +01:00
|
|
|
|
|
|
|
### Event: 'error'
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2015-11-05 20:15:39 +01:00
|
|
|
|
2016-01-19 17:03:15 +01:00
|
|
|
* {Error}
|
2015-11-05 20:15:39 +01:00
|
|
|
|
2018-04-02 07:38:48 +02:00
|
|
|
Emitted when an error occurs. The `'close'` event will be called directly
|
2015-11-05 20:15:39 +01:00
|
|
|
following this event.
|
|
|
|
|
|
|
|
### Event: 'lookup'
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.3
|
2017-02-21 23:38:46 +01:00
|
|
|
changes:
|
|
|
|
- version: v5.10.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/5598
|
|
|
|
description: The `host` parameter is supported now.
|
2016-06-03 02:15:22 +02:00
|
|
|
-->
|
2015-11-05 20:15:39 +01:00
|
|
|
|
|
|
|
Emitted after resolving the hostname but before connecting.
|
|
|
|
Not applicable to UNIX sockets.
|
|
|
|
|
2018-04-02 07:38:48 +02:00
|
|
|
* `err` {Error|null} The error object. See [`dns.lookup()`][].
|
2017-02-04 16:15:33 +01:00
|
|
|
* `address` {string} The IP address.
|
2018-04-02 07:38:48 +02:00
|
|
|
* `family` {string|null} The address type. See [`dns.lookup()`][].
|
2017-02-04 16:15:33 +01:00
|
|
|
* `host` {string} The hostname.
|
2015-11-05 20:15:39 +01:00
|
|
|
|
2018-04-12 10:11:51 +02:00
|
|
|
### Event: 'ready'
|
|
|
|
<!-- YAML
|
|
|
|
added: v9.11.0
|
|
|
|
-->
|
|
|
|
|
|
|
|
Emitted when a socket is ready to be used.
|
|
|
|
|
|
|
|
Triggered immediately after `'connect'`.
|
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### Event: 'timeout'
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2015-11-05 20:15:39 +01:00
|
|
|
|
|
|
|
Emitted if the socket times out from inactivity. This is only to notify that
|
|
|
|
the socket has been idle. The user must manually close the connection.
|
|
|
|
|
2018-04-29 13:16:44 +02:00
|
|
|
See also: [`socket.setTimeout()`][].
|
2015-11-05 20:15:39 +01:00
|
|
|
|
|
|
|
### socket.address()
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2015-11-05 20:15:39 +01:00
|
|
|
|
2018-04-11 20:07:14 +02:00
|
|
|
* Returns: {Object}
|
|
|
|
|
2018-04-29 13:16:44 +02:00
|
|
|
Returns the bound `address`, the address `family` name and `port` of the
|
|
|
|
socket as reported by the operating system:
|
2015-11-05 20:15:39 +01:00
|
|
|
`{ port: 12346, family: 'IPv4', address: '127.0.0.1' }`
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-02-27 20:09:34 +01:00
|
|
|
### socket.bufferSize
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.8
|
|
|
|
-->
|
2011-01-31 19:41:52 +01:00
|
|
|
|
|
|
|
`net.Socket` has the property that `socket.write()` always works. This is to
|
2011-08-31 15:12:34 +02:00
|
|
|
help users get up and running quickly. The computer cannot always keep up
|
2011-10-06 07:56:50 +02:00
|
|
|
with the amount of data that is written to a socket - the network connection
|
2015-08-13 18:14:34 +02:00
|
|
|
simply might be too slow. Node.js will internally queue up the data written to a
|
2011-10-06 07:56:50 +02:00
|
|
|
socket and send it out over the wire when it is possible. (Internally it is
|
|
|
|
polling on the socket's file descriptor for being writable).
|
2011-01-31 19:41:52 +01:00
|
|
|
|
|
|
|
The consequence of this internal buffering is that memory may grow. This
|
|
|
|
property shows the number of characters currently buffered to be written.
|
|
|
|
(Number of characters is approximately equal to the number of bytes to be
|
|
|
|
written, but the buffer may contain strings, and the strings are lazily
|
|
|
|
encoded, so the exact number of bytes is not known.)
|
|
|
|
|
|
|
|
Users who experience large or growing `bufferSize` should attempt to
|
2017-03-01 09:02:29 +01:00
|
|
|
"throttle" the data flows in their program with
|
|
|
|
[`socket.pause()`][] and [`socket.resume()`][].
|
2011-01-31 19:41:52 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### socket.bytesRead
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.5.3
|
|
|
|
-->
|
2011-01-31 19:41:52 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
The amount of received bytes.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### socket.bytesWritten
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.5.3
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
The amount of bytes sent.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
### socket.connect()
|
|
|
|
|
|
|
|
Initiate a connection on a given socket.
|
|
|
|
|
|
|
|
Possible signatures:
|
|
|
|
|
2018-04-29 19:46:41 +02:00
|
|
|
* [`socket.connect(options[, connectListener])`][`socket.connect(options)`]
|
|
|
|
* [`socket.connect(path[, connectListener])`][`socket.connect(path)`]
|
2017-03-07 07:49:36 +01:00
|
|
|
for [IPC][] connections.
|
2018-04-29 19:46:41 +02:00
|
|
|
* [`socket.connect(port[, host][, connectListener])`][`socket.connect(port, host)`]
|
2017-03-07 07:49:36 +01:00
|
|
|
for TCP connections.
|
2017-06-07 21:49:00 +02:00
|
|
|
* Returns: {net.Socket} The socket itself.
|
2017-03-07 07:49:36 +01:00
|
|
|
|
|
|
|
This function is asynchronous. When the connection is established, the
|
|
|
|
[`'connect'`][] event will be emitted. If there is a problem connecting,
|
|
|
|
instead of a [`'connect'`][] event, an [`'error'`][] event will be emitted with
|
|
|
|
the error passed to the [`'error'`][] listener.
|
|
|
|
The last parameter `connectListener`, if supplied, will be added as a listener
|
|
|
|
for the [`'connect'`][] event **once**.
|
|
|
|
|
|
|
|
#### socket.connect(options[, connectListener])
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
2017-02-21 23:38:46 +01:00
|
|
|
changes:
|
|
|
|
- version: v6.0.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/6021
|
|
|
|
description: The `hints` option defaults to `0` in all cases now.
|
|
|
|
Previously, in the absence of the `family` option it would
|
|
|
|
default to `dns.ADDRCONFIG | dns.V4MAPPED`.
|
|
|
|
- version: v5.11.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/6000
|
|
|
|
description: The `hints` option is supported now.
|
2016-06-03 02:15:22 +02:00
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
* `options` {Object}
|
|
|
|
* `connectListener` {Function} Common parameter of [`socket.connect()`][]
|
|
|
|
methods. Will be added as a listener for the [`'connect'`][] event once.
|
|
|
|
* Returns: {net.Socket} The socket itself.
|
2015-11-05 20:15:39 +01:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
Initiate a connection on a given socket. Normally this method is not needed,
|
|
|
|
the socket should be created and opened with [`net.createConnection()`][]. Use
|
2017-04-26 19:16:12 +02:00
|
|
|
this only when implementing a custom Socket.
|
2015-11-05 20:15:39 +01:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
For TCP connections, available `options` are:
|
2015-11-05 20:15:39 +01:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
* `port` {number} Required. Port the socket should connect to.
|
2018-04-02 03:44:32 +02:00
|
|
|
* `host` {string} Host the socket should connect to. **Default:** `'localhost'`.
|
2017-03-07 07:49:36 +01:00
|
|
|
* `localAddress` {string} Local address the socket should connect from.
|
|
|
|
* `localPort` {number} Local port the socket should connect from.
|
2018-04-02 03:44:32 +02:00
|
|
|
* `family` {number}: Version of IP stack, can be either `4` or `6`.
|
|
|
|
**Default:** `4`.
|
2017-03-07 07:49:36 +01:00
|
|
|
* `hints` {number} Optional [`dns.lookup()` hints][].
|
2018-04-02 03:44:32 +02:00
|
|
|
* `lookup` {Function} Custom lookup function. **Default:** [`dns.lookup()`][].
|
2016-04-01 22:59:09 +02:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
For [IPC][] connections, available `options` are:
|
2015-11-05 20:15:39 +01:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
* `path` {string} Required. Path the client should connect to.
|
2017-08-01 08:58:39 +02:00
|
|
|
See [Identifying paths for IPC connections][]. If provided, the TCP-specific
|
|
|
|
options above are ignored.
|
2015-11-05 20:15:39 +01:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
#### socket.connect(path[, connectListener])
|
2015-11-05 20:15:39 +01:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
* `path` {string} Path the client should connect to. See
|
|
|
|
[Identifying paths for IPC connections][].
|
|
|
|
* `connectListener` {Function} Common parameter of [`socket.connect()`][]
|
|
|
|
methods. Will be added as a listener for the [`'connect'`][] event once.
|
|
|
|
* Returns: {net.Socket} The socket itself.
|
2015-11-05 20:15:39 +01:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
Initiate an [IPC][] connection on the given socket.
|
2015-11-05 20:15:39 +01:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
Alias to
|
|
|
|
[`socket.connect(options[, connectListener])`][`socket.connect(options)`]
|
|
|
|
called with `{ path: path }` as `options`.
|
2015-11-05 20:15:39 +01:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
#### socket.connect(port[, host][, connectListener])
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2015-11-05 20:15:39 +01:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
* `port` {number} Port the client should connect to.
|
|
|
|
* `host` {string} Host the client should connect to.
|
|
|
|
* `connectListener` {Function} Common parameter of [`socket.connect()`][]
|
|
|
|
methods. Will be added as a listener for the [`'connect'`][] event once.
|
|
|
|
* Returns: {net.Socket} The socket itself.
|
|
|
|
|
|
|
|
Initiate a TCP connection on the given socket.
|
|
|
|
|
|
|
|
Alias to
|
|
|
|
[`socket.connect(options[, connectListener])`][`socket.connect(options)`]
|
|
|
|
called with `{port: port, host: host}` as `options`.
|
2015-11-05 20:15:39 +01:00
|
|
|
|
2016-04-26 22:27:08 +02:00
|
|
|
### socket.connecting
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v6.1.0
|
|
|
|
-->
|
2016-04-26 22:27:08 +02:00
|
|
|
|
2018-11-03 19:51:54 +01:00
|
|
|
If `true`,
|
2017-03-07 07:49:36 +01:00
|
|
|
[`socket.connect(options[, connectListener])`][`socket.connect(options)`]
|
2018-11-03 19:51:54 +01:00
|
|
|
was called and has not yet finished. Will be set to `true` before emitting
|
2018-04-09 18:30:22 +02:00
|
|
|
`'connect'` event and/or calling
|
2017-03-07 07:49:36 +01:00
|
|
|
[`socket.connect(options[, connectListener])`][`socket.connect(options)`]'s
|
|
|
|
callback.
|
2016-04-26 22:27:08 +02:00
|
|
|
|
2016-06-09 01:31:54 +02:00
|
|
|
### socket.destroy([exception])
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2015-11-05 20:15:39 +01:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `exception` {Object}
|
2017-06-07 21:48:35 +02:00
|
|
|
* Returns: {net.Socket}
|
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Ensures that no more I/O activity happens on this socket. Only necessary in
|
|
|
|
case of errors (parse error or so).
|
2010-12-16 00:47:02 +01:00
|
|
|
|
2016-06-09 01:31:54 +02:00
|
|
|
If `exception` is specified, an [`'error'`][] event will be emitted and any
|
|
|
|
listeners for that event will receive `exception` as an argument.
|
|
|
|
|
2016-04-09 06:48:13 +02:00
|
|
|
### socket.destroyed
|
|
|
|
|
2018-04-29 19:46:41 +02:00
|
|
|
* {boolean} Indicates if the connection is destroyed or not. Once a
|
|
|
|
connection is destroyed no further data can be transferred using it.
|
2016-04-09 06:48:13 +02:00
|
|
|
|
2018-10-28 15:19:24 +01:00
|
|
|
### socket.end([data][, encoding][, callback])
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `data` {string|Buffer|Uint8Array}
|
|
|
|
* `encoding` {string} Only used when data is `string`. **Default:** `'utf8'`.
|
2018-10-28 15:19:24 +01:00
|
|
|
* `callback` {Function} Optional callback for when the socket is finished.
|
2017-06-07 21:49:00 +02:00
|
|
|
* Returns: {net.Socket} The socket itself.
|
|
|
|
|
2011-08-31 15:12:34 +02:00
|
|
|
Half-closes the socket. i.e., it sends a FIN packet. It is possible the
|
2010-12-09 01:04:21 +01:00
|
|
|
server will still send some data.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-10-06 07:56:50 +02:00
|
|
|
If `data` is specified, it is equivalent to calling
|
2017-03-01 09:02:29 +01:00
|
|
|
`socket.write(data, encoding)` followed by [`socket.end()`][].
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### socket.localAddress
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.9.6
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
The string representation of the local IP address the remote client is
|
2017-04-26 19:16:12 +02:00
|
|
|
connecting on. For example, in a server listening on `'0.0.0.0'`, if a client
|
|
|
|
connects on `'192.168.1.1'`, the value of `socket.localAddress` would be
|
|
|
|
`'192.168.1.1'`.
|
2015-11-05 20:15:39 +01:00
|
|
|
|
|
|
|
### socket.localPort
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.9.6
|
|
|
|
-->
|
2015-11-05 20:15:39 +01:00
|
|
|
|
2018-08-26 18:02:27 +02:00
|
|
|
The numeric representation of the local port. For example, `80` or `21`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-02-27 20:09:34 +01:00
|
|
|
### socket.pause()
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-06-07 21:49:00 +02:00
|
|
|
* Returns: {net.Socket} The socket itself.
|
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
Pauses the reading of data. That is, [`'data'`][] events will not be emitted.
|
2010-11-14 11:12:04 +01:00
|
|
|
Useful to throttle back an upload.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2018-11-03 19:53:06 +01:00
|
|
|
### socket.pending
|
|
|
|
<!-- YAML
|
2018-11-14 01:57:14 +01:00
|
|
|
added: v11.2.0
|
2018-11-03 19:53:06 +01:00
|
|
|
-->
|
|
|
|
|
|
|
|
* {boolean}
|
|
|
|
|
|
|
|
This is `true` if the socket is not connected yet, either because `.connect()`
|
|
|
|
has not yet been called or because it is still in the process of connecting
|
|
|
|
(see [`socket.connecting`][]).
|
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### socket.ref()
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.9.1
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-06-07 21:49:00 +02:00
|
|
|
* Returns: {net.Socket} The socket itself.
|
|
|
|
|
2018-04-09 18:30:22 +02:00
|
|
|
Opposite of `unref()`, calling `ref()` on a previously `unref`ed socket will
|
|
|
|
*not* let the program exit if it's the only socket left (the default behavior).
|
|
|
|
If the socket is `ref`ed calling `ref` again will have no effect.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### socket.remoteAddress
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.5.10
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
The string representation of the remote IP address. For example,
|
2015-12-08 17:55:45 +01:00
|
|
|
`'74.125.127.100'` or `'2001:4860:a005::68'`. Value may be `undefined` if
|
|
|
|
the socket is destroyed (for example, if the client disconnected).
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### socket.remoteFamily
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.14
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
The string representation of the remote IP family. `'IPv4'` or `'IPv6'`.
|
2011-01-26 09:06:22 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### socket.remotePort
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.5.10
|
|
|
|
-->
|
2015-05-14 04:25:57 +02:00
|
|
|
|
2018-08-26 18:02:27 +02:00
|
|
|
The numeric representation of the remote port. For example, `80` or `21`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### socket.resume()
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-06-07 21:49:00 +02:00
|
|
|
* Returns: {net.Socket} The socket itself.
|
|
|
|
|
2017-03-01 09:02:29 +01:00
|
|
|
Resumes reading after a call to [`socket.pause()`][].
|
2015-11-05 20:15:39 +01:00
|
|
|
|
|
|
|
### socket.setEncoding([encoding])
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2015-11-05 20:15:39 +01:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `encoding` {string}
|
2017-06-07 21:49:00 +02:00
|
|
|
* Returns: {net.Socket} The socket itself.
|
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
Set the encoding for the socket as a [Readable Stream][]. See
|
2018-03-20 18:19:57 +01:00
|
|
|
[`readable.setEncoding()`][] for more information.
|
2015-05-23 07:48:13 +02:00
|
|
|
|
2014-09-30 01:32:34 +02:00
|
|
|
### socket.setKeepAlive([enable][, initialDelay])
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.92
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2018-04-02 03:44:32 +02:00
|
|
|
* `enable` {boolean} **Default:** `false`
|
|
|
|
* `initialDelay` {number} **Default:** `0`
|
2017-06-07 21:49:00 +02:00
|
|
|
* Returns: {net.Socket} The socket itself.
|
|
|
|
|
2010-11-14 11:12:04 +01:00
|
|
|
Enable/disable keep-alive functionality, and optionally set the initial
|
2011-01-05 20:53:56 +01:00
|
|
|
delay before the first keepalive probe is sent on an idle socket.
|
2011-12-27 09:43:58 +01:00
|
|
|
|
2010-11-14 11:12:04 +01:00
|
|
|
Set `initialDelay` (in milliseconds) to set the delay between the last
|
2018-04-29 19:46:41 +02:00
|
|
|
data packet received and the first keepalive probe. Setting `0` for
|
|
|
|
`initialDelay` will leave the value unchanged from the default
|
2018-04-02 03:44:32 +02:00
|
|
|
(or previous) setting.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### socket.setNoDelay([noDelay])
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2011-05-09 19:49:20 +02:00
|
|
|
|
2018-04-02 03:44:32 +02:00
|
|
|
* `noDelay` {boolean} **Default:** `true`
|
2017-06-07 21:49:00 +02:00
|
|
|
* Returns: {net.Socket} The socket itself.
|
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Disables the Nagle algorithm. By default TCP connections use the Nagle
|
|
|
|
algorithm, they buffer data before sending it off. Setting `true` for
|
|
|
|
`noDelay` will immediately fire off data each time `socket.write()` is called.
|
|
|
|
|
|
|
|
### socket.setTimeout(timeout[, callback])
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2015-11-05 20:15:39 +01:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `timeout` {number}
|
|
|
|
* `callback` {Function}
|
2017-06-07 21:49:00 +02:00
|
|
|
* Returns: {net.Socket} The socket itself.
|
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Sets the socket to timeout after `timeout` milliseconds of inactivity on
|
|
|
|
the socket. By default `net.Socket` do not have a timeout.
|
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
When an idle timeout is triggered the socket will receive a [`'timeout'`][]
|
2017-03-01 09:02:29 +01:00
|
|
|
event but the connection will not be severed. The user must manually call
|
|
|
|
[`socket.end()`][] or [`socket.destroy()`][] to end the connection.
|
|
|
|
|
|
|
|
```js
|
|
|
|
socket.setTimeout(3000);
|
|
|
|
socket.on('timeout', () => {
|
|
|
|
console.log('socket timeout');
|
|
|
|
socket.end();
|
2017-03-10 11:07:15 +01:00
|
|
|
});
|
2017-03-01 09:02:29 +01:00
|
|
|
```
|
2015-11-05 20:15:39 +01:00
|
|
|
|
|
|
|
If `timeout` is 0, then the existing idle timeout is disabled.
|
|
|
|
|
2017-12-06 19:15:58 +01:00
|
|
|
The optional `callback` parameter will be added as a one-time listener for the
|
2015-11-28 00:30:32 +01:00
|
|
|
[`'timeout'`][] event.
|
2015-11-05 20:15:39 +01:00
|
|
|
|
2012-07-13 21:08:32 +02:00
|
|
|
### socket.unref()
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.9.1
|
|
|
|
-->
|
2012-07-13 21:08:32 +02:00
|
|
|
|
2017-06-07 21:49:00 +02:00
|
|
|
* Returns: {net.Socket} The socket itself.
|
|
|
|
|
2018-04-09 18:30:22 +02:00
|
|
|
Calling `unref()` on a socket will allow the program to exit if this is the only
|
|
|
|
active socket in the event system. If the socket is already `unref`ed calling
|
|
|
|
`unref()` again will have no effect.
|
2012-07-13 21:08:32 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### socket.write(data[, encoding][, callback])
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2012-07-13 21:08:32 +02:00
|
|
|
|
2018-04-12 08:15:56 +02:00
|
|
|
* `data` {string|Buffer|Uint8Array}
|
|
|
|
* `encoding` {string} Only used when data is `string`. **Default:** `utf8`.
|
|
|
|
* `callback` {Function}
|
2018-04-11 20:07:14 +02:00
|
|
|
* Returns: {boolean}
|
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Sends data on the socket. The second parameter specifies the encoding in the
|
2018-04-02 07:38:48 +02:00
|
|
|
case of a string — it defaults to UTF8 encoding.
|
2012-07-13 21:08:32 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Returns `true` if the entire data was flushed successfully to the kernel
|
|
|
|
buffer. Returns `false` if all or part of the data was queued in user memory.
|
2015-11-28 00:30:32 +01:00
|
|
|
[`'drain'`][] will be emitted when the buffer is again free.
|
2015-05-22 18:35:57 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
The optional `callback` parameter will be executed when the data is finally
|
|
|
|
written out - this may not be immediately.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2018-04-29 19:46:41 +02:00
|
|
|
See `Writable` stream [`write()`][stream_writable_write] method for more
|
2018-04-12 08:15:56 +02:00
|
|
|
information.
|
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
## net.connect()
|
2014-07-16 16:04:34 +02:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
Aliases to
|
|
|
|
[`net.createConnection()`][`net.createConnection()`].
|
2011-04-13 18:19:15 +02:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
Possible signatures:
|
2013-01-01 03:01:42 +01:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
* [`net.connect(options[, connectListener])`][`net.connect(options)`]
|
|
|
|
* [`net.connect(path[, connectListener])`][`net.connect(path)`] for [IPC][]
|
|
|
|
connections.
|
|
|
|
* [`net.connect(port[, host][, connectListener])`][`net.connect(port, host)`]
|
|
|
|
for TCP connections.
|
2013-01-01 03:01:42 +01:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
### net.connect(options[, connectListener])
|
|
|
|
<!-- YAML
|
|
|
|
added: v0.7.0
|
|
|
|
-->
|
2018-07-12 19:48:11 +02:00
|
|
|
* `options` {Object}
|
|
|
|
* `connectListener` {Function}
|
2018-11-01 22:15:48 +01:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
Alias to
|
|
|
|
[`net.createConnection(options[, connectListener])`][`net.createConnection(options)`].
|
2013-01-01 03:01:42 +01:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
### net.connect(path[, connectListener])
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2018-07-12 19:48:11 +02:00
|
|
|
* `path` {string}
|
|
|
|
* `connectListener` {Function}
|
2013-01-01 03:01:42 +01:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
Alias to
|
|
|
|
[`net.createConnection(path[, connectListener])`][`net.createConnection(path)`].
|
2011-07-23 09:00:43 +02:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
### net.connect(port[, host][, connectListener])
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2018-07-12 19:48:11 +02:00
|
|
|
* `port` {number}
|
|
|
|
* `host` {string}
|
|
|
|
* `connectListener` {Function}
|
2011-07-23 09:00:43 +02:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
Alias to
|
|
|
|
[`net.createConnection(port[, host][, connectListener])`][`net.createConnection(port, host)`].
|
|
|
|
|
|
|
|
## net.createConnection()
|
2011-07-23 09:00:43 +02:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
A factory function, which creates a new [`net.Socket`][],
|
|
|
|
immediately initiates connection with [`socket.connect()`][],
|
|
|
|
then returns the `net.Socket` that starts the connection.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
When the connection is established, a [`'connect'`][] event will be emitted
|
|
|
|
on the returned socket. The last parameter `connectListener`, if supplied,
|
|
|
|
will be added as a listener for the [`'connect'`][] event **once**.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
Possible signatures:
|
|
|
|
|
|
|
|
* [`net.createConnection(options[, connectListener])`][`net.createConnection(options)`]
|
|
|
|
* [`net.createConnection(path[, connectListener])`][`net.createConnection(path)`]
|
|
|
|
for [IPC][] connections.
|
|
|
|
* [`net.createConnection(port[, host][, connectListener])`][`net.createConnection(port, host)`]
|
|
|
|
for TCP connections.
|
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
The [`net.connect()`][] function is an alias to this function.
|
2017-03-07 07:49:36 +01:00
|
|
|
|
|
|
|
### net.createConnection(options[, connectListener])
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2013-05-07 20:55:12 +02:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
* `options` {Object} Required. Will be passed to both the
|
|
|
|
[`new net.Socket([options])`][`new net.Socket(options)`] call and the
|
|
|
|
[`socket.connect(options[, connectListener])`][`socket.connect(options)`]
|
|
|
|
method.
|
|
|
|
* `connectListener` {Function} Common parameter of the
|
|
|
|
[`net.createConnection()`][] functions. If supplied, will be added as
|
|
|
|
a listener for the [`'connect'`][] event on the returned socket once.
|
|
|
|
* Returns: {net.Socket} The newly created socket used to start the connection.
|
2013-05-07 20:55:12 +02:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
For available options, see
|
|
|
|
[`new net.Socket([options])`][`new net.Socket(options)`]
|
|
|
|
and [`socket.connect(options[, connectListener])`][`socket.connect(options)`].
|
2016-08-31 02:00:41 +02:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
Additional options:
|
2013-05-07 20:55:12 +02:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
* `timeout` {number} If set, will be used to call
|
|
|
|
[`socket.setTimeout(timeout)`][] after the socket is created, but before
|
|
|
|
it starts the connection.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-04-06 01:44:02 +02:00
|
|
|
Following is an example of a client of the echo server described
|
|
|
|
in the [`net.createServer()`][] section:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const net = require('net');
|
2017-06-01 01:07:25 +02:00
|
|
|
const client = net.createConnection({ port: 8124 }, () => {
|
2018-03-25 16:27:38 +02:00
|
|
|
// 'connect' listener
|
2016-01-17 18:39:07 +01:00
|
|
|
console.log('connected to server!');
|
|
|
|
client.write('world!\r\n');
|
|
|
|
});
|
|
|
|
client.on('data', (data) => {
|
|
|
|
console.log(data.toString());
|
|
|
|
client.end();
|
|
|
|
});
|
|
|
|
client.on('end', () => {
|
|
|
|
console.log('disconnected from server');
|
|
|
|
});
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
To connect on the socket `/tmp/echo.sock` the second line would just be
|
2018-04-29 13:16:44 +02:00
|
|
|
changed to:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2017-06-01 01:07:25 +02:00
|
|
|
const client = net.createConnection({ path: '/tmp/echo.sock' });
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
### net.createConnection(path[, connectListener])
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2012-02-05 11:11:54 +01:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
* `path` {string} Path the socket should connect to. Will be passed to
|
|
|
|
[`socket.connect(path[, connectListener])`][`socket.connect(path)`].
|
|
|
|
See [Identifying paths for IPC connections][].
|
|
|
|
* `connectListener` {Function} Common parameter of the
|
|
|
|
[`net.createConnection()`][] functions, an "once" listener for the
|
|
|
|
`'connect'` event on the initiating socket. Will be passed to
|
|
|
|
[`socket.connect(path[, connectListener])`][`socket.connect(path)`].
|
|
|
|
* Returns: {net.Socket} The newly created socket used to start the connection.
|
|
|
|
|
|
|
|
Initiates an [IPC][] connection.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
This function creates a new [`net.Socket`][] with all options set to default,
|
|
|
|
immediately initiates connection with
|
|
|
|
[`socket.connect(path[, connectListener])`][`socket.connect(path)`],
|
|
|
|
then returns the `net.Socket` that starts the connection.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
### net.createConnection(port[, host][, connectListener])
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
* `port` {number} Port the socket should connect to. Will be passed to
|
|
|
|
[`socket.connect(port[, host][, connectListener])`][`socket.connect(port, host)`].
|
2017-10-27 21:51:28 +02:00
|
|
|
* `host` {string} Host the socket should connect to. Will be passed to
|
2017-03-07 07:49:36 +01:00
|
|
|
[`socket.connect(port[, host][, connectListener])`][`socket.connect(port, host)`].
|
2018-04-02 03:44:32 +02:00
|
|
|
**Default:** `'localhost'`.
|
2017-03-07 07:49:36 +01:00
|
|
|
* `connectListener` {Function} Common parameter of the
|
|
|
|
[`net.createConnection()`][] functions, an "once" listener for the
|
|
|
|
`'connect'` event on the initiating socket. Will be passed to
|
|
|
|
[`socket.connect(path[, connectListener])`][`socket.connect(port, host)`].
|
|
|
|
* Returns: {net.Socket} The newly created socket used to start the connection.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
Initiates a TCP connection.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-03-07 07:49:36 +01:00
|
|
|
This function creates a new [`net.Socket`][] with all options set to default,
|
|
|
|
immediately initiates connection with
|
|
|
|
[`socket.connect(port[, host][, connectListener])`][`socket.connect(port, host)`],
|
|
|
|
then returns the `net.Socket` that starts the connection.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
## net.createServer([options][, connectionListener])
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.5.0
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-03-01 09:02:29 +01:00
|
|
|
* `options` {Object}
|
2017-10-27 21:51:28 +02:00
|
|
|
* `allowHalfOpen` {boolean} Indicates whether half-opened TCP
|
2018-04-02 03:44:32 +02:00
|
|
|
connections are allowed. **Default:** `false`.
|
2017-10-27 21:51:28 +02:00
|
|
|
* `pauseOnConnect` {boolean} Indicates whether the socket should be
|
2018-04-02 03:44:32 +02:00
|
|
|
paused on incoming connections. **Default:** `false`.
|
2017-03-01 09:02:29 +01:00
|
|
|
* `connectionListener` {Function} Automatically set as a listener for the
|
2017-10-27 21:51:28 +02:00
|
|
|
[`'connection'`][] event.
|
2017-06-07 21:49:00 +02:00
|
|
|
* Returns: {net.Server}
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2018-11-01 22:15:48 +01:00
|
|
|
Creates a new TCP or [IPC][] server.
|
|
|
|
|
2017-03-01 09:02:29 +01:00
|
|
|
If `allowHalfOpen` is set to `true`, when the other end of the socket
|
|
|
|
sends a FIN packet, the server will only send a FIN packet back when
|
|
|
|
[`socket.end()`][] is explicitly called, until then the connection is
|
|
|
|
half-closed (non-readable but still writable). See [`'end'`][] event
|
2017-09-22 00:27:16 +02:00
|
|
|
and [RFC 1122][half-closed] (section 4.2.2.13) for more information.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-03-01 09:02:29 +01:00
|
|
|
If `pauseOnConnect` is set to `true`, then the socket associated with each
|
|
|
|
incoming connection will be paused, and no data will be read from its handle.
|
|
|
|
This allows connections to be passed between processes without any data being
|
|
|
|
read by the original process. To begin reading data from a paused socket, call
|
|
|
|
[`socket.resume()`][].
|
2011-08-31 15:12:34 +02:00
|
|
|
|
2018-04-29 19:46:41 +02:00
|
|
|
The server can be a TCP server or an [IPC][] server, depending on what it
|
2017-03-01 09:02:29 +01:00
|
|
|
[`listen()`][`server.listen()`] to.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-03-01 09:02:29 +01:00
|
|
|
Here is an example of an TCP echo server which listens for connections
|
2015-11-05 20:15:39 +01:00
|
|
|
on port 8124:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const net = require('net');
|
2016-01-24 10:35:35 +01:00
|
|
|
const server = net.createServer((c) => {
|
|
|
|
// 'connection' listener
|
2016-01-17 18:39:07 +01:00
|
|
|
console.log('client connected');
|
|
|
|
c.on('end', () => {
|
|
|
|
console.log('client disconnected');
|
|
|
|
});
|
|
|
|
c.write('hello\r\n');
|
|
|
|
c.pipe(c);
|
|
|
|
});
|
2016-02-21 19:07:27 +01:00
|
|
|
server.on('error', (err) => {
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
server.listen(8124, () => {
|
2016-01-17 18:39:07 +01:00
|
|
|
console.log('server bound');
|
|
|
|
});
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Test this by using `telnet`:
|
2010-11-14 11:12:04 +01:00
|
|
|
|
2017-03-10 11:07:15 +01:00
|
|
|
```console
|
|
|
|
$ telnet localhost 8124
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2010-11-14 11:12:04 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
To listen on the socket `/tmp/echo.sock` the third line from the last would
|
2018-04-29 13:16:44 +02:00
|
|
|
just be changed to:
|
2015-11-05 20:15:39 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2016-02-21 19:07:27 +01:00
|
|
|
server.listen('/tmp/echo.sock', () => {
|
|
|
|
console.log('server bound');
|
2016-01-24 10:35:35 +01:00
|
|
|
});
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2015-11-05 20:15:39 +01:00
|
|
|
|
|
|
|
Use `nc` to connect to a UNIX domain socket server:
|
|
|
|
|
2017-03-10 11:07:15 +01:00
|
|
|
```console
|
|
|
|
$ nc -U /tmp/echo.sock
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2010-11-14 11:12:04 +01:00
|
|
|
|
2012-02-27 20:09:34 +01:00
|
|
|
## net.isIP(input)
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.0
|
|
|
|
-->
|
2010-11-14 11:12:04 +01:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `input` {string}
|
2018-04-11 20:07:14 +02:00
|
|
|
* Returns: {integer}
|
2010-11-14 11:12:04 +01:00
|
|
|
|
2018-04-11 20:07:14 +02:00
|
|
|
Tests if input is an IP address. Returns `0` for invalid strings,
|
|
|
|
returns `4` for IP version 4 addresses, and returns `6` for IP version 6
|
|
|
|
addresses.
|
2010-11-14 11:12:04 +01:00
|
|
|
|
2012-02-27 20:09:34 +01:00
|
|
|
## net.isIPv4(input)
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.0
|
|
|
|
-->
|
2010-11-14 11:12:04 +01:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `input` {string}
|
2018-04-11 20:07:14 +02:00
|
|
|
* Returns: {boolean}
|
2010-11-14 11:12:04 +01:00
|
|
|
|
2018-04-11 20:07:14 +02:00
|
|
|
Returns `true` if input is a version 4 IP address, otherwise returns `false`.
|
2010-11-14 11:12:04 +01:00
|
|
|
|
2012-02-27 20:09:34 +01:00
|
|
|
## net.isIPv6(input)
|
2016-06-03 02:15:22 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.0
|
|
|
|
-->
|
2010-11-14 11:12:04 +01:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `input` {string}
|
2018-04-11 20:07:14 +02:00
|
|
|
* Returns: {boolean}
|
|
|
|
|
|
|
|
Returns `true` if input is a version 6 IP address, otherwise returns `false`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
[`'close'`]: #net_event_close
|
|
|
|
[`'connect'`]: #net_event_connect
|
|
|
|
[`'connection'`]: #net_event_connection
|
|
|
|
[`'data'`]: #net_event_data
|
|
|
|
[`'drain'`]: #net_event_drain
|
|
|
|
[`'end'`]: #net_event_end
|
|
|
|
[`'error'`]: #net_event_error_1
|
|
|
|
[`'listening'`]: #net_event_listening
|
|
|
|
[`'timeout'`]: #net_event_timeout
|
2017-05-08 18:30:13 +02:00
|
|
|
[`EventEmitter`]: events.html#events_class_eventemitter
|
2015-11-28 00:30:32 +01:00
|
|
|
[`child_process.fork()`]: child_process.html#child_process_child_process_fork_modulepath_args_options
|
2016-05-23 22:36:41 +02:00
|
|
|
[`dns.lookup()` hints]: dns.html#dns_supported_getaddrinfo_flags
|
2017-05-08 18:30:13 +02:00
|
|
|
[`dns.lookup()`]: dns.html#dns_dns_lookup_hostname_options_callback
|
|
|
|
[`net.Server`]: #net_class_net_server
|
|
|
|
[`net.Socket`]: #net_class_net_socket
|
|
|
|
[`net.connect()`]: #net_net_connect
|
2017-03-07 07:49:36 +01:00
|
|
|
[`net.connect(options)`]: #net_net_connect_options_connectlistener
|
|
|
|
[`net.connect(path)`]: #net_net_connect_path_connectlistener
|
|
|
|
[`net.connect(port, host)`]: #net_net_connect_port_host_connectlistener
|
|
|
|
[`net.createConnection()`]: #net_net_createconnection
|
|
|
|
[`net.createConnection(options)`]: #net_net_createconnection_options_connectlistener
|
|
|
|
[`net.createConnection(path)`]: #net_net_createconnection_path_connectlistener
|
|
|
|
[`net.createConnection(port, host)`]: #net_net_createconnection_port_host_connectlistener
|
2017-03-01 04:35:23 +01:00
|
|
|
[`net.createServer()`]: #net_net_createserver_options_connectionlistener
|
2017-03-07 07:49:36 +01:00
|
|
|
[`new net.Socket(options)`]: #net_new_net_socket_options
|
2018-11-27 20:49:21 +01:00
|
|
|
[`readable.setEncoding()`]: stream.html#stream_readable_setencoding_encoding
|
2017-05-08 18:30:13 +02:00
|
|
|
[`server.close()`]: #net_server_close_callback
|
2016-01-27 14:49:05 +01:00
|
|
|
[`server.getConnections()`]: #net_server_getconnections_callback
|
2017-03-01 09:02:29 +01:00
|
|
|
[`server.listen()`]: #net_server_listen
|
|
|
|
[`server.listen(handle)`]: #net_server_listen_handle_backlog_callback
|
|
|
|
[`server.listen(options)`]: #net_server_listen_options_callback
|
|
|
|
[`server.listen(path)`]: #net_server_listen_path_backlog_callback
|
2018-05-20 19:16:19 +02:00
|
|
|
[`socket(7)`]: http://man7.org/linux/man-pages/man7/socket.7.html
|
2017-03-07 07:49:36 +01:00
|
|
|
[`socket.connect()`]: #net_socket_connect
|
|
|
|
[`socket.connect(options)`]: #net_socket_connect_options_connectlistener
|
|
|
|
[`socket.connect(path)`]: #net_socket_connect_path_connectlistener
|
|
|
|
[`socket.connect(port, host)`]: #net_socket_connect_port_host_connectlistener
|
2018-11-03 19:53:06 +01:00
|
|
|
[`socket.connecting`]: #net_socket_connecting
|
2017-03-01 09:02:29 +01:00
|
|
|
[`socket.destroy()`]: #net_socket_destroy_exception
|
2018-10-28 15:19:24 +01:00
|
|
|
[`socket.end()`]: #net_socket_end_data_encoding_callback
|
2017-05-08 18:30:13 +02:00
|
|
|
[`socket.pause()`]: #net_socket_pause
|
|
|
|
[`socket.resume()`]: #net_socket_resume
|
2018-03-20 18:19:57 +01:00
|
|
|
[`socket.setEncoding()`]: #net_socket_setencoding_encoding
|
2015-11-28 00:30:32 +01:00
|
|
|
[`socket.setTimeout()`]: #net_socket_settimeout_timeout_callback
|
2017-03-07 07:49:36 +01:00
|
|
|
[`socket.setTimeout(timeout)`]: #net_socket_settimeout_timeout_callback
|
|
|
|
[IPC]: #net_ipc_support
|
2017-05-08 18:30:13 +02:00
|
|
|
[Identifying paths for IPC connections]: #net_identifying_paths_for_ipc_connections
|
2014-11-20 19:35:30 +01:00
|
|
|
[Readable Stream]: stream.html#stream_class_stream_readable
|
2017-05-08 18:30:13 +02:00
|
|
|
[duplex stream]: stream.html#stream_class_stream_duplex
|
2017-09-22 00:27:16 +02:00
|
|
|
[half-closed]: https://tools.ietf.org/html/rfc1122
|
2018-04-12 08:15:56 +02:00
|
|
|
[stream_writable_write]: stream.html#stream_writable_write_chunk_encoding_callback
|
2017-02-12 11:09:34 +01:00
|
|
|
[unspecified IPv4 address]: https://en.wikipedia.org/wiki/0.0.0.0
|
2017-05-08 18:30:13 +02:00
|
|
|
[unspecified IPv6 address]: https://en.wikipedia.org/wiki/IPv6_address#Unspecified_address
|