2012-02-27 20:09:34 +01:00
|
|
|
# net
|
2010-11-14 11:12:04 +01:00
|
|
|
|
2015-02-25 01:15:26 +01:00
|
|
|
Stability: 2 - Stable
|
2012-03-03 00:14:03 +01:00
|
|
|
|
2010-11-14 11:12:04 +01:00
|
|
|
The `net` module provides you with an asynchronous network wrapper. It contains
|
2015-02-25 12:24:45 +01:00
|
|
|
functions for creating both servers and clients (called streams). You can include
|
|
|
|
this module with `require('net');`.
|
2010-11-14 11:12:04 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
## Class: net.Server
|
2011-03-02 19:32:13 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
This class is used to create a TCP or local server.
|
2011-03-03 17:05:03 +01:00
|
|
|
|
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'
|
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'
|
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'
|
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
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
Emitted when an error occurs. The [`'close'`][] event will be called directly
|
2015-11-05 20:15:39 +01:00
|
|
|
following this event. See example in discussion of `server.listen`.
|
2011-06-21 16:53:02 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### Event: 'listening'
|
2010-11-14 11:12:04 +01:00
|
|
|
|
2015-11-05 20:15:39 +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()
|
2011-10-06 07:56:50 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Returns the bound address, the address family name and port of the server
|
|
|
|
as reported by the operating system.
|
|
|
|
Useful to find which port was assigned when giving getting an OS-assigned address.
|
|
|
|
Returns an object with three properties, e.g.
|
|
|
|
`{ port: 12346, family: 'IPv4', address: '127.0.0.1' }`
|
2011-10-06 07:56:50 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Example:
|
2011-10-06 07:56:50 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
var server = net.createServer((socket) => {
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
// grab a random port.
|
2016-02-21 19:07:27 +01:00
|
|
|
server.listen(() => {
|
2016-01-17 18:39:07 +01:00
|
|
|
address = server.address();
|
|
|
|
console.log('opened server on %j', address);
|
|
|
|
});
|
|
|
|
```
|
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])
|
2010-11-29 09:20:21 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Stops the server from accepting new connections and keeps existing
|
|
|
|
connections. This function is asynchronous, the server is finally
|
2015-11-28 00:30:32 +01:00
|
|
|
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
|
|
|
|
that event, it will be called with an Error as its only argument if the server
|
|
|
|
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
|
2013-12-19 23:57:30 +01:00
|
|
|
|
2016-01-27 14:49:05 +01: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
|
2015-11-05 20:15:39 +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)
|
2013-12-19 23:57:30 +01: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
|
|
|
|
2015-11-25 21:56:11 +01:00
|
|
|
### server.listen(handle[, backlog][, callback])
|
2012-06-12 19:02:52 +02:00
|
|
|
|
|
|
|
* `handle` {Object}
|
2015-11-25 21:56:11 +01:00
|
|
|
* `backlog` {Number}
|
2012-10-08 19:10:29 +02:00
|
|
|
* `callback` {Function}
|
2012-06-12 19:02:52 +02:00
|
|
|
|
|
|
|
The `handle` object can be set to either a server or socket (anything
|
|
|
|
with an underlying `_handle` member), or a `{fd: <n>}` object.
|
|
|
|
|
|
|
|
This will cause the server to accept connections on the specified
|
|
|
|
handle, but it is presumed that the file descriptor or handle has
|
|
|
|
already been bound to a port or domain socket.
|
|
|
|
|
|
|
|
Listening on a file descriptor is not supported on Windows.
|
|
|
|
|
|
|
|
This function is asynchronous. When the server has been bound,
|
2015-11-28 00:30:32 +01:00
|
|
|
[`'listening'`][] event will be emitted.
|
2015-07-28 06:00:32 +02:00
|
|
|
The last parameter `callback` will be added as a listener for the
|
2015-11-28 00:30:32 +01:00
|
|
|
[`'listening'`][] event.
|
2012-06-12 19:02:52 +02:00
|
|
|
|
2015-11-25 21:56:11 +01:00
|
|
|
The parameter `backlog` behaves the same as in
|
2016-02-22 08:28:55 +01:00
|
|
|
[`server.listen(port[, hostname][, backlog][, callback])`][`server.listen(port, host, backlog, callback)`].
|
2015-11-25 21:56:11 +01:00
|
|
|
|
2014-09-25 00:41:31 +02:00
|
|
|
### server.listen(options[, callback])
|
2014-08-22 22:51:53 +02:00
|
|
|
|
|
|
|
* `options` {Object} - Required. Supports the following properties:
|
|
|
|
* `port` {Number} - Optional.
|
|
|
|
* `host` {String} - Optional.
|
|
|
|
* `backlog` {Number} - Optional.
|
|
|
|
* `path` {String} - Optional.
|
|
|
|
* `exclusive` {Boolean} - Optional.
|
|
|
|
* `callback` {Function} - Optional.
|
|
|
|
|
|
|
|
The `port`, `host`, and `backlog` properties of `options`, as well as the
|
|
|
|
optional callback function, behave as they do on a call to
|
2016-02-22 08:28:55 +01:00
|
|
|
[`server.listen(port[, hostname][, backlog][, callback])`][`server.listen(port, host, backlog, callback)`].
|
|
|
|
Alternatively, the `path` option can be used to specify a UNIX socket.
|
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
|
|
|
|
2015-11-25 21:56:11 +01:00
|
|
|
### server.listen(path[, backlog][, callback])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
* `path` {String}
|
2015-11-25 21:56:11 +01:00
|
|
|
* `backlog` {Number}
|
2015-11-05 20:15:39 +01:00
|
|
|
* `callback` {Function}
|
2010-11-16 05:26:46 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Start a local socket server listening for connections on the given `path`.
|
2010-11-16 05:26:46 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
This function is asynchronous. When the server has been bound,
|
2015-11-28 00:30:32 +01:00
|
|
|
[`'listening'`][] event will be emitted. The last parameter `callback`
|
|
|
|
will be added as a listener for the [`'listening'`][] event.
|
2010-11-16 05:26:46 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
On UNIX, the local domain is usually known as the UNIX domain. The path is a
|
2016-05-09 19:33:09 +02:00
|
|
|
filesystem path name. It gets truncated to `sizeof(sockaddr_un.sun_path)`
|
|
|
|
bytes, decreased by 1. It varies on different operating system between 91 and
|
|
|
|
107 bytes. The typical values are 107 on Linux and 103 on OS X. The path is
|
|
|
|
subject to the same naming conventions and permissions checks as would be done
|
|
|
|
on file creation, will be visible in the filesystem, and will *persist until
|
|
|
|
unlinked*.
|
2010-11-16 05:26:46 +01:00
|
|
|
|
2015-11-05 20:15:39 +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 `..`
|
|
|
|
sequences. Despite appearances, the pipe name space is flat. Pipes will *not
|
|
|
|
persist*, they are removed when the last reference to them is closed. Do not
|
|
|
|
forget JavaScript string escaping requires paths to be specified with
|
|
|
|
double-backslashes, such as:
|
2010-11-16 05:26:46 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
net.createServer().listen(
|
|
|
|
path.join('\\\\?\\pipe', process.cwd(), 'myctl'))
|
2010-11-16 05:26:46 +01:00
|
|
|
|
2015-11-25 21:56:11 +01:00
|
|
|
The parameter `backlog` behaves the same as in
|
2016-02-22 08:28:55 +01:00
|
|
|
[`server.listen(port[, hostname][, backlog][, callback])`][`server.listen(port, host, backlog, callback)`].
|
2015-11-25 21:56:11 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### server.listen(port[, hostname][, backlog][, callback])
|
2010-11-16 05:26:46 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Begin accepting connections on the specified `port` and `hostname`. If the
|
|
|
|
`hostname` is omitted, the server will accept connections on any IPv6 address
|
|
|
|
(`::`) when IPv6 is available, or any IPv4 address (`0.0.0.0`) otherwise. A
|
|
|
|
port value of zero will assign a random port.
|
2012-07-13 21:08:32 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Backlog is the maximum length of the queue of pending connections.
|
|
|
|
The actual length will be determined by your OS through sysctl settings such as
|
|
|
|
`tcp_max_syn_backlog` and `somaxconn` on linux. The default value of this
|
|
|
|
parameter is 511 (not 512).
|
2012-07-13 21:08:32 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
This function is asynchronous. When the server has been bound,
|
2015-11-28 00:30:32 +01:00
|
|
|
[`'listening'`][] event will be emitted. The last parameter `callback`
|
|
|
|
will be added as a listener for the [`'listening'`][] event.
|
2015-05-22 18:35:57 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
One issue some users run into is getting `EADDRINUSE` errors. This means that
|
|
|
|
another server is already running on the requested port. One way of handling this
|
|
|
|
would be to wait a second and then try again. This can be done with
|
2012-07-13 21:08:32 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
server.on('error', (e) => {
|
|
|
|
if (e.code == 'EADDRINUSE') {
|
|
|
|
console.log('Address in use, retrying...');
|
|
|
|
setTimeout(() => {
|
|
|
|
server.close();
|
|
|
|
server.listen(PORT, HOST);
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
```
|
2012-07-13 21:08:32 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
(Note: All sockets in Node.js set `SO_REUSEADDR` already)
|
2015-05-22 18:35:57 +02:00
|
|
|
|
2016-01-18 14:36:48 +01:00
|
|
|
### server.listening
|
|
|
|
|
|
|
|
A Boolean indicating whether or not the server is listening for
|
|
|
|
connections.
|
|
|
|
|
2012-02-27 20:09:34 +01:00
|
|
|
### server.maxConnections
|
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()
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Opposite of `unref`, calling `ref` on a previously `unref`d server will *not*
|
|
|
|
let the program exit if it's the only server left (the default behavior). If
|
|
|
|
the server is `ref`d calling `ref` again will have no effect.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Returns `server`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### server.unref()
|
2011-08-31 15:12:34 +02:00
|
|
|
|
2015-11-05 20:15:39 +01: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`d calling
|
|
|
|
`unref` again will have no effect.
|
2011-08-31 15:12:34 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Returns `server`.
|
2011-08-31 15:12:34 +02:00
|
|
|
|
2012-02-27 20:09:34 +01:00
|
|
|
## Class: net.Socket
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-12-19 23:57:30 +01:00
|
|
|
This object is an abstraction of a TCP or local socket. `net.Socket`
|
2011-01-05 20:53:56 +01:00
|
|
|
instances implement a duplex Stream interface. They can be created by the
|
2015-11-28 00:30:32 +01:00
|
|
|
user and used as a client (with [`connect()`][]) or they can be created by Node.js
|
2010-10-28 14:18:16 +02:00
|
|
|
and passed to the user through the `'connection'` event of a server.
|
|
|
|
|
2012-02-27 20:09:34 +01:00
|
|
|
### new net.Socket([options])
|
2011-03-10 18:15:05 +01:00
|
|
|
|
|
|
|
Construct a new socket object.
|
|
|
|
|
|
|
|
`options` is an object with the following defaults:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
{
|
|
|
|
fd: null,
|
|
|
|
allowHalfOpen: false,
|
|
|
|
readable: false,
|
|
|
|
writable: false
|
|
|
|
}
|
|
|
|
```
|
2011-03-10 18:15:05 +01:00
|
|
|
|
2013-11-19 08:02:26 +01:00
|
|
|
`fd` allows you to specify the existing file descriptor of socket.
|
|
|
|
Set `readable` and/or `writable` to `true` to allow reads and/or writes on this
|
|
|
|
socket (NOTE: Works only when `fd` is passed).
|
2011-03-10 18:15:05 +01:00
|
|
|
About `allowHalfOpen`, refer to `createServer()` and `'end'` event.
|
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
`net.Socket` instances are [`EventEmitter`][] with the following events:
|
2015-02-25 12:24:45 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### Event: 'close'
|
2015-02-25 12:24:45 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
* `had_error` {Boolean} `true` if the socket had a transmission error.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Emitted once the socket is fully closed. The argument `had_error` is a boolean
|
|
|
|
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'
|
2015-02-25 12:24:45 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Emitted when a socket connection is successfully established.
|
2015-11-28 00:30:32 +01:00
|
|
|
See [`connect()`][].
|
2015-02-25 12:24:45 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### Event: 'data'
|
2015-02-25 12:24:45 +01:00
|
|
|
|
2016-01-19 17:03:15 +01:00
|
|
|
* {Buffer}
|
2015-05-14 04:25:57 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Emitted when data is received. The argument `data` will be a `Buffer` or
|
|
|
|
`String`. Encoding of data is set by `socket.setEncoding()`.
|
|
|
|
(See the [Readable Stream][] section for more information.)
|
2015-02-25 12:24:45 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Note that the __data will be lost__ if there is no listener when a `Socket`
|
|
|
|
emits a `'data'` event.
|
2015-02-25 12:24:45 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### Event: 'drain'
|
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
|
|
|
|
2015-11-05 20:15:39 +01: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'
|
2010-12-16 00:57:13 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Emitted when the other end of the socket sends a FIN packet.
|
2015-02-25 12:24:45 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
By default (`allowHalfOpen == false`) the socket will destroy its file
|
|
|
|
descriptor once it has written out its pending write queue. However, by
|
|
|
|
setting `allowHalfOpen == true` the socket will not automatically `end()`
|
|
|
|
its side allowing the user to write arbitrary amounts of data, with the
|
|
|
|
caveat that the user is required to `end()` their side now.
|
|
|
|
|
|
|
|
### Event: 'error'
|
|
|
|
|
2016-01-19 17:03:15 +01:00
|
|
|
* {Error}
|
2015-11-05 20:15:39 +01:00
|
|
|
|
|
|
|
Emitted when an error occurs. The `'close'` event will be called directly
|
|
|
|
following this event.
|
|
|
|
|
|
|
|
### Event: 'lookup'
|
|
|
|
|
|
|
|
Emitted after resolving the hostname but before connecting.
|
|
|
|
Not applicable to UNIX sockets.
|
|
|
|
|
2016-01-19 17:03:15 +01:00
|
|
|
* `err` {Error|Null} The error object. See [`dns.lookup()`][].
|
2015-11-05 20:15:39 +01:00
|
|
|
* `address` {String} The IP address.
|
2016-01-19 17:03:15 +01:00
|
|
|
* `family` {String|Null} The address type. See [`dns.lookup()`][].
|
2016-03-08 08:39:16 +01:00
|
|
|
* `host` {String} The hostname.
|
2015-11-05 20:15:39 +01:00
|
|
|
|
|
|
|
### Event: 'timeout'
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
See also: [`socket.setTimeout()`][]
|
2015-11-05 20:15:39 +01:00
|
|
|
|
|
|
|
### socket.address()
|
|
|
|
|
|
|
|
Returns the bound address, the address family name and port of the
|
|
|
|
socket as reported by the operating system. Returns an object with
|
|
|
|
three properties, e.g.
|
|
|
|
`{ 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
|
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
|
2015-11-28 00:30:32 +01:00
|
|
|
"throttle" the data flows in their program with [`pause()`][] and [`resume()`][].
|
2011-01-31 19:41:52 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### socket.bytesRead
|
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
|
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
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### socket.connect(options[, connectListener])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Opens the connection for a given socket.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
For TCP sockets, `options` argument should be an object which specifies:
|
|
|
|
|
|
|
|
- `port`: Port the client should connect to (Required).
|
|
|
|
|
|
|
|
- `host`: Host the client should connect to. Defaults to `'localhost'`.
|
|
|
|
|
|
|
|
- `localAddress`: Local interface to bind to for network connections.
|
|
|
|
|
|
|
|
- `localPort`: Local port to bind to for network connections.
|
|
|
|
|
|
|
|
- `family` : Version of IP stack. Defaults to `4`.
|
|
|
|
|
2016-04-01 22:59:09 +02:00
|
|
|
- `hints`: [`dns.lookup()` hints][]. Defaults to `0`.
|
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
- `lookup` : Custom lookup function. Defaults to `dns.lookup`.
|
|
|
|
|
|
|
|
For local domain sockets, `options` argument should be an object which
|
|
|
|
specifies:
|
|
|
|
|
|
|
|
- `path`: Path the client should connect to (Required).
|
|
|
|
|
|
|
|
Normally this method is not needed, as `net.createConnection` opens the
|
|
|
|
socket. Use this only if you are implementing a custom Socket.
|
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
This function is asynchronous. When the [`'connect'`][] event is emitted the
|
2015-11-05 20:15:39 +01:00
|
|
|
socket is established. If there is a problem connecting, the `'connect'` event
|
2015-11-28 00:30:32 +01:00
|
|
|
will not be emitted, the [`'error'`][] event will be emitted with the exception.
|
2015-11-05 20:15:39 +01:00
|
|
|
|
|
|
|
The `connectListener` parameter will be added as a listener for the
|
2015-11-28 00:30:32 +01:00
|
|
|
[`'connect'`][] event.
|
2015-11-05 20:15:39 +01:00
|
|
|
|
|
|
|
### socket.connect(path[, connectListener])
|
|
|
|
### socket.connect(port[, host][, connectListener])
|
|
|
|
|
2016-05-09 19:15:14 +02:00
|
|
|
As [`socket.connect(options[, connectListener])`][`socket.connect(options, connectListener)`],
|
2015-11-05 20:15:39 +01:00
|
|
|
with options either as either `{port: port, host: host}` or `{path: path}`.
|
|
|
|
|
2016-04-26 22:27:08 +02:00
|
|
|
### socket.connecting
|
|
|
|
|
2016-05-09 19:15:14 +02:00
|
|
|
If `true` - [`socket.connect(options[, connectListener])`][`socket.connect(options, connectListener)`] was called and
|
2016-04-26 22:27:08 +02:00
|
|
|
haven't yet finished. Will be set to `false` before emitting `connect` event
|
2016-05-09 19:15:14 +02:00
|
|
|
and/or calling [`socket.connect(options[, connectListener])`][`socket.connect(options, connectListener)`]'s callback.
|
2016-04-26 22:27:08 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### socket.destroy()
|
|
|
|
|
|
|
|
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
|
|
|
|
2014-09-30 01:32:34 +02:00
|
|
|
### socket.end([data][, encoding])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
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
|
|
|
|
`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
|
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
|
|
|
|
connecting on. For example, if you are listening on `'0.0.0.0'` and the
|
|
|
|
client connects on `'192.168.1.1'`, the value would be `'192.168.1.1'`.
|
|
|
|
|
|
|
|
### socket.localPort
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### socket.ref()
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Opposite of `unref`, calling `ref` on a previously `unref`d socket will *not*
|
|
|
|
let the program exit if it's the only socket left (the default behavior). If
|
|
|
|
the socket is `ref`d calling `ref` again will have no effect.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Returns `socket`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### socket.remoteAddress
|
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
|
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
|
2015-05-14 04:25:57 +02:00
|
|
|
|
2015-11-05 20:15:39 +01: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
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
Resumes reading after a call to [`pause()`][].
|
2015-11-05 20:15:39 +01:00
|
|
|
|
|
|
|
### socket.setEncoding([encoding])
|
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
Set the encoding for the socket as a [Readable Stream][]. See
|
|
|
|
[`stream.setEncoding()`][] for more information.
|
2015-05-23 07:48:13 +02:00
|
|
|
|
2014-09-30 01:32:34 +02:00
|
|
|
### socket.setKeepAlive([enable][, initialDelay])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
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
|
|
|
`enable` defaults to `false`.
|
|
|
|
|
2010-11-14 11:12:04 +01:00
|
|
|
Set `initialDelay` (in milliseconds) to set the delay between the last
|
|
|
|
data packet received and the first keepalive probe. Setting 0 for
|
|
|
|
initialDelay will leave the value unchanged from the default
|
2011-12-27 09:43:58 +01:00
|
|
|
(or previous) setting. Defaults to `0`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-05-23 07:48:13 +02:00
|
|
|
Returns `socket`.
|
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### socket.setNoDelay([noDelay])
|
2011-05-09 19:49:20 +02:00
|
|
|
|
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.
|
|
|
|
`noDelay` defaults to `true`.
|
|
|
|
|
|
|
|
Returns `socket`.
|
|
|
|
|
|
|
|
### socket.setTimeout(timeout[, callback])
|
|
|
|
|
|
|
|
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'`][]
|
|
|
|
event but the connection will not be severed. The user must manually [`end()`][]
|
|
|
|
or [`destroy()`][] the socket.
|
2015-11-05 20:15:39 +01:00
|
|
|
|
|
|
|
If `timeout` is 0, then the existing idle timeout is disabled.
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
Returns `socket`.
|
2011-05-09 19:49:20 +02:00
|
|
|
|
2012-07-13 21:08:32 +02:00
|
|
|
### socket.unref()
|
|
|
|
|
|
|
|
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`d calling
|
|
|
|
`unref` again will have no effect.
|
|
|
|
|
2015-05-22 18:35:57 +02:00
|
|
|
Returns `socket`.
|
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
### socket.write(data[, encoding][, callback])
|
2012-07-13 21:08:32 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Sends data on the socket. The second parameter specifies the encoding in the
|
|
|
|
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
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
## net.connect(options[, connectListener])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
A factory function, which returns a new [`net.Socket`][] and automatically
|
2015-11-14 04:21:49 +01:00
|
|
|
connects with the supplied `options`.
|
2014-07-16 16:04:34 +02:00
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
The options are passed to both the [`net.Socket`][] constructor and the
|
|
|
|
[`socket.connect`][] method.
|
2014-07-16 16:04:34 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
The `connectListener` parameter will be added as a listener for the
|
2015-11-28 00:30:32 +01:00
|
|
|
[`'connect'`][] event once.
|
2011-04-13 18:19:15 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Here is an example of a client of the previously described echo server:
|
2011-04-13 18:19:15 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const net = require('net');
|
2016-01-24 10:35:35 +01:00
|
|
|
const client = net.connect({port: 8124}, () => {
|
|
|
|
// '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');
|
|
|
|
});
|
|
|
|
```
|
2013-01-01 03:01:42 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
To connect on the socket `/tmp/echo.sock` the second line would just be
|
|
|
|
changed to
|
2013-01-01 03:01:42 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const client = net.connect({path: '/tmp/echo.sock'});
|
|
|
|
```
|
2013-01-01 03:01:42 +01:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
## net.connect(path[, connectListener])
|
2013-01-01 03:01:42 +01:00
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
A factory function, which returns a new unix [`net.Socket`][] and automatically
|
2015-11-14 04:21:49 +01:00
|
|
|
connects to the supplied `path`.
|
2011-07-23 09:00:43 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
The `connectListener` parameter will be added as a listener for the
|
2015-11-28 00:30:32 +01:00
|
|
|
[`'connect'`][] event once.
|
2011-07-23 09:00:43 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
## net.connect(port[, host][, connectListener])
|
2011-07-23 09:00:43 +02:00
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
A factory function, which returns a new [`net.Socket`][] and automatically
|
2015-11-14 04:21:49 +01:00
|
|
|
connects to the supplied `port` and `host`.
|
2011-07-23 09:00:43 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
If `host` is omitted, `'localhost'` will be assumed.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
The `connectListener` parameter will be added as a listener for the
|
2015-11-28 00:30:32 +01:00
|
|
|
[`'connect'`][] event once.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
## net.createConnection(options[, connectListener])
|
2013-05-07 20:55:12 +02:00
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
A factory function, which returns a new [`net.Socket`][] and automatically
|
2015-11-14 04:21:49 +01:00
|
|
|
connects with the supplied `options`.
|
2013-05-07 20:55:12 +02:00
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
The options are passed to both the [`net.Socket`][] constructor and the
|
|
|
|
[`socket.connect`][] method.
|
2013-05-07 20:55:12 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
The `connectListener` parameter will be added as a listener for the
|
2015-11-28 00:30:32 +01:00
|
|
|
[`'connect'`][] event once.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Here is an example of a client of the previously described echo server:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const net = require('net');
|
2016-02-13 21:40:00 +01:00
|
|
|
const client = net.createConnection({port: 8124}, () => {
|
2016-01-24 10:35:35 +01: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
|
|
|
|
changed to
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const client = net.connect({path: '/tmp/echo.sock'});
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
## net.createConnection(path[, connectListener])
|
2012-02-05 11:11:54 +01:00
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
A factory function, which returns a new unix [`net.Socket`][] and automatically
|
2015-11-14 04:21:49 +01:00
|
|
|
connects to the supplied `path`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
The `connectListener` parameter will be added as a listener for the
|
2015-11-28 00:30:32 +01:00
|
|
|
[`'connect'`][] event once.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
## net.createConnection(port[, host][, connectListener])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
A factory function, which returns a new [`net.Socket`][] and automatically
|
2015-11-14 04:21:49 +01:00
|
|
|
connects to the supplied `port` and `host`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
If `host` is omitted, `'localhost'` will be assumed.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
The `connectListener` parameter will be added as a listener for the
|
2015-11-28 00:30:32 +01:00
|
|
|
[`'connect'`][] event once.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
## net.createServer([options][, connectionListener])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Creates a new server. The `connectionListener` argument is
|
2015-11-28 00:30:32 +01:00
|
|
|
automatically set as a listener for the [`'connection'`][] event.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
`options` is an object with the following defaults:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
{
|
|
|
|
allowHalfOpen: false,
|
|
|
|
pauseOnConnect: false
|
|
|
|
}
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
If `allowHalfOpen` is `true`, then the socket won't automatically send a FIN
|
|
|
|
packet when the other end of the socket sends a FIN packet. The socket becomes
|
2015-11-28 00:30:32 +01:00
|
|
|
non-readable, but still writable. You should call the [`end()`][] method explicitly.
|
|
|
|
See [`'end'`][] event for more information.
|
2011-08-31 15:12:34 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
If `pauseOnConnect` is `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
|
2015-11-28 00:30:32 +01:00
|
|
|
original process. To begin reading data from a paused socket, call [`resume()`][].
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 20:15:39 +01:00
|
|
|
Here is an example of an echo server which listens for connections
|
|
|
|
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
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
|
|
|
telnet localhost 8124
|
|
|
|
```
|
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
|
|
|
|
just be changed to
|
|
|
|
|
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:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
nc -U /tmp/echo.sock
|
|
|
|
```
|
2010-11-14 11:12:04 +01:00
|
|
|
|
2012-02-27 20:09:34 +01:00
|
|
|
## net.isIP(input)
|
2010-11-14 11:12:04 +01: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.
|
|
|
|
|
|
|
|
|
2012-02-27 20:09:34 +01:00
|
|
|
## net.isIPv4(input)
|
2010-11-14 11:12:04 +01:00
|
|
|
|
|
|
|
Returns true if input is a version 4 IP address, otherwise returns false.
|
|
|
|
|
|
|
|
|
2012-02-27 20:09:34 +01:00
|
|
|
## net.isIPv6(input)
|
2010-11-14 11:12:04 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
[`child_process.fork()`]: child_process.html#child_process_child_process_fork_modulepath_args_options
|
|
|
|
[`connect()`]: #net_socket_connect_options_connectlistener
|
|
|
|
[`destroy()`]: #net_socket_destroy
|
|
|
|
[`dns.lookup()`]: dns.html#dns_dns_lookup_hostname_options_callback
|
2016-04-01 22:59:09 +02:00
|
|
|
[`dns.lookup()` hints]: #dns_supported_getaddrinfo_flags
|
2015-11-28 00:30:32 +01:00
|
|
|
[`end()`]: #net_socket_end_data_encoding
|
2016-04-13 12:30:14 +02:00
|
|
|
[`EventEmitter`]: events.html#events_class_eventemitter
|
2015-11-28 00:30:32 +01:00
|
|
|
[`net.Socket`]: #net_class_net_socket
|
|
|
|
[`pause()`]: #net_socket_pause
|
|
|
|
[`resume()`]: #net_socket_resume
|
2016-01-27 14:49:05 +01:00
|
|
|
[`server.getConnections()`]: #net_server_getconnections_callback
|
2016-02-22 08:28:55 +01:00
|
|
|
[`server.listen(port, host, backlog, callback)`]: #net_server_listen_port_hostname_backlog_callback
|
|
|
|
[`socket.connect(options, connectListener)`]: #net_socket_connect_options_connectlistener
|
2015-11-28 00:30:32 +01:00
|
|
|
[`socket.connect`]: #net_socket_connect_options_connectlistener
|
|
|
|
[`socket.setTimeout()`]: #net_socket_settimeout_timeout_callback
|
|
|
|
[`stream.setEncoding()`]: stream.html#stream_readable_setencoding_encoding
|
2014-11-20 19:35:30 +01:00
|
|
|
[Readable Stream]: stream.html#stream_class_stream_readable
|