11cb0ef41Sopenharmony_ci# Net
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci<!--introduced_in=v0.10.0-->
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci<!--lint disable maximum-line-length-->
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci> Stability: 2 - Stable
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci<!-- source_link=lib/net.js -->
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciThe `node:net` module provides an asynchronous network API for creating stream-based
121cb0ef41Sopenharmony_ciTCP or [IPC][] servers ([`net.createServer()`][]) and clients
131cb0ef41Sopenharmony_ci([`net.createConnection()`][]).
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciIt can be accessed using:
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci```js
181cb0ef41Sopenharmony_ciconst net = require('node:net');
191cb0ef41Sopenharmony_ci```
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci## IPC support
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciThe `node:net` module supports IPC with named pipes on Windows, and Unix domain
241cb0ef41Sopenharmony_cisockets on other operating systems.
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci### Identifying paths for IPC connections
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci[`net.connect()`][], [`net.createConnection()`][], [`server.listen()`][], and
291cb0ef41Sopenharmony_ci[`socket.connect()`][] take a `path` parameter to identify IPC endpoints.
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciOn Unix, the local domain is also known as the Unix domain. The path is a
321cb0ef41Sopenharmony_cifile system pathname. It gets truncated to an OS-dependent length of
331cb0ef41Sopenharmony_ci`sizeof(sockaddr_un.sun_path) - 1`. Typical values are 107 bytes on Linux and
341cb0ef41Sopenharmony_ci103 bytes on macOS. If a Node.js API abstraction creates the Unix domain socket,
351cb0ef41Sopenharmony_ciit will unlink the Unix domain socket as well. For example,
361cb0ef41Sopenharmony_ci[`net.createServer()`][] may create a Unix domain socket and
371cb0ef41Sopenharmony_ci[`server.close()`][] will unlink it. But if a user creates the Unix domain
381cb0ef41Sopenharmony_cisocket outside of these abstractions, the user will need to remove it. The same
391cb0ef41Sopenharmony_ciapplies when a Node.js API creates a Unix domain socket but the program then
401cb0ef41Sopenharmony_cicrashes. In short, a Unix domain socket will be visible in the file system and
411cb0ef41Sopenharmony_ciwill persist until unlinked.
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ciOn Windows, the local domain is implemented using a named pipe. The path _must_
441cb0ef41Sopenharmony_cirefer to an entry in `\\?\pipe\` or `\\.\pipe\`. Any characters are permitted,
451cb0ef41Sopenharmony_cibut the latter may do some processing of pipe names, such as resolving `..`
461cb0ef41Sopenharmony_cisequences. Despite how it might look, the pipe namespace is flat. Pipes will
471cb0ef41Sopenharmony_ci_not persist_. They are removed when the last reference to them is closed.
481cb0ef41Sopenharmony_ciUnlike Unix domain sockets, Windows will close and remove the pipe when the
491cb0ef41Sopenharmony_ciowning process exits.
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ciJavaScript string escaping requires paths to be specified with extra backslash
521cb0ef41Sopenharmony_ciescaping such as:
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci```js
551cb0ef41Sopenharmony_cinet.createServer().listen(
561cb0ef41Sopenharmony_ci  path.join('\\\\?\\pipe', process.cwd(), 'myctl'));
571cb0ef41Sopenharmony_ci```
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci## Class: `net.BlockList`
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci<!-- YAML
621cb0ef41Sopenharmony_ciadded:
631cb0ef41Sopenharmony_ci  - v15.0.0
641cb0ef41Sopenharmony_ci  - v14.18.0
651cb0ef41Sopenharmony_ci-->
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ciThe `BlockList` object can be used with some network APIs to specify rules for
681cb0ef41Sopenharmony_cidisabling inbound or outbound access to specific IP addresses, IP ranges, or
691cb0ef41Sopenharmony_ciIP subnets.
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci### `blockList.addAddress(address[, type])`
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci<!-- YAML
741cb0ef41Sopenharmony_ciadded:
751cb0ef41Sopenharmony_ci  - v15.0.0
761cb0ef41Sopenharmony_ci  - v14.18.0
771cb0ef41Sopenharmony_ci-->
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci* `address` {string|net.SocketAddress} An IPv4 or IPv6 address.
801cb0ef41Sopenharmony_ci* `type` {string} Either `'ipv4'` or `'ipv6'`. **Default:** `'ipv4'`.
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ciAdds a rule to block the given IP address.
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci### `blockList.addRange(start, end[, type])`
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci<!-- YAML
871cb0ef41Sopenharmony_ciadded:
881cb0ef41Sopenharmony_ci  - v15.0.0
891cb0ef41Sopenharmony_ci  - v14.18.0
901cb0ef41Sopenharmony_ci-->
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci* `start` {string|net.SocketAddress} The starting IPv4 or IPv6 address in the
931cb0ef41Sopenharmony_ci  range.
941cb0ef41Sopenharmony_ci* `end` {string|net.SocketAddress} The ending IPv4 or IPv6 address in the range.
951cb0ef41Sopenharmony_ci* `type` {string} Either `'ipv4'` or `'ipv6'`. **Default:** `'ipv4'`.
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ciAdds a rule to block a range of IP addresses from `start` (inclusive) to
981cb0ef41Sopenharmony_ci`end` (inclusive).
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci### `blockList.addSubnet(net, prefix[, type])`
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci<!-- YAML
1031cb0ef41Sopenharmony_ciadded:
1041cb0ef41Sopenharmony_ci  - v15.0.0
1051cb0ef41Sopenharmony_ci  - v14.18.0
1061cb0ef41Sopenharmony_ci-->
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci* `net` {string|net.SocketAddress} The network IPv4 or IPv6 address.
1091cb0ef41Sopenharmony_ci* `prefix` {number} The number of CIDR prefix bits. For IPv4, this
1101cb0ef41Sopenharmony_ci  must be a value between `0` and `32`. For IPv6, this must be between
1111cb0ef41Sopenharmony_ci  `0` and `128`.
1121cb0ef41Sopenharmony_ci* `type` {string} Either `'ipv4'` or `'ipv6'`. **Default:** `'ipv4'`.
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ciAdds a rule to block a range of IP addresses specified as a subnet mask.
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_ci### `blockList.check(address[, type])`
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci<!-- YAML
1191cb0ef41Sopenharmony_ciadded:
1201cb0ef41Sopenharmony_ci  - v15.0.0
1211cb0ef41Sopenharmony_ci  - v14.18.0
1221cb0ef41Sopenharmony_ci-->
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci* `address` {string|net.SocketAddress} The IP address to check
1251cb0ef41Sopenharmony_ci* `type` {string} Either `'ipv4'` or `'ipv6'`. **Default:** `'ipv4'`.
1261cb0ef41Sopenharmony_ci* Returns: {boolean}
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ciReturns `true` if the given IP address matches any of the rules added to the
1291cb0ef41Sopenharmony_ci`BlockList`.
1301cb0ef41Sopenharmony_ci
1311cb0ef41Sopenharmony_ci```js
1321cb0ef41Sopenharmony_ciconst blockList = new net.BlockList();
1331cb0ef41Sopenharmony_ciblockList.addAddress('123.123.123.123');
1341cb0ef41Sopenharmony_ciblockList.addRange('10.0.0.1', '10.0.0.10');
1351cb0ef41Sopenharmony_ciblockList.addSubnet('8592:757c:efae:4e45::', 64, 'ipv6');
1361cb0ef41Sopenharmony_ci
1371cb0ef41Sopenharmony_ciconsole.log(blockList.check('123.123.123.123'));  // Prints: true
1381cb0ef41Sopenharmony_ciconsole.log(blockList.check('10.0.0.3'));  // Prints: true
1391cb0ef41Sopenharmony_ciconsole.log(blockList.check('222.111.111.222'));  // Prints: false
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_ci// IPv6 notation for IPv4 addresses works:
1421cb0ef41Sopenharmony_ciconsole.log(blockList.check('::ffff:7b7b:7b7b', 'ipv6')); // Prints: true
1431cb0ef41Sopenharmony_ciconsole.log(blockList.check('::ffff:123.123.123.123', 'ipv6')); // Prints: true
1441cb0ef41Sopenharmony_ci```
1451cb0ef41Sopenharmony_ci
1461cb0ef41Sopenharmony_ci### `blockList.rules`
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ci<!-- YAML
1491cb0ef41Sopenharmony_ciadded:
1501cb0ef41Sopenharmony_ci  - v15.0.0
1511cb0ef41Sopenharmony_ci  - v14.18.0
1521cb0ef41Sopenharmony_ci-->
1531cb0ef41Sopenharmony_ci
1541cb0ef41Sopenharmony_ci* Type: {string\[]}
1551cb0ef41Sopenharmony_ci
1561cb0ef41Sopenharmony_ciThe list of rules added to the blocklist.
1571cb0ef41Sopenharmony_ci
1581cb0ef41Sopenharmony_ci## Class: `net.SocketAddress`
1591cb0ef41Sopenharmony_ci
1601cb0ef41Sopenharmony_ci<!-- YAML
1611cb0ef41Sopenharmony_ciadded:
1621cb0ef41Sopenharmony_ci  - v15.14.0
1631cb0ef41Sopenharmony_ci  - v14.18.0
1641cb0ef41Sopenharmony_ci-->
1651cb0ef41Sopenharmony_ci
1661cb0ef41Sopenharmony_ci### `new net.SocketAddress([options])`
1671cb0ef41Sopenharmony_ci
1681cb0ef41Sopenharmony_ci<!-- YAML
1691cb0ef41Sopenharmony_ciadded:
1701cb0ef41Sopenharmony_ci  - v15.14.0
1711cb0ef41Sopenharmony_ci  - v14.18.0
1721cb0ef41Sopenharmony_ci-->
1731cb0ef41Sopenharmony_ci
1741cb0ef41Sopenharmony_ci* `options` {Object}
1751cb0ef41Sopenharmony_ci  * `address` {string} The network address as either an IPv4 or IPv6 string.
1761cb0ef41Sopenharmony_ci    **Default**: `'127.0.0.1'` if `family` is `'ipv4'`; `'::'` if `family` is
1771cb0ef41Sopenharmony_ci    `'ipv6'`.
1781cb0ef41Sopenharmony_ci  * `family` {string} One of either `'ipv4'` or `'ipv6'`.
1791cb0ef41Sopenharmony_ci    **Default**: `'ipv4'`.
1801cb0ef41Sopenharmony_ci  * `flowlabel` {number} An IPv6 flow-label used only if `family` is `'ipv6'`.
1811cb0ef41Sopenharmony_ci  * `port` {number} An IP port.
1821cb0ef41Sopenharmony_ci
1831cb0ef41Sopenharmony_ci### `socketaddress.address`
1841cb0ef41Sopenharmony_ci
1851cb0ef41Sopenharmony_ci<!-- YAML
1861cb0ef41Sopenharmony_ciadded:
1871cb0ef41Sopenharmony_ci  - v15.14.0
1881cb0ef41Sopenharmony_ci  - v14.18.0
1891cb0ef41Sopenharmony_ci-->
1901cb0ef41Sopenharmony_ci
1911cb0ef41Sopenharmony_ci* Type {string}
1921cb0ef41Sopenharmony_ci
1931cb0ef41Sopenharmony_ci### `socketaddress.family`
1941cb0ef41Sopenharmony_ci
1951cb0ef41Sopenharmony_ci<!-- YAML
1961cb0ef41Sopenharmony_ciadded:
1971cb0ef41Sopenharmony_ci  - v15.14.0
1981cb0ef41Sopenharmony_ci  - v14.18.0
1991cb0ef41Sopenharmony_ci-->
2001cb0ef41Sopenharmony_ci
2011cb0ef41Sopenharmony_ci* Type {string} Either `'ipv4'` or `'ipv6'`.
2021cb0ef41Sopenharmony_ci
2031cb0ef41Sopenharmony_ci### `socketaddress.flowlabel`
2041cb0ef41Sopenharmony_ci
2051cb0ef41Sopenharmony_ci<!-- YAML
2061cb0ef41Sopenharmony_ciadded:
2071cb0ef41Sopenharmony_ci  - v15.14.0
2081cb0ef41Sopenharmony_ci  - v14.18.0
2091cb0ef41Sopenharmony_ci-->
2101cb0ef41Sopenharmony_ci
2111cb0ef41Sopenharmony_ci* Type {number}
2121cb0ef41Sopenharmony_ci
2131cb0ef41Sopenharmony_ci### `socketaddress.port`
2141cb0ef41Sopenharmony_ci
2151cb0ef41Sopenharmony_ci<!-- YAML
2161cb0ef41Sopenharmony_ciadded:
2171cb0ef41Sopenharmony_ci  - v15.14.0
2181cb0ef41Sopenharmony_ci  - v14.18.0
2191cb0ef41Sopenharmony_ci-->
2201cb0ef41Sopenharmony_ci
2211cb0ef41Sopenharmony_ci* Type {number}
2221cb0ef41Sopenharmony_ci
2231cb0ef41Sopenharmony_ci## Class: `net.Server`
2241cb0ef41Sopenharmony_ci
2251cb0ef41Sopenharmony_ci<!-- YAML
2261cb0ef41Sopenharmony_ciadded: v0.1.90
2271cb0ef41Sopenharmony_ci-->
2281cb0ef41Sopenharmony_ci
2291cb0ef41Sopenharmony_ci* Extends: {EventEmitter}
2301cb0ef41Sopenharmony_ci
2311cb0ef41Sopenharmony_ciThis class is used to create a TCP or [IPC][] server.
2321cb0ef41Sopenharmony_ci
2331cb0ef41Sopenharmony_ci### `new net.Server([options][, connectionListener])`
2341cb0ef41Sopenharmony_ci
2351cb0ef41Sopenharmony_ci* `options` {Object} See
2361cb0ef41Sopenharmony_ci  [`net.createServer([options][, connectionListener])`][`net.createServer()`].
2371cb0ef41Sopenharmony_ci* `connectionListener` {Function} Automatically set as a listener for the
2381cb0ef41Sopenharmony_ci  [`'connection'`][] event.
2391cb0ef41Sopenharmony_ci* Returns: {net.Server}
2401cb0ef41Sopenharmony_ci
2411cb0ef41Sopenharmony_ci`net.Server` is an [`EventEmitter`][] with the following events:
2421cb0ef41Sopenharmony_ci
2431cb0ef41Sopenharmony_ci### Event: `'close'`
2441cb0ef41Sopenharmony_ci
2451cb0ef41Sopenharmony_ci<!-- YAML
2461cb0ef41Sopenharmony_ciadded: v0.5.0
2471cb0ef41Sopenharmony_ci-->
2481cb0ef41Sopenharmony_ci
2491cb0ef41Sopenharmony_ciEmitted when the server closes. If connections exist, this
2501cb0ef41Sopenharmony_cievent is not emitted until all connections are ended.
2511cb0ef41Sopenharmony_ci
2521cb0ef41Sopenharmony_ci### Event: `'connection'`
2531cb0ef41Sopenharmony_ci
2541cb0ef41Sopenharmony_ci<!-- YAML
2551cb0ef41Sopenharmony_ciadded: v0.1.90
2561cb0ef41Sopenharmony_ci-->
2571cb0ef41Sopenharmony_ci
2581cb0ef41Sopenharmony_ci* {net.Socket} The connection object
2591cb0ef41Sopenharmony_ci
2601cb0ef41Sopenharmony_ciEmitted when a new connection is made. `socket` is an instance of
2611cb0ef41Sopenharmony_ci`net.Socket`.
2621cb0ef41Sopenharmony_ci
2631cb0ef41Sopenharmony_ci### Event: `'error'`
2641cb0ef41Sopenharmony_ci
2651cb0ef41Sopenharmony_ci<!-- YAML
2661cb0ef41Sopenharmony_ciadded: v0.1.90
2671cb0ef41Sopenharmony_ci-->
2681cb0ef41Sopenharmony_ci
2691cb0ef41Sopenharmony_ci* {Error}
2701cb0ef41Sopenharmony_ci
2711cb0ef41Sopenharmony_ciEmitted when an error occurs. Unlike [`net.Socket`][], the [`'close'`][]
2721cb0ef41Sopenharmony_cievent will **not** be emitted directly following this event unless
2731cb0ef41Sopenharmony_ci[`server.close()`][] is manually called. See the example in discussion of
2741cb0ef41Sopenharmony_ci[`server.listen()`][].
2751cb0ef41Sopenharmony_ci
2761cb0ef41Sopenharmony_ci### Event: `'listening'`
2771cb0ef41Sopenharmony_ci
2781cb0ef41Sopenharmony_ci<!-- YAML
2791cb0ef41Sopenharmony_ciadded: v0.1.90
2801cb0ef41Sopenharmony_ci-->
2811cb0ef41Sopenharmony_ci
2821cb0ef41Sopenharmony_ciEmitted when the server has been bound after calling [`server.listen()`][].
2831cb0ef41Sopenharmony_ci
2841cb0ef41Sopenharmony_ci### Event: `'drop'`
2851cb0ef41Sopenharmony_ci
2861cb0ef41Sopenharmony_ci<!-- YAML
2871cb0ef41Sopenharmony_ciadded: v18.6.0
2881cb0ef41Sopenharmony_ci-->
2891cb0ef41Sopenharmony_ci
2901cb0ef41Sopenharmony_ciWhen the number of connections reaches the threshold of `server.maxConnections`,
2911cb0ef41Sopenharmony_cithe server will drop new connections and emit `'drop'` event instead. If it is a
2921cb0ef41Sopenharmony_ciTCP server, the argument is as follows, otherwise the argument is `undefined`.
2931cb0ef41Sopenharmony_ci
2941cb0ef41Sopenharmony_ci* `data` {Object} The argument passed to event listener.
2951cb0ef41Sopenharmony_ci  * `localAddress` {string}  Local address.
2961cb0ef41Sopenharmony_ci  * `localPort` {number} Local port.
2971cb0ef41Sopenharmony_ci  * `localFamily` {string} Local family.
2981cb0ef41Sopenharmony_ci  * `remoteAddress` {string} Remote address.
2991cb0ef41Sopenharmony_ci  * `remotePort` {number} Remote port.
3001cb0ef41Sopenharmony_ci  * `remoteFamily` {string} Remote IP family. `'IPv4'` or `'IPv6'`.
3011cb0ef41Sopenharmony_ci
3021cb0ef41Sopenharmony_ci### `server.address()`
3031cb0ef41Sopenharmony_ci
3041cb0ef41Sopenharmony_ci<!-- YAML
3051cb0ef41Sopenharmony_ciadded: v0.1.90
3061cb0ef41Sopenharmony_cichanges:
3071cb0ef41Sopenharmony_ci  - version: v18.4.0
3081cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/43054
3091cb0ef41Sopenharmony_ci    description: The `family` property now returns a string instead of a number.
3101cb0ef41Sopenharmony_ci  - version: v18.0.0
3111cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/41431
3121cb0ef41Sopenharmony_ci    description: The `family` property now returns a number instead of a string.
3131cb0ef41Sopenharmony_ci-->
3141cb0ef41Sopenharmony_ci
3151cb0ef41Sopenharmony_ci* Returns: {Object|string|null}
3161cb0ef41Sopenharmony_ci
3171cb0ef41Sopenharmony_ciReturns the bound `address`, the address `family` name, and `port` of the server
3181cb0ef41Sopenharmony_cias reported by the operating system if listening on an IP socket
3191cb0ef41Sopenharmony_ci(useful to find which port was assigned when getting an OS-assigned address):
3201cb0ef41Sopenharmony_ci`{ port: 12346, family: 'IPv4', address: '127.0.0.1' }`.
3211cb0ef41Sopenharmony_ci
3221cb0ef41Sopenharmony_ciFor a server listening on a pipe or Unix domain socket, the name is returned
3231cb0ef41Sopenharmony_cias a string.
3241cb0ef41Sopenharmony_ci
3251cb0ef41Sopenharmony_ci```js
3261cb0ef41Sopenharmony_ciconst server = net.createServer((socket) => {
3271cb0ef41Sopenharmony_ci  socket.end('goodbye\n');
3281cb0ef41Sopenharmony_ci}).on('error', (err) => {
3291cb0ef41Sopenharmony_ci  // Handle errors here.
3301cb0ef41Sopenharmony_ci  throw err;
3311cb0ef41Sopenharmony_ci});
3321cb0ef41Sopenharmony_ci
3331cb0ef41Sopenharmony_ci// Grab an arbitrary unused port.
3341cb0ef41Sopenharmony_ciserver.listen(() => {
3351cb0ef41Sopenharmony_ci  console.log('opened server on', server.address());
3361cb0ef41Sopenharmony_ci});
3371cb0ef41Sopenharmony_ci```
3381cb0ef41Sopenharmony_ci
3391cb0ef41Sopenharmony_ci`server.address()` returns `null` before the `'listening'` event has been
3401cb0ef41Sopenharmony_ciemitted or after calling `server.close()`.
3411cb0ef41Sopenharmony_ci
3421cb0ef41Sopenharmony_ci### `server.close([callback])`
3431cb0ef41Sopenharmony_ci
3441cb0ef41Sopenharmony_ci<!-- YAML
3451cb0ef41Sopenharmony_ciadded: v0.1.90
3461cb0ef41Sopenharmony_ci-->
3471cb0ef41Sopenharmony_ci
3481cb0ef41Sopenharmony_ci* `callback` {Function} Called when the server is closed.
3491cb0ef41Sopenharmony_ci* Returns: {net.Server}
3501cb0ef41Sopenharmony_ci
3511cb0ef41Sopenharmony_ciStops the server from accepting new connections and keeps existing
3521cb0ef41Sopenharmony_ciconnections. This function is asynchronous, the server is finally closed
3531cb0ef41Sopenharmony_ciwhen all connections are ended and the server emits a [`'close'`][] event.
3541cb0ef41Sopenharmony_ciThe optional `callback` will be called once the `'close'` event occurs. Unlike
3551cb0ef41Sopenharmony_cithat event, it will be called with an `Error` as its only argument if the server
3561cb0ef41Sopenharmony_ciwas not open when it was closed.
3571cb0ef41Sopenharmony_ci
3581cb0ef41Sopenharmony_ci### `server[Symbol.asyncDispose]()`
3591cb0ef41Sopenharmony_ci
3601cb0ef41Sopenharmony_ci<!-- YAML
3611cb0ef41Sopenharmony_ciadded: v18.18.0
3621cb0ef41Sopenharmony_ci-->
3631cb0ef41Sopenharmony_ci
3641cb0ef41Sopenharmony_ci> Stability: 1 - Experimental
3651cb0ef41Sopenharmony_ci
3661cb0ef41Sopenharmony_ciCalls [`server.close()`][] and returns a promise that fulfills when the
3671cb0ef41Sopenharmony_ciserver has closed.
3681cb0ef41Sopenharmony_ci
3691cb0ef41Sopenharmony_ci### `server.getConnections(callback)`
3701cb0ef41Sopenharmony_ci
3711cb0ef41Sopenharmony_ci<!-- YAML
3721cb0ef41Sopenharmony_ciadded: v0.9.7
3731cb0ef41Sopenharmony_ci-->
3741cb0ef41Sopenharmony_ci
3751cb0ef41Sopenharmony_ci* `callback` {Function}
3761cb0ef41Sopenharmony_ci* Returns: {net.Server}
3771cb0ef41Sopenharmony_ci
3781cb0ef41Sopenharmony_ciAsynchronously get the number of concurrent connections on the server. Works
3791cb0ef41Sopenharmony_ciwhen sockets were sent to forks.
3801cb0ef41Sopenharmony_ci
3811cb0ef41Sopenharmony_ciCallback should take two arguments `err` and `count`.
3821cb0ef41Sopenharmony_ci
3831cb0ef41Sopenharmony_ci### `server.listen()`
3841cb0ef41Sopenharmony_ci
3851cb0ef41Sopenharmony_ciStart a server listening for connections. A `net.Server` can be a TCP or
3861cb0ef41Sopenharmony_cian [IPC][] server depending on what it listens to.
3871cb0ef41Sopenharmony_ci
3881cb0ef41Sopenharmony_ciPossible signatures:
3891cb0ef41Sopenharmony_ci
3901cb0ef41Sopenharmony_ci* [`server.listen(handle[, backlog][, callback])`][`server.listen(handle)`]
3911cb0ef41Sopenharmony_ci* [`server.listen(options[, callback])`][`server.listen(options)`]
3921cb0ef41Sopenharmony_ci* [`server.listen(path[, backlog][, callback])`][`server.listen(path)`]
3931cb0ef41Sopenharmony_ci  for [IPC][] servers
3941cb0ef41Sopenharmony_ci* [`server.listen([port[, host[, backlog]]][, callback])`][`server.listen(port)`]
3951cb0ef41Sopenharmony_ci  for TCP servers
3961cb0ef41Sopenharmony_ci
3971cb0ef41Sopenharmony_ciThis function is asynchronous. When the server starts listening, the
3981cb0ef41Sopenharmony_ci[`'listening'`][] event will be emitted. The last parameter `callback`
3991cb0ef41Sopenharmony_ciwill be added as a listener for the [`'listening'`][] event.
4001cb0ef41Sopenharmony_ci
4011cb0ef41Sopenharmony_ciAll `listen()` methods can take a `backlog` parameter to specify the maximum
4021cb0ef41Sopenharmony_cilength of the queue of pending connections. The actual length will be determined
4031cb0ef41Sopenharmony_ciby the OS through sysctl settings such as `tcp_max_syn_backlog` and `somaxconn`
4041cb0ef41Sopenharmony_cion Linux. The default value of this parameter is 511 (not 512).
4051cb0ef41Sopenharmony_ci
4061cb0ef41Sopenharmony_ciAll [`net.Socket`][] are set to `SO_REUSEADDR` (see [`socket(7)`][] for
4071cb0ef41Sopenharmony_cidetails).
4081cb0ef41Sopenharmony_ci
4091cb0ef41Sopenharmony_ciThe `server.listen()` method can be called again if and only if there was an
4101cb0ef41Sopenharmony_cierror during the first `server.listen()` call or `server.close()` has been
4111cb0ef41Sopenharmony_cicalled. Otherwise, an `ERR_SERVER_ALREADY_LISTEN` error will be thrown.
4121cb0ef41Sopenharmony_ci
4131cb0ef41Sopenharmony_ciOne of the most common errors raised when listening is `EADDRINUSE`.
4141cb0ef41Sopenharmony_ciThis happens when another server is already listening on the requested
4151cb0ef41Sopenharmony_ci`port`/`path`/`handle`. One way to handle this would be to retry
4161cb0ef41Sopenharmony_ciafter a certain amount of time:
4171cb0ef41Sopenharmony_ci
4181cb0ef41Sopenharmony_ci```js
4191cb0ef41Sopenharmony_ciserver.on('error', (e) => {
4201cb0ef41Sopenharmony_ci  if (e.code === 'EADDRINUSE') {
4211cb0ef41Sopenharmony_ci    console.error('Address in use, retrying...');
4221cb0ef41Sopenharmony_ci    setTimeout(() => {
4231cb0ef41Sopenharmony_ci      server.close();
4241cb0ef41Sopenharmony_ci      server.listen(PORT, HOST);
4251cb0ef41Sopenharmony_ci    }, 1000);
4261cb0ef41Sopenharmony_ci  }
4271cb0ef41Sopenharmony_ci});
4281cb0ef41Sopenharmony_ci```
4291cb0ef41Sopenharmony_ci
4301cb0ef41Sopenharmony_ci#### `server.listen(handle[, backlog][, callback])`
4311cb0ef41Sopenharmony_ci
4321cb0ef41Sopenharmony_ci<!-- YAML
4331cb0ef41Sopenharmony_ciadded: v0.5.10
4341cb0ef41Sopenharmony_ci-->
4351cb0ef41Sopenharmony_ci
4361cb0ef41Sopenharmony_ci* `handle` {Object}
4371cb0ef41Sopenharmony_ci* `backlog` {number} Common parameter of [`server.listen()`][] functions
4381cb0ef41Sopenharmony_ci* `callback` {Function}
4391cb0ef41Sopenharmony_ci* Returns: {net.Server}
4401cb0ef41Sopenharmony_ci
4411cb0ef41Sopenharmony_ciStart a server listening for connections on a given `handle` that has
4421cb0ef41Sopenharmony_cialready been bound to a port, a Unix domain socket, or a Windows named pipe.
4431cb0ef41Sopenharmony_ci
4441cb0ef41Sopenharmony_ciThe `handle` object can be either a server, a socket (anything with an
4451cb0ef41Sopenharmony_ciunderlying `_handle` member), or an object with an `fd` member that is a
4461cb0ef41Sopenharmony_civalid file descriptor.
4471cb0ef41Sopenharmony_ci
4481cb0ef41Sopenharmony_ciListening on a file descriptor is not supported on Windows.
4491cb0ef41Sopenharmony_ci
4501cb0ef41Sopenharmony_ci#### `server.listen(options[, callback])`
4511cb0ef41Sopenharmony_ci
4521cb0ef41Sopenharmony_ci<!-- YAML
4531cb0ef41Sopenharmony_ciadded: v0.11.14
4541cb0ef41Sopenharmony_cichanges:
4551cb0ef41Sopenharmony_ci  - version: v15.6.0
4561cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/36623
4571cb0ef41Sopenharmony_ci    description: AbortSignal support was added.
4581cb0ef41Sopenharmony_ci  - version: v11.4.0
4591cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/23798
4601cb0ef41Sopenharmony_ci    description: The `ipv6Only` option is supported.
4611cb0ef41Sopenharmony_ci-->
4621cb0ef41Sopenharmony_ci
4631cb0ef41Sopenharmony_ci* `options` {Object} Required. Supports the following properties:
4641cb0ef41Sopenharmony_ci  * `port` {number}
4651cb0ef41Sopenharmony_ci  * `host` {string}
4661cb0ef41Sopenharmony_ci  * `path` {string} Will be ignored if `port` is specified. See
4671cb0ef41Sopenharmony_ci    [Identifying paths for IPC connections][].
4681cb0ef41Sopenharmony_ci  * `backlog` {number} Common parameter of [`server.listen()`][]
4691cb0ef41Sopenharmony_ci    functions.
4701cb0ef41Sopenharmony_ci  * `exclusive` {boolean} **Default:** `false`
4711cb0ef41Sopenharmony_ci  * `readableAll` {boolean} For IPC servers makes the pipe readable
4721cb0ef41Sopenharmony_ci    for all users. **Default:** `false`.
4731cb0ef41Sopenharmony_ci  * `writableAll` {boolean} For IPC servers makes the pipe writable
4741cb0ef41Sopenharmony_ci    for all users. **Default:** `false`.
4751cb0ef41Sopenharmony_ci  * `ipv6Only` {boolean} For TCP servers, setting `ipv6Only` to `true` will
4761cb0ef41Sopenharmony_ci    disable dual-stack support, i.e., binding to host `::` won't make
4771cb0ef41Sopenharmony_ci    `0.0.0.0` be bound. **Default:** `false`.
4781cb0ef41Sopenharmony_ci  * `signal` {AbortSignal} An AbortSignal that may be used to close a listening server.
4791cb0ef41Sopenharmony_ci* `callback` {Function}
4801cb0ef41Sopenharmony_ci  functions.
4811cb0ef41Sopenharmony_ci* Returns: {net.Server}
4821cb0ef41Sopenharmony_ci
4831cb0ef41Sopenharmony_ciIf `port` is specified, it behaves the same as
4841cb0ef41Sopenharmony_ci[`server.listen([port[, host[, backlog]]][, callback])`][`server.listen(port)`].
4851cb0ef41Sopenharmony_ciOtherwise, if `path` is specified, it behaves the same as
4861cb0ef41Sopenharmony_ci[`server.listen(path[, backlog][, callback])`][`server.listen(path)`].
4871cb0ef41Sopenharmony_ciIf none of them is specified, an error will be thrown.
4881cb0ef41Sopenharmony_ci
4891cb0ef41Sopenharmony_ciIf `exclusive` is `false` (default), then cluster workers will use the same
4901cb0ef41Sopenharmony_ciunderlying handle, allowing connection handling duties to be shared. When
4911cb0ef41Sopenharmony_ci`exclusive` is `true`, the handle is not shared, and attempted port sharing
4921cb0ef41Sopenharmony_ciresults in an error. An example which listens on an exclusive port is
4931cb0ef41Sopenharmony_cishown below.
4941cb0ef41Sopenharmony_ci
4951cb0ef41Sopenharmony_ci```js
4961cb0ef41Sopenharmony_ciserver.listen({
4971cb0ef41Sopenharmony_ci  host: 'localhost',
4981cb0ef41Sopenharmony_ci  port: 80,
4991cb0ef41Sopenharmony_ci  exclusive: true,
5001cb0ef41Sopenharmony_ci});
5011cb0ef41Sopenharmony_ci```
5021cb0ef41Sopenharmony_ci
5031cb0ef41Sopenharmony_ciWhen `exclusive` is `true` and the underlying handle is shared, it is
5041cb0ef41Sopenharmony_cipossible that several workers query a handle with different backlogs.
5051cb0ef41Sopenharmony_ciIn this case, the first `backlog` passed to the master process will be used.
5061cb0ef41Sopenharmony_ci
5071cb0ef41Sopenharmony_ciStarting an IPC server as root may cause the server path to be inaccessible for
5081cb0ef41Sopenharmony_ciunprivileged users. Using `readableAll` and `writableAll` will make the server
5091cb0ef41Sopenharmony_ciaccessible for all users.
5101cb0ef41Sopenharmony_ci
5111cb0ef41Sopenharmony_ciIf the `signal` option is enabled, calling `.abort()` on the corresponding
5121cb0ef41Sopenharmony_ci`AbortController` is similar to calling `.close()` on the server:
5131cb0ef41Sopenharmony_ci
5141cb0ef41Sopenharmony_ci```js
5151cb0ef41Sopenharmony_ciconst controller = new AbortController();
5161cb0ef41Sopenharmony_ciserver.listen({
5171cb0ef41Sopenharmony_ci  host: 'localhost',
5181cb0ef41Sopenharmony_ci  port: 80,
5191cb0ef41Sopenharmony_ci  signal: controller.signal,
5201cb0ef41Sopenharmony_ci});
5211cb0ef41Sopenharmony_ci// Later, when you want to close the server.
5221cb0ef41Sopenharmony_cicontroller.abort();
5231cb0ef41Sopenharmony_ci```
5241cb0ef41Sopenharmony_ci
5251cb0ef41Sopenharmony_ci#### `server.listen(path[, backlog][, callback])`
5261cb0ef41Sopenharmony_ci
5271cb0ef41Sopenharmony_ci<!-- YAML
5281cb0ef41Sopenharmony_ciadded: v0.1.90
5291cb0ef41Sopenharmony_ci-->
5301cb0ef41Sopenharmony_ci
5311cb0ef41Sopenharmony_ci* `path` {string} Path the server should listen to. See
5321cb0ef41Sopenharmony_ci  [Identifying paths for IPC connections][].
5331cb0ef41Sopenharmony_ci* `backlog` {number} Common parameter of [`server.listen()`][] functions.
5341cb0ef41Sopenharmony_ci* `callback` {Function}.
5351cb0ef41Sopenharmony_ci* Returns: {net.Server}
5361cb0ef41Sopenharmony_ci
5371cb0ef41Sopenharmony_ciStart an [IPC][] server listening for connections on the given `path`.
5381cb0ef41Sopenharmony_ci
5391cb0ef41Sopenharmony_ci#### `server.listen([port[, host[, backlog]]][, callback])`
5401cb0ef41Sopenharmony_ci
5411cb0ef41Sopenharmony_ci<!-- YAML
5421cb0ef41Sopenharmony_ciadded: v0.1.90
5431cb0ef41Sopenharmony_ci-->
5441cb0ef41Sopenharmony_ci
5451cb0ef41Sopenharmony_ci* `port` {number}
5461cb0ef41Sopenharmony_ci* `host` {string}
5471cb0ef41Sopenharmony_ci* `backlog` {number} Common parameter of [`server.listen()`][] functions.
5481cb0ef41Sopenharmony_ci* `callback` {Function}.
5491cb0ef41Sopenharmony_ci* Returns: {net.Server}
5501cb0ef41Sopenharmony_ci
5511cb0ef41Sopenharmony_ciStart a TCP server listening for connections on the given `port` and `host`.
5521cb0ef41Sopenharmony_ci
5531cb0ef41Sopenharmony_ciIf `port` is omitted or is 0, the operating system will assign an arbitrary
5541cb0ef41Sopenharmony_ciunused port, which can be retrieved by using `server.address().port`
5551cb0ef41Sopenharmony_ciafter the [`'listening'`][] event has been emitted.
5561cb0ef41Sopenharmony_ci
5571cb0ef41Sopenharmony_ciIf `host` is omitted, the server will accept connections on the
5581cb0ef41Sopenharmony_ci[unspecified IPv6 address][] (`::`) when IPv6 is available, or the
5591cb0ef41Sopenharmony_ci[unspecified IPv4 address][] (`0.0.0.0`) otherwise.
5601cb0ef41Sopenharmony_ci
5611cb0ef41Sopenharmony_ciIn most operating systems, listening to the [unspecified IPv6 address][] (`::`)
5621cb0ef41Sopenharmony_cimay cause the `net.Server` to also listen on the [unspecified IPv4 address][]
5631cb0ef41Sopenharmony_ci(`0.0.0.0`).
5641cb0ef41Sopenharmony_ci
5651cb0ef41Sopenharmony_ci### `server.listening`
5661cb0ef41Sopenharmony_ci
5671cb0ef41Sopenharmony_ci<!-- YAML
5681cb0ef41Sopenharmony_ciadded: v5.7.0
5691cb0ef41Sopenharmony_ci-->
5701cb0ef41Sopenharmony_ci
5711cb0ef41Sopenharmony_ci* {boolean} Indicates whether or not the server is listening for connections.
5721cb0ef41Sopenharmony_ci
5731cb0ef41Sopenharmony_ci### `server.maxConnections`
5741cb0ef41Sopenharmony_ci
5751cb0ef41Sopenharmony_ci<!-- YAML
5761cb0ef41Sopenharmony_ciadded: v0.2.0
5771cb0ef41Sopenharmony_ci-->
5781cb0ef41Sopenharmony_ci
5791cb0ef41Sopenharmony_ci* {integer}
5801cb0ef41Sopenharmony_ci
5811cb0ef41Sopenharmony_ciSet this property to reject connections when the server's connection count gets
5821cb0ef41Sopenharmony_cihigh.
5831cb0ef41Sopenharmony_ci
5841cb0ef41Sopenharmony_ciIt is not recommended to use this option once a socket has been sent to a child
5851cb0ef41Sopenharmony_ciwith [`child_process.fork()`][].
5861cb0ef41Sopenharmony_ci
5871cb0ef41Sopenharmony_ci### `server.ref()`
5881cb0ef41Sopenharmony_ci
5891cb0ef41Sopenharmony_ci<!-- YAML
5901cb0ef41Sopenharmony_ciadded: v0.9.1
5911cb0ef41Sopenharmony_ci-->
5921cb0ef41Sopenharmony_ci
5931cb0ef41Sopenharmony_ci* Returns: {net.Server}
5941cb0ef41Sopenharmony_ci
5951cb0ef41Sopenharmony_ciOpposite of `unref()`, calling `ref()` on a previously `unref`ed server will
5961cb0ef41Sopenharmony_ci_not_ let the program exit if it's the only server left (the default behavior).
5971cb0ef41Sopenharmony_ciIf the server is `ref`ed calling `ref()` again will have no effect.
5981cb0ef41Sopenharmony_ci
5991cb0ef41Sopenharmony_ci### `server.unref()`
6001cb0ef41Sopenharmony_ci
6011cb0ef41Sopenharmony_ci<!-- YAML
6021cb0ef41Sopenharmony_ciadded: v0.9.1
6031cb0ef41Sopenharmony_ci-->
6041cb0ef41Sopenharmony_ci
6051cb0ef41Sopenharmony_ci* Returns: {net.Server}
6061cb0ef41Sopenharmony_ci
6071cb0ef41Sopenharmony_ciCalling `unref()` on a server will allow the program to exit if this is the only
6081cb0ef41Sopenharmony_ciactive server in the event system. If the server is already `unref`ed calling
6091cb0ef41Sopenharmony_ci`unref()` again will have no effect.
6101cb0ef41Sopenharmony_ci
6111cb0ef41Sopenharmony_ci## Class: `net.Socket`
6121cb0ef41Sopenharmony_ci
6131cb0ef41Sopenharmony_ci<!-- YAML
6141cb0ef41Sopenharmony_ciadded: v0.3.4
6151cb0ef41Sopenharmony_ci-->
6161cb0ef41Sopenharmony_ci
6171cb0ef41Sopenharmony_ci* Extends: {stream.Duplex}
6181cb0ef41Sopenharmony_ci
6191cb0ef41Sopenharmony_ciThis class is an abstraction of a TCP socket or a streaming [IPC][] endpoint
6201cb0ef41Sopenharmony_ci(uses named pipes on Windows, and Unix domain sockets otherwise). It is also
6211cb0ef41Sopenharmony_cian [`EventEmitter`][].
6221cb0ef41Sopenharmony_ci
6231cb0ef41Sopenharmony_ciA `net.Socket` can be created by the user and used directly to interact with
6241cb0ef41Sopenharmony_cia server. For example, it is returned by [`net.createConnection()`][],
6251cb0ef41Sopenharmony_ciso the user can use it to talk to the server.
6261cb0ef41Sopenharmony_ci
6271cb0ef41Sopenharmony_ciIt can also be created by Node.js and passed to the user when a connection
6281cb0ef41Sopenharmony_ciis received. For example, it is passed to the listeners of a
6291cb0ef41Sopenharmony_ci[`'connection'`][] event emitted on a [`net.Server`][], so the user can use
6301cb0ef41Sopenharmony_ciit to interact with the client.
6311cb0ef41Sopenharmony_ci
6321cb0ef41Sopenharmony_ci### `new net.Socket([options])`
6331cb0ef41Sopenharmony_ci
6341cb0ef41Sopenharmony_ci<!-- YAML
6351cb0ef41Sopenharmony_ciadded: v0.3.4
6361cb0ef41Sopenharmony_cichanges:
6371cb0ef41Sopenharmony_ci  - version: v15.14.0
6381cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/37735
6391cb0ef41Sopenharmony_ci    description: AbortSignal support was added.
6401cb0ef41Sopenharmony_ci-->
6411cb0ef41Sopenharmony_ci
6421cb0ef41Sopenharmony_ci* `options` {Object} Available options are:
6431cb0ef41Sopenharmony_ci  * `fd` {number} If specified, wrap around an existing socket with
6441cb0ef41Sopenharmony_ci    the given file descriptor, otherwise a new socket will be created.
6451cb0ef41Sopenharmony_ci  * `allowHalfOpen` {boolean} If set to `false`, then the socket will
6461cb0ef41Sopenharmony_ci    automatically end the writable side when the readable side ends. See
6471cb0ef41Sopenharmony_ci    [`net.createServer()`][] and the [`'end'`][] event for details. **Default:**
6481cb0ef41Sopenharmony_ci    `false`.
6491cb0ef41Sopenharmony_ci  * `readable` {boolean} Allow reads on the socket when an `fd` is passed,
6501cb0ef41Sopenharmony_ci    otherwise ignored. **Default:** `false`.
6511cb0ef41Sopenharmony_ci  * `writable` {boolean} Allow writes on the socket when an `fd` is passed,
6521cb0ef41Sopenharmony_ci    otherwise ignored. **Default:** `false`.
6531cb0ef41Sopenharmony_ci  * `signal` {AbortSignal} An Abort signal that may be used to destroy the
6541cb0ef41Sopenharmony_ci    socket.
6551cb0ef41Sopenharmony_ci* Returns: {net.Socket}
6561cb0ef41Sopenharmony_ci
6571cb0ef41Sopenharmony_ciCreates a new socket object.
6581cb0ef41Sopenharmony_ci
6591cb0ef41Sopenharmony_ciThe newly created socket can be either a TCP socket or a streaming [IPC][]
6601cb0ef41Sopenharmony_ciendpoint, depending on what it [`connect()`][`socket.connect()`] to.
6611cb0ef41Sopenharmony_ci
6621cb0ef41Sopenharmony_ci### Event: `'close'`
6631cb0ef41Sopenharmony_ci
6641cb0ef41Sopenharmony_ci<!-- YAML
6651cb0ef41Sopenharmony_ciadded: v0.1.90
6661cb0ef41Sopenharmony_ci-->
6671cb0ef41Sopenharmony_ci
6681cb0ef41Sopenharmony_ci* `hadError` {boolean} `true` if the socket had a transmission error.
6691cb0ef41Sopenharmony_ci
6701cb0ef41Sopenharmony_ciEmitted once the socket is fully closed. The argument `hadError` is a boolean
6711cb0ef41Sopenharmony_ciwhich says if the socket was closed due to a transmission error.
6721cb0ef41Sopenharmony_ci
6731cb0ef41Sopenharmony_ci### Event: `'connect'`
6741cb0ef41Sopenharmony_ci
6751cb0ef41Sopenharmony_ci<!-- YAML
6761cb0ef41Sopenharmony_ciadded: v0.1.90
6771cb0ef41Sopenharmony_ci-->
6781cb0ef41Sopenharmony_ci
6791cb0ef41Sopenharmony_ciEmitted when a socket connection is successfully established.
6801cb0ef41Sopenharmony_ciSee [`net.createConnection()`][].
6811cb0ef41Sopenharmony_ci
6821cb0ef41Sopenharmony_ci### Event: `'data'`
6831cb0ef41Sopenharmony_ci
6841cb0ef41Sopenharmony_ci<!-- YAML
6851cb0ef41Sopenharmony_ciadded: v0.1.90
6861cb0ef41Sopenharmony_ci-->
6871cb0ef41Sopenharmony_ci
6881cb0ef41Sopenharmony_ci* {Buffer|string}
6891cb0ef41Sopenharmony_ci
6901cb0ef41Sopenharmony_ciEmitted when data is received. The argument `data` will be a `Buffer` or
6911cb0ef41Sopenharmony_ci`String`. Encoding of data is set by [`socket.setEncoding()`][].
6921cb0ef41Sopenharmony_ci
6931cb0ef41Sopenharmony_ciThe data will be lost if there is no listener when a `Socket`
6941cb0ef41Sopenharmony_ciemits a `'data'` event.
6951cb0ef41Sopenharmony_ci
6961cb0ef41Sopenharmony_ci### Event: `'drain'`
6971cb0ef41Sopenharmony_ci
6981cb0ef41Sopenharmony_ci<!-- YAML
6991cb0ef41Sopenharmony_ciadded: v0.1.90
7001cb0ef41Sopenharmony_ci-->
7011cb0ef41Sopenharmony_ci
7021cb0ef41Sopenharmony_ciEmitted when the write buffer becomes empty. Can be used to throttle uploads.
7031cb0ef41Sopenharmony_ci
7041cb0ef41Sopenharmony_ciSee also: the return values of `socket.write()`.
7051cb0ef41Sopenharmony_ci
7061cb0ef41Sopenharmony_ci### Event: `'end'`
7071cb0ef41Sopenharmony_ci
7081cb0ef41Sopenharmony_ci<!-- YAML
7091cb0ef41Sopenharmony_ciadded: v0.1.90
7101cb0ef41Sopenharmony_ci-->
7111cb0ef41Sopenharmony_ci
7121cb0ef41Sopenharmony_ciEmitted when the other end of the socket signals the end of transmission, thus
7131cb0ef41Sopenharmony_ciending the readable side of the socket.
7141cb0ef41Sopenharmony_ci
7151cb0ef41Sopenharmony_ciBy default (`allowHalfOpen` is `false`) the socket will send an end of
7161cb0ef41Sopenharmony_citransmission packet back and destroy its file descriptor once it has written out
7171cb0ef41Sopenharmony_ciits pending write queue. However, if `allowHalfOpen` is set to `true`, the
7181cb0ef41Sopenharmony_cisocket will not automatically [`end()`][`socket.end()`] its writable side,
7191cb0ef41Sopenharmony_ciallowing the user to write arbitrary amounts of data. The user must call
7201cb0ef41Sopenharmony_ci[`end()`][`socket.end()`] explicitly to close the connection (i.e. sending a
7211cb0ef41Sopenharmony_ciFIN packet back).
7221cb0ef41Sopenharmony_ci
7231cb0ef41Sopenharmony_ci### Event: `'error'`
7241cb0ef41Sopenharmony_ci
7251cb0ef41Sopenharmony_ci<!-- YAML
7261cb0ef41Sopenharmony_ciadded: v0.1.90
7271cb0ef41Sopenharmony_ci-->
7281cb0ef41Sopenharmony_ci
7291cb0ef41Sopenharmony_ci* {Error}
7301cb0ef41Sopenharmony_ci
7311cb0ef41Sopenharmony_ciEmitted when an error occurs. The `'close'` event will be called directly
7321cb0ef41Sopenharmony_cifollowing this event.
7331cb0ef41Sopenharmony_ci
7341cb0ef41Sopenharmony_ci### Event: `'lookup'`
7351cb0ef41Sopenharmony_ci
7361cb0ef41Sopenharmony_ci<!-- YAML
7371cb0ef41Sopenharmony_ciadded: v0.11.3
7381cb0ef41Sopenharmony_cichanges:
7391cb0ef41Sopenharmony_ci  - version: v5.10.0
7401cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/5598
7411cb0ef41Sopenharmony_ci    description: The `host` parameter is supported now.
7421cb0ef41Sopenharmony_ci-->
7431cb0ef41Sopenharmony_ci
7441cb0ef41Sopenharmony_ciEmitted after resolving the host name but before connecting.
7451cb0ef41Sopenharmony_ciNot applicable to Unix sockets.
7461cb0ef41Sopenharmony_ci
7471cb0ef41Sopenharmony_ci* `err` {Error|null} The error object. See [`dns.lookup()`][].
7481cb0ef41Sopenharmony_ci* `address` {string} The IP address.
7491cb0ef41Sopenharmony_ci* `family` {number|null} The address type. See [`dns.lookup()`][].
7501cb0ef41Sopenharmony_ci* `host` {string} The host name.
7511cb0ef41Sopenharmony_ci
7521cb0ef41Sopenharmony_ci### Event: `'ready'`
7531cb0ef41Sopenharmony_ci
7541cb0ef41Sopenharmony_ci<!-- YAML
7551cb0ef41Sopenharmony_ciadded: v9.11.0
7561cb0ef41Sopenharmony_ci-->
7571cb0ef41Sopenharmony_ci
7581cb0ef41Sopenharmony_ciEmitted when a socket is ready to be used.
7591cb0ef41Sopenharmony_ci
7601cb0ef41Sopenharmony_ciTriggered immediately after `'connect'`.
7611cb0ef41Sopenharmony_ci
7621cb0ef41Sopenharmony_ci### Event: `'timeout'`
7631cb0ef41Sopenharmony_ci
7641cb0ef41Sopenharmony_ci<!-- YAML
7651cb0ef41Sopenharmony_ciadded: v0.1.90
7661cb0ef41Sopenharmony_ci-->
7671cb0ef41Sopenharmony_ci
7681cb0ef41Sopenharmony_ciEmitted if the socket times out from inactivity. This is only to notify that
7691cb0ef41Sopenharmony_cithe socket has been idle. The user must manually close the connection.
7701cb0ef41Sopenharmony_ci
7711cb0ef41Sopenharmony_ciSee also: [`socket.setTimeout()`][].
7721cb0ef41Sopenharmony_ci
7731cb0ef41Sopenharmony_ci### `socket.address()`
7741cb0ef41Sopenharmony_ci
7751cb0ef41Sopenharmony_ci<!-- YAML
7761cb0ef41Sopenharmony_ciadded: v0.1.90
7771cb0ef41Sopenharmony_cichanges:
7781cb0ef41Sopenharmony_ci  - version: v18.4.0
7791cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/43054
7801cb0ef41Sopenharmony_ci    description: The `family` property now returns a string instead of a number.
7811cb0ef41Sopenharmony_ci  - version: v18.0.0
7821cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/41431
7831cb0ef41Sopenharmony_ci    description: The `family` property now returns a number instead of a string.
7841cb0ef41Sopenharmony_ci-->
7851cb0ef41Sopenharmony_ci
7861cb0ef41Sopenharmony_ci* Returns: {Object}
7871cb0ef41Sopenharmony_ci
7881cb0ef41Sopenharmony_ciReturns the bound `address`, the address `family` name and `port` of the
7891cb0ef41Sopenharmony_cisocket as reported by the operating system:
7901cb0ef41Sopenharmony_ci`{ port: 12346, family: 'IPv4', address: '127.0.0.1' }`
7911cb0ef41Sopenharmony_ci
7921cb0ef41Sopenharmony_ci### `socket.autoSelectFamilyAttemptedAddresses`
7931cb0ef41Sopenharmony_ci
7941cb0ef41Sopenharmony_ci<!-- YAML
7951cb0ef41Sopenharmony_ciadded: v18.18.0
7961cb0ef41Sopenharmony_ci-->
7971cb0ef41Sopenharmony_ci
7981cb0ef41Sopenharmony_ci* {string\[]}
7991cb0ef41Sopenharmony_ci
8001cb0ef41Sopenharmony_ciThis property is only present if the family autoselection algorithm is enabled in
8011cb0ef41Sopenharmony_ci[`socket.connect(options)`][] and it is an array of the addresses that have been attempted.
8021cb0ef41Sopenharmony_ci
8031cb0ef41Sopenharmony_ciEach address is a string in the form of `$IP:$PORT`. If the connection was successful,
8041cb0ef41Sopenharmony_cithen the last address is the one that the socket is currently connected to.
8051cb0ef41Sopenharmony_ci
8061cb0ef41Sopenharmony_ci### `socket.bufferSize`
8071cb0ef41Sopenharmony_ci
8081cb0ef41Sopenharmony_ci<!-- YAML
8091cb0ef41Sopenharmony_ciadded: v0.3.8
8101cb0ef41Sopenharmony_cideprecated:
8111cb0ef41Sopenharmony_ci  - v14.6.0
8121cb0ef41Sopenharmony_ci-->
8131cb0ef41Sopenharmony_ci
8141cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use [`writable.writableLength`][] instead.
8151cb0ef41Sopenharmony_ci
8161cb0ef41Sopenharmony_ci* {integer}
8171cb0ef41Sopenharmony_ci
8181cb0ef41Sopenharmony_ciThis property shows the number of characters buffered for writing. The buffer
8191cb0ef41Sopenharmony_cimay contain strings whose length after encoding is not yet known. So this number
8201cb0ef41Sopenharmony_ciis only an approximation of the number of bytes in the buffer.
8211cb0ef41Sopenharmony_ci
8221cb0ef41Sopenharmony_ci`net.Socket` has the property that `socket.write()` always works. This is to
8231cb0ef41Sopenharmony_cihelp users get up and running quickly. The computer cannot always keep up
8241cb0ef41Sopenharmony_ciwith the amount of data that is written to a socket. The network connection
8251cb0ef41Sopenharmony_cisimply might be too slow. Node.js will internally queue up the data written to a
8261cb0ef41Sopenharmony_cisocket and send it out over the wire when it is possible.
8271cb0ef41Sopenharmony_ci
8281cb0ef41Sopenharmony_ciThe consequence of this internal buffering is that memory may grow.
8291cb0ef41Sopenharmony_ciUsers who experience large or growing `bufferSize` should attempt to
8301cb0ef41Sopenharmony_ci"throttle" the data flows in their program with
8311cb0ef41Sopenharmony_ci[`socket.pause()`][] and [`socket.resume()`][].
8321cb0ef41Sopenharmony_ci
8331cb0ef41Sopenharmony_ci### `socket.bytesRead`
8341cb0ef41Sopenharmony_ci
8351cb0ef41Sopenharmony_ci<!-- YAML
8361cb0ef41Sopenharmony_ciadded: v0.5.3
8371cb0ef41Sopenharmony_ci-->
8381cb0ef41Sopenharmony_ci
8391cb0ef41Sopenharmony_ci* {integer}
8401cb0ef41Sopenharmony_ci
8411cb0ef41Sopenharmony_ciThe amount of received bytes.
8421cb0ef41Sopenharmony_ci
8431cb0ef41Sopenharmony_ci### `socket.bytesWritten`
8441cb0ef41Sopenharmony_ci
8451cb0ef41Sopenharmony_ci<!-- YAML
8461cb0ef41Sopenharmony_ciadded: v0.5.3
8471cb0ef41Sopenharmony_ci-->
8481cb0ef41Sopenharmony_ci
8491cb0ef41Sopenharmony_ci* {integer}
8501cb0ef41Sopenharmony_ci
8511cb0ef41Sopenharmony_ciThe amount of bytes sent.
8521cb0ef41Sopenharmony_ci
8531cb0ef41Sopenharmony_ci### `socket.connect()`
8541cb0ef41Sopenharmony_ci
8551cb0ef41Sopenharmony_ciInitiate a connection on a given socket.
8561cb0ef41Sopenharmony_ci
8571cb0ef41Sopenharmony_ciPossible signatures:
8581cb0ef41Sopenharmony_ci
8591cb0ef41Sopenharmony_ci* [`socket.connect(options[, connectListener])`][`socket.connect(options)`]
8601cb0ef41Sopenharmony_ci* [`socket.connect(path[, connectListener])`][`socket.connect(path)`]
8611cb0ef41Sopenharmony_ci  for [IPC][] connections.
8621cb0ef41Sopenharmony_ci* [`socket.connect(port[, host][, connectListener])`][`socket.connect(port)`]
8631cb0ef41Sopenharmony_ci  for TCP connections.
8641cb0ef41Sopenharmony_ci* Returns: {net.Socket} The socket itself.
8651cb0ef41Sopenharmony_ci
8661cb0ef41Sopenharmony_ciThis function is asynchronous. When the connection is established, the
8671cb0ef41Sopenharmony_ci[`'connect'`][] event will be emitted. If there is a problem connecting,
8681cb0ef41Sopenharmony_ciinstead of a [`'connect'`][] event, an [`'error'`][] event will be emitted with
8691cb0ef41Sopenharmony_cithe error passed to the [`'error'`][] listener.
8701cb0ef41Sopenharmony_ciThe last parameter `connectListener`, if supplied, will be added as a listener
8711cb0ef41Sopenharmony_cifor the [`'connect'`][] event **once**.
8721cb0ef41Sopenharmony_ci
8731cb0ef41Sopenharmony_ciThis function should only be used for reconnecting a socket after
8741cb0ef41Sopenharmony_ci`'close'` has been emitted or otherwise it may lead to undefined
8751cb0ef41Sopenharmony_cibehavior.
8761cb0ef41Sopenharmony_ci
8771cb0ef41Sopenharmony_ci#### `socket.connect(options[, connectListener])`
8781cb0ef41Sopenharmony_ci
8791cb0ef41Sopenharmony_ci<!-- YAML
8801cb0ef41Sopenharmony_ciadded: v0.1.90
8811cb0ef41Sopenharmony_cichanges:
8821cb0ef41Sopenharmony_ci  - version: v18.18.0
8831cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/45777
8841cb0ef41Sopenharmony_ci    description: The default value for autoSelectFamily option can be changed
8851cb0ef41Sopenharmony_ci                 at runtime using `setDefaultAutoSelectFamily` or via the
8861cb0ef41Sopenharmony_ci                 command line option `--enable-network-family-autoselection`.
8871cb0ef41Sopenharmony_ci  - version: v18.13.0
8881cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/44731
8891cb0ef41Sopenharmony_ci    description: Added the `autoSelectFamily` option.
8901cb0ef41Sopenharmony_ci  - version: v17.7.0
8911cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/41310
8921cb0ef41Sopenharmony_ci    description: The `noDelay`, `keepAlive`, and `keepAliveInitialDelay`
8931cb0ef41Sopenharmony_ci                 options are supported now.
8941cb0ef41Sopenharmony_ci  - version: v12.10.0
8951cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/25436
8961cb0ef41Sopenharmony_ci    description: Added `onread` option.
8971cb0ef41Sopenharmony_ci  - version: v6.0.0
8981cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/6021
8991cb0ef41Sopenharmony_ci    description: The `hints` option defaults to `0` in all cases now.
9001cb0ef41Sopenharmony_ci                 Previously, in the absence of the `family` option it would
9011cb0ef41Sopenharmony_ci                 default to `dns.ADDRCONFIG | dns.V4MAPPED`.
9021cb0ef41Sopenharmony_ci  - version: v5.11.0
9031cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/6000
9041cb0ef41Sopenharmony_ci    description: The `hints` option is supported now.
9051cb0ef41Sopenharmony_ci-->
9061cb0ef41Sopenharmony_ci
9071cb0ef41Sopenharmony_ci* `options` {Object}
9081cb0ef41Sopenharmony_ci* `connectListener` {Function} Common parameter of [`socket.connect()`][]
9091cb0ef41Sopenharmony_ci  methods. Will be added as a listener for the [`'connect'`][] event once.
9101cb0ef41Sopenharmony_ci* Returns: {net.Socket} The socket itself.
9111cb0ef41Sopenharmony_ci
9121cb0ef41Sopenharmony_ciInitiate a connection on a given socket. Normally this method is not needed,
9131cb0ef41Sopenharmony_cithe socket should be created and opened with [`net.createConnection()`][]. Use
9141cb0ef41Sopenharmony_cithis only when implementing a custom Socket.
9151cb0ef41Sopenharmony_ci
9161cb0ef41Sopenharmony_ciFor TCP connections, available `options` are:
9171cb0ef41Sopenharmony_ci
9181cb0ef41Sopenharmony_ci* `port` {number} Required. Port the socket should connect to.
9191cb0ef41Sopenharmony_ci* `host` {string} Host the socket should connect to. **Default:** `'localhost'`.
9201cb0ef41Sopenharmony_ci* `localAddress` {string} Local address the socket should connect from.
9211cb0ef41Sopenharmony_ci* `localPort` {number} Local port the socket should connect from.
9221cb0ef41Sopenharmony_ci* `family` {number}: Version of IP stack. Must be `4`, `6`, or `0`. The value
9231cb0ef41Sopenharmony_ci  `0` indicates that both IPv4 and IPv6 addresses are allowed. **Default:** `0`.
9241cb0ef41Sopenharmony_ci* `hints` {number} Optional [`dns.lookup()` hints][].
9251cb0ef41Sopenharmony_ci* `lookup` {Function} Custom lookup function. **Default:** [`dns.lookup()`][].
9261cb0ef41Sopenharmony_ci* `noDelay` {boolean} If set to `true`, it disables the use of Nagle's algorithm immediately
9271cb0ef41Sopenharmony_ci  after the socket is established. **Default:** `false`.
9281cb0ef41Sopenharmony_ci* `keepAlive` {boolean} If set to `true`, it enables keep-alive functionality on the socket
9291cb0ef41Sopenharmony_ci  immediately after the connection is established, similarly on what is done in
9301cb0ef41Sopenharmony_ci  [`socket.setKeepAlive([enable][, initialDelay])`][`socket.setKeepAlive(enable, initialDelay)`].
9311cb0ef41Sopenharmony_ci  **Default:** `false`.
9321cb0ef41Sopenharmony_ci* `keepAliveInitialDelay` {number} If set to a positive number, it sets the initial delay before
9331cb0ef41Sopenharmony_ci  the first keepalive probe is sent on an idle socket.**Default:** `0`.
9341cb0ef41Sopenharmony_ci* `autoSelectFamily` {boolean}: If set to `true`, it enables a family autodetection algorithm
9351cb0ef41Sopenharmony_ci  that loosely implements section 5 of [RFC 8305][].
9361cb0ef41Sopenharmony_ci  The `all` option passed to lookup is set to `true` and the sockets attempts to connect to all
9371cb0ef41Sopenharmony_ci  obtained IPv6 and IPv4 addresses, in sequence, until a connection is established.
9381cb0ef41Sopenharmony_ci  The first returned AAAA address is tried first, then the first returned A address,
9391cb0ef41Sopenharmony_ci  then the second returned AAAA address and so on.
9401cb0ef41Sopenharmony_ci  Each connection attempt is given the amount of time specified by the `autoSelectFamilyAttemptTimeout`
9411cb0ef41Sopenharmony_ci  option before timing out and trying the next address.
9421cb0ef41Sopenharmony_ci  Ignored if the `family` option is not `0` or if `localAddress` is set.
9431cb0ef41Sopenharmony_ci  Connection errors are not emitted if at least one connection succeeds.
9441cb0ef41Sopenharmony_ci  **Default:** initially `false`, but it can be changed at runtime using [`net.setDefaultAutoSelectFamily(value)`][]
9451cb0ef41Sopenharmony_ci  or via the command line option `--enable-network-family-autoselection`.
9461cb0ef41Sopenharmony_ci* `autoSelectFamilyAttemptTimeout` {number}: The amount of time in milliseconds to wait
9471cb0ef41Sopenharmony_ci  for a connection attempt to finish before trying the next address when using the `autoSelectFamily` option.
9481cb0ef41Sopenharmony_ci  If set to a positive integer less than `10`, then the value `10` will be used instead.
9491cb0ef41Sopenharmony_ci  **Default:** initially `250`, but it can be changed at runtime using [`net.setDefaultAutoSelectFamilyAttemptTimeout(value)`][]
9501cb0ef41Sopenharmony_ci
9511cb0ef41Sopenharmony_ciFor [IPC][] connections, available `options` are:
9521cb0ef41Sopenharmony_ci
9531cb0ef41Sopenharmony_ci* `path` {string} Required. Path the client should connect to.
9541cb0ef41Sopenharmony_ci  See [Identifying paths for IPC connections][]. If provided, the TCP-specific
9551cb0ef41Sopenharmony_ci  options above are ignored.
9561cb0ef41Sopenharmony_ci
9571cb0ef41Sopenharmony_ciFor both types, available `options` include:
9581cb0ef41Sopenharmony_ci
9591cb0ef41Sopenharmony_ci* `onread` {Object} If specified, incoming data is stored in a single `buffer`
9601cb0ef41Sopenharmony_ci  and passed to the supplied `callback` when data arrives on the socket.
9611cb0ef41Sopenharmony_ci  This will cause the streaming functionality to not provide any data.
9621cb0ef41Sopenharmony_ci  The socket will emit events like `'error'`, `'end'`, and `'close'`
9631cb0ef41Sopenharmony_ci  as usual. Methods like `pause()` and `resume()` will also behave as
9641cb0ef41Sopenharmony_ci  expected.
9651cb0ef41Sopenharmony_ci  * `buffer` {Buffer|Uint8Array|Function} Either a reusable chunk of memory to
9661cb0ef41Sopenharmony_ci    use for storing incoming data or a function that returns such.
9671cb0ef41Sopenharmony_ci  * `callback` {Function} This function is called for every chunk of incoming
9681cb0ef41Sopenharmony_ci    data. Two arguments are passed to it: the number of bytes written to
9691cb0ef41Sopenharmony_ci    `buffer` and a reference to `buffer`. Return `false` from this function to
9701cb0ef41Sopenharmony_ci    implicitly `pause()` the socket. This function will be executed in the
9711cb0ef41Sopenharmony_ci    global context.
9721cb0ef41Sopenharmony_ci
9731cb0ef41Sopenharmony_ciFollowing is an example of a client using the `onread` option:
9741cb0ef41Sopenharmony_ci
9751cb0ef41Sopenharmony_ci```js
9761cb0ef41Sopenharmony_ciconst net = require('node:net');
9771cb0ef41Sopenharmony_cinet.connect({
9781cb0ef41Sopenharmony_ci  port: 80,
9791cb0ef41Sopenharmony_ci  onread: {
9801cb0ef41Sopenharmony_ci    // Reuses a 4KiB Buffer for every read from the socket.
9811cb0ef41Sopenharmony_ci    buffer: Buffer.alloc(4 * 1024),
9821cb0ef41Sopenharmony_ci    callback: function(nread, buf) {
9831cb0ef41Sopenharmony_ci      // Received data is available in `buf` from 0 to `nread`.
9841cb0ef41Sopenharmony_ci      console.log(buf.toString('utf8', 0, nread));
9851cb0ef41Sopenharmony_ci    },
9861cb0ef41Sopenharmony_ci  },
9871cb0ef41Sopenharmony_ci});
9881cb0ef41Sopenharmony_ci```
9891cb0ef41Sopenharmony_ci
9901cb0ef41Sopenharmony_ci#### `socket.connect(path[, connectListener])`
9911cb0ef41Sopenharmony_ci
9921cb0ef41Sopenharmony_ci* `path` {string} Path the client should connect to. See
9931cb0ef41Sopenharmony_ci  [Identifying paths for IPC connections][].
9941cb0ef41Sopenharmony_ci* `connectListener` {Function} Common parameter of [`socket.connect()`][]
9951cb0ef41Sopenharmony_ci  methods. Will be added as a listener for the [`'connect'`][] event once.
9961cb0ef41Sopenharmony_ci* Returns: {net.Socket} The socket itself.
9971cb0ef41Sopenharmony_ci
9981cb0ef41Sopenharmony_ciInitiate an [IPC][] connection on the given socket.
9991cb0ef41Sopenharmony_ci
10001cb0ef41Sopenharmony_ciAlias to
10011cb0ef41Sopenharmony_ci[`socket.connect(options[, connectListener])`][`socket.connect(options)`]
10021cb0ef41Sopenharmony_cicalled with `{ path: path }` as `options`.
10031cb0ef41Sopenharmony_ci
10041cb0ef41Sopenharmony_ci#### `socket.connect(port[, host][, connectListener])`
10051cb0ef41Sopenharmony_ci
10061cb0ef41Sopenharmony_ci<!-- YAML
10071cb0ef41Sopenharmony_ciadded: v0.1.90
10081cb0ef41Sopenharmony_ci-->
10091cb0ef41Sopenharmony_ci
10101cb0ef41Sopenharmony_ci* `port` {number} Port the client should connect to.
10111cb0ef41Sopenharmony_ci* `host` {string} Host the client should connect to.
10121cb0ef41Sopenharmony_ci* `connectListener` {Function} Common parameter of [`socket.connect()`][]
10131cb0ef41Sopenharmony_ci  methods. Will be added as a listener for the [`'connect'`][] event once.
10141cb0ef41Sopenharmony_ci* Returns: {net.Socket} The socket itself.
10151cb0ef41Sopenharmony_ci
10161cb0ef41Sopenharmony_ciInitiate a TCP connection on the given socket.
10171cb0ef41Sopenharmony_ci
10181cb0ef41Sopenharmony_ciAlias to
10191cb0ef41Sopenharmony_ci[`socket.connect(options[, connectListener])`][`socket.connect(options)`]
10201cb0ef41Sopenharmony_cicalled with `{port: port, host: host}` as `options`.
10211cb0ef41Sopenharmony_ci
10221cb0ef41Sopenharmony_ci### `socket.connecting`
10231cb0ef41Sopenharmony_ci
10241cb0ef41Sopenharmony_ci<!-- YAML
10251cb0ef41Sopenharmony_ciadded: v6.1.0
10261cb0ef41Sopenharmony_ci-->
10271cb0ef41Sopenharmony_ci
10281cb0ef41Sopenharmony_ci* {boolean}
10291cb0ef41Sopenharmony_ci
10301cb0ef41Sopenharmony_ciIf `true`,
10311cb0ef41Sopenharmony_ci[`socket.connect(options[, connectListener])`][`socket.connect(options)`] was
10321cb0ef41Sopenharmony_cicalled and has not yet finished. It will stay `true` until the socket becomes
10331cb0ef41Sopenharmony_ciconnected, then it is set to `false` and the `'connect'` event is emitted. Note
10341cb0ef41Sopenharmony_cithat the
10351cb0ef41Sopenharmony_ci[`socket.connect(options[, connectListener])`][`socket.connect(options)`]
10361cb0ef41Sopenharmony_cicallback is a listener for the `'connect'` event.
10371cb0ef41Sopenharmony_ci
10381cb0ef41Sopenharmony_ci### `socket.destroy([error])`
10391cb0ef41Sopenharmony_ci
10401cb0ef41Sopenharmony_ci<!-- YAML
10411cb0ef41Sopenharmony_ciadded: v0.1.90
10421cb0ef41Sopenharmony_ci-->
10431cb0ef41Sopenharmony_ci
10441cb0ef41Sopenharmony_ci* `error` {Object}
10451cb0ef41Sopenharmony_ci* Returns: {net.Socket}
10461cb0ef41Sopenharmony_ci
10471cb0ef41Sopenharmony_ciEnsures that no more I/O activity happens on this socket.
10481cb0ef41Sopenharmony_ciDestroys the stream and closes the connection.
10491cb0ef41Sopenharmony_ci
10501cb0ef41Sopenharmony_ciSee [`writable.destroy()`][] for further details.
10511cb0ef41Sopenharmony_ci
10521cb0ef41Sopenharmony_ci### `socket.destroyed`
10531cb0ef41Sopenharmony_ci
10541cb0ef41Sopenharmony_ci* {boolean} Indicates if the connection is destroyed or not. Once a
10551cb0ef41Sopenharmony_ci  connection is destroyed no further data can be transferred using it.
10561cb0ef41Sopenharmony_ci
10571cb0ef41Sopenharmony_ciSee [`writable.destroyed`][] for further details.
10581cb0ef41Sopenharmony_ci
10591cb0ef41Sopenharmony_ci### `socket.destroySoon()`
10601cb0ef41Sopenharmony_ci
10611cb0ef41Sopenharmony_ci<!-- YAML
10621cb0ef41Sopenharmony_ciadded: v0.3.4
10631cb0ef41Sopenharmony_ci-->
10641cb0ef41Sopenharmony_ci
10651cb0ef41Sopenharmony_ciDestroys the socket after all data is written. If the `'finish'` event was
10661cb0ef41Sopenharmony_cialready emitted the socket is destroyed immediately. If the socket is still
10671cb0ef41Sopenharmony_ciwritable it implicitly calls `socket.end()`.
10681cb0ef41Sopenharmony_ci
10691cb0ef41Sopenharmony_ci### `socket.end([data[, encoding]][, callback])`
10701cb0ef41Sopenharmony_ci
10711cb0ef41Sopenharmony_ci<!-- YAML
10721cb0ef41Sopenharmony_ciadded: v0.1.90
10731cb0ef41Sopenharmony_ci-->
10741cb0ef41Sopenharmony_ci
10751cb0ef41Sopenharmony_ci* `data` {string|Buffer|Uint8Array}
10761cb0ef41Sopenharmony_ci* `encoding` {string} Only used when data is `string`. **Default:** `'utf8'`.
10771cb0ef41Sopenharmony_ci* `callback` {Function} Optional callback for when the socket is finished.
10781cb0ef41Sopenharmony_ci* Returns: {net.Socket} The socket itself.
10791cb0ef41Sopenharmony_ci
10801cb0ef41Sopenharmony_ciHalf-closes the socket. i.e., it sends a FIN packet. It is possible the
10811cb0ef41Sopenharmony_ciserver will still send some data.
10821cb0ef41Sopenharmony_ci
10831cb0ef41Sopenharmony_ciSee [`writable.end()`][] for further details.
10841cb0ef41Sopenharmony_ci
10851cb0ef41Sopenharmony_ci### `socket.localAddress`
10861cb0ef41Sopenharmony_ci
10871cb0ef41Sopenharmony_ci<!-- YAML
10881cb0ef41Sopenharmony_ciadded: v0.9.6
10891cb0ef41Sopenharmony_ci-->
10901cb0ef41Sopenharmony_ci
10911cb0ef41Sopenharmony_ci* {string}
10921cb0ef41Sopenharmony_ci
10931cb0ef41Sopenharmony_ciThe string representation of the local IP address the remote client is
10941cb0ef41Sopenharmony_ciconnecting on. For example, in a server listening on `'0.0.0.0'`, if a client
10951cb0ef41Sopenharmony_ciconnects on `'192.168.1.1'`, the value of `socket.localAddress` would be
10961cb0ef41Sopenharmony_ci`'192.168.1.1'`.
10971cb0ef41Sopenharmony_ci
10981cb0ef41Sopenharmony_ci### `socket.localPort`
10991cb0ef41Sopenharmony_ci
11001cb0ef41Sopenharmony_ci<!-- YAML
11011cb0ef41Sopenharmony_ciadded: v0.9.6
11021cb0ef41Sopenharmony_ci-->
11031cb0ef41Sopenharmony_ci
11041cb0ef41Sopenharmony_ci* {integer}
11051cb0ef41Sopenharmony_ci
11061cb0ef41Sopenharmony_ciThe numeric representation of the local port. For example, `80` or `21`.
11071cb0ef41Sopenharmony_ci
11081cb0ef41Sopenharmony_ci### `socket.localFamily`
11091cb0ef41Sopenharmony_ci
11101cb0ef41Sopenharmony_ci<!-- YAML
11111cb0ef41Sopenharmony_ciadded: v18.8.0
11121cb0ef41Sopenharmony_ci-->
11131cb0ef41Sopenharmony_ci
11141cb0ef41Sopenharmony_ci* {string}
11151cb0ef41Sopenharmony_ci
11161cb0ef41Sopenharmony_ciThe string representation of the local IP family. `'IPv4'` or `'IPv6'`.
11171cb0ef41Sopenharmony_ci
11181cb0ef41Sopenharmony_ci### `socket.pause()`
11191cb0ef41Sopenharmony_ci
11201cb0ef41Sopenharmony_ci* Returns: {net.Socket} The socket itself.
11211cb0ef41Sopenharmony_ci
11221cb0ef41Sopenharmony_ciPauses the reading of data. That is, [`'data'`][] events will not be emitted.
11231cb0ef41Sopenharmony_ciUseful to throttle back an upload.
11241cb0ef41Sopenharmony_ci
11251cb0ef41Sopenharmony_ci### `socket.pending`
11261cb0ef41Sopenharmony_ci
11271cb0ef41Sopenharmony_ci<!-- YAML
11281cb0ef41Sopenharmony_ciadded:
11291cb0ef41Sopenharmony_ci - v11.2.0
11301cb0ef41Sopenharmony_ci - v10.16.0
11311cb0ef41Sopenharmony_ci-->
11321cb0ef41Sopenharmony_ci
11331cb0ef41Sopenharmony_ci* {boolean}
11341cb0ef41Sopenharmony_ci
11351cb0ef41Sopenharmony_ciThis is `true` if the socket is not connected yet, either because `.connect()`
11361cb0ef41Sopenharmony_cihas not yet been called or because it is still in the process of connecting
11371cb0ef41Sopenharmony_ci(see [`socket.connecting`][]).
11381cb0ef41Sopenharmony_ci
11391cb0ef41Sopenharmony_ci### `socket.ref()`
11401cb0ef41Sopenharmony_ci
11411cb0ef41Sopenharmony_ci<!-- YAML
11421cb0ef41Sopenharmony_ciadded: v0.9.1
11431cb0ef41Sopenharmony_ci-->
11441cb0ef41Sopenharmony_ci
11451cb0ef41Sopenharmony_ci* Returns: {net.Socket} The socket itself.
11461cb0ef41Sopenharmony_ci
11471cb0ef41Sopenharmony_ciOpposite of `unref()`, calling `ref()` on a previously `unref`ed socket will
11481cb0ef41Sopenharmony_ci_not_ let the program exit if it's the only socket left (the default behavior).
11491cb0ef41Sopenharmony_ciIf the socket is `ref`ed calling `ref` again will have no effect.
11501cb0ef41Sopenharmony_ci
11511cb0ef41Sopenharmony_ci### `socket.remoteAddress`
11521cb0ef41Sopenharmony_ci
11531cb0ef41Sopenharmony_ci<!-- YAML
11541cb0ef41Sopenharmony_ciadded: v0.5.10
11551cb0ef41Sopenharmony_ci-->
11561cb0ef41Sopenharmony_ci
11571cb0ef41Sopenharmony_ci* {string}
11581cb0ef41Sopenharmony_ci
11591cb0ef41Sopenharmony_ciThe string representation of the remote IP address. For example,
11601cb0ef41Sopenharmony_ci`'74.125.127.100'` or `'2001:4860:a005::68'`. Value may be `undefined` if
11611cb0ef41Sopenharmony_cithe socket is destroyed (for example, if the client disconnected).
11621cb0ef41Sopenharmony_ci
11631cb0ef41Sopenharmony_ci### `socket.remoteFamily`
11641cb0ef41Sopenharmony_ci
11651cb0ef41Sopenharmony_ci<!-- YAML
11661cb0ef41Sopenharmony_ciadded: v0.11.14
11671cb0ef41Sopenharmony_ci-->
11681cb0ef41Sopenharmony_ci
11691cb0ef41Sopenharmony_ci* {string}
11701cb0ef41Sopenharmony_ci
11711cb0ef41Sopenharmony_ciThe string representation of the remote IP family. `'IPv4'` or `'IPv6'`. Value may be `undefined` if
11721cb0ef41Sopenharmony_cithe socket is destroyed (for example, if the client disconnected).
11731cb0ef41Sopenharmony_ci
11741cb0ef41Sopenharmony_ci### `socket.remotePort`
11751cb0ef41Sopenharmony_ci
11761cb0ef41Sopenharmony_ci<!-- YAML
11771cb0ef41Sopenharmony_ciadded: v0.5.10
11781cb0ef41Sopenharmony_ci-->
11791cb0ef41Sopenharmony_ci
11801cb0ef41Sopenharmony_ci* {integer}
11811cb0ef41Sopenharmony_ci
11821cb0ef41Sopenharmony_ciThe numeric representation of the remote port. For example, `80` or `21`. Value may be `undefined` if
11831cb0ef41Sopenharmony_cithe socket is destroyed (for example, if the client disconnected).
11841cb0ef41Sopenharmony_ci
11851cb0ef41Sopenharmony_ci### `socket.resetAndDestroy()`
11861cb0ef41Sopenharmony_ci
11871cb0ef41Sopenharmony_ci<!-- YAML
11881cb0ef41Sopenharmony_ciadded: v18.3.0
11891cb0ef41Sopenharmony_ci-->
11901cb0ef41Sopenharmony_ci
11911cb0ef41Sopenharmony_ci* Returns: {net.Socket}
11921cb0ef41Sopenharmony_ci
11931cb0ef41Sopenharmony_ciClose the TCP connection by sending an RST packet and destroy the stream.
11941cb0ef41Sopenharmony_ciIf this TCP socket is in connecting status, it will send an RST packet and destroy this TCP socket once it is connected.
11951cb0ef41Sopenharmony_ciOtherwise, it will call `socket.destroy` with an `ERR_SOCKET_CLOSED` Error.
11961cb0ef41Sopenharmony_ciIf this is not a TCP socket (for example, a pipe), calling this method will immediately throw an `ERR_INVALID_HANDLE_TYPE` Error.
11971cb0ef41Sopenharmony_ci
11981cb0ef41Sopenharmony_ci### `socket.resume()`
11991cb0ef41Sopenharmony_ci
12001cb0ef41Sopenharmony_ci* Returns: {net.Socket} The socket itself.
12011cb0ef41Sopenharmony_ci
12021cb0ef41Sopenharmony_ciResumes reading after a call to [`socket.pause()`][].
12031cb0ef41Sopenharmony_ci
12041cb0ef41Sopenharmony_ci### `socket.setEncoding([encoding])`
12051cb0ef41Sopenharmony_ci
12061cb0ef41Sopenharmony_ci<!-- YAML
12071cb0ef41Sopenharmony_ciadded: v0.1.90
12081cb0ef41Sopenharmony_ci-->
12091cb0ef41Sopenharmony_ci
12101cb0ef41Sopenharmony_ci* `encoding` {string}
12111cb0ef41Sopenharmony_ci* Returns: {net.Socket} The socket itself.
12121cb0ef41Sopenharmony_ci
12131cb0ef41Sopenharmony_ciSet the encoding for the socket as a [Readable Stream][]. See
12141cb0ef41Sopenharmony_ci[`readable.setEncoding()`][] for more information.
12151cb0ef41Sopenharmony_ci
12161cb0ef41Sopenharmony_ci### `socket.setKeepAlive([enable][, initialDelay])`
12171cb0ef41Sopenharmony_ci
12181cb0ef41Sopenharmony_ci<!-- YAML
12191cb0ef41Sopenharmony_ciadded: v0.1.92
12201cb0ef41Sopenharmony_cichanges:
12211cb0ef41Sopenharmony_ci  - version:
12221cb0ef41Sopenharmony_ci    - v13.12.0
12231cb0ef41Sopenharmony_ci    - v12.17.0
12241cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/32204
12251cb0ef41Sopenharmony_ci    description: New defaults for `TCP_KEEPCNT` and `TCP_KEEPINTVL` socket options were added.
12261cb0ef41Sopenharmony_ci-->
12271cb0ef41Sopenharmony_ci
12281cb0ef41Sopenharmony_ci* `enable` {boolean} **Default:** `false`
12291cb0ef41Sopenharmony_ci* `initialDelay` {number} **Default:** `0`
12301cb0ef41Sopenharmony_ci* Returns: {net.Socket} The socket itself.
12311cb0ef41Sopenharmony_ci
12321cb0ef41Sopenharmony_ciEnable/disable keep-alive functionality, and optionally set the initial
12331cb0ef41Sopenharmony_cidelay before the first keepalive probe is sent on an idle socket.
12341cb0ef41Sopenharmony_ci
12351cb0ef41Sopenharmony_ciSet `initialDelay` (in milliseconds) to set the delay between the last
12361cb0ef41Sopenharmony_cidata packet received and the first keepalive probe. Setting `0` for
12371cb0ef41Sopenharmony_ci`initialDelay` will leave the value unchanged from the default
12381cb0ef41Sopenharmony_ci(or previous) setting.
12391cb0ef41Sopenharmony_ci
12401cb0ef41Sopenharmony_ciEnabling the keep-alive functionality will set the following socket options:
12411cb0ef41Sopenharmony_ci
12421cb0ef41Sopenharmony_ci* `SO_KEEPALIVE=1`
12431cb0ef41Sopenharmony_ci* `TCP_KEEPIDLE=initialDelay`
12441cb0ef41Sopenharmony_ci* `TCP_KEEPCNT=10`
12451cb0ef41Sopenharmony_ci* `TCP_KEEPINTVL=1`
12461cb0ef41Sopenharmony_ci
12471cb0ef41Sopenharmony_ci### `socket.setNoDelay([noDelay])`
12481cb0ef41Sopenharmony_ci
12491cb0ef41Sopenharmony_ci<!-- YAML
12501cb0ef41Sopenharmony_ciadded: v0.1.90
12511cb0ef41Sopenharmony_ci-->
12521cb0ef41Sopenharmony_ci
12531cb0ef41Sopenharmony_ci* `noDelay` {boolean} **Default:** `true`
12541cb0ef41Sopenharmony_ci* Returns: {net.Socket} The socket itself.
12551cb0ef41Sopenharmony_ci
12561cb0ef41Sopenharmony_ciEnable/disable the use of Nagle's algorithm.
12571cb0ef41Sopenharmony_ci
12581cb0ef41Sopenharmony_ciWhen a TCP connection is created, it will have Nagle's algorithm enabled.
12591cb0ef41Sopenharmony_ci
12601cb0ef41Sopenharmony_ciNagle's algorithm delays data before it is sent via the network. It attempts
12611cb0ef41Sopenharmony_cito optimize throughput at the expense of latency.
12621cb0ef41Sopenharmony_ci
12631cb0ef41Sopenharmony_ciPassing `true` for `noDelay` or not passing an argument will disable Nagle's
12641cb0ef41Sopenharmony_cialgorithm for the socket. Passing `false` for `noDelay` will enable Nagle's
12651cb0ef41Sopenharmony_cialgorithm.
12661cb0ef41Sopenharmony_ci
12671cb0ef41Sopenharmony_ci### `socket.setTimeout(timeout[, callback])`
12681cb0ef41Sopenharmony_ci
12691cb0ef41Sopenharmony_ci<!-- YAML
12701cb0ef41Sopenharmony_ciadded: v0.1.90
12711cb0ef41Sopenharmony_cichanges:
12721cb0ef41Sopenharmony_ci  - version: v18.0.0
12731cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/41678
12741cb0ef41Sopenharmony_ci    description: Passing an invalid callback to the `callback` argument
12751cb0ef41Sopenharmony_ci                 now throws `ERR_INVALID_ARG_TYPE` instead of
12761cb0ef41Sopenharmony_ci                 `ERR_INVALID_CALLBACK`.
12771cb0ef41Sopenharmony_ci-->
12781cb0ef41Sopenharmony_ci
12791cb0ef41Sopenharmony_ci* `timeout` {number}
12801cb0ef41Sopenharmony_ci* `callback` {Function}
12811cb0ef41Sopenharmony_ci* Returns: {net.Socket} The socket itself.
12821cb0ef41Sopenharmony_ci
12831cb0ef41Sopenharmony_ciSets the socket to timeout after `timeout` milliseconds of inactivity on
12841cb0ef41Sopenharmony_cithe socket. By default `net.Socket` do not have a timeout.
12851cb0ef41Sopenharmony_ci
12861cb0ef41Sopenharmony_ciWhen an idle timeout is triggered the socket will receive a [`'timeout'`][]
12871cb0ef41Sopenharmony_cievent but the connection will not be severed. The user must manually call
12881cb0ef41Sopenharmony_ci[`socket.end()`][] or [`socket.destroy()`][] to end the connection.
12891cb0ef41Sopenharmony_ci
12901cb0ef41Sopenharmony_ci```js
12911cb0ef41Sopenharmony_cisocket.setTimeout(3000);
12921cb0ef41Sopenharmony_cisocket.on('timeout', () => {
12931cb0ef41Sopenharmony_ci  console.log('socket timeout');
12941cb0ef41Sopenharmony_ci  socket.end();
12951cb0ef41Sopenharmony_ci});
12961cb0ef41Sopenharmony_ci```
12971cb0ef41Sopenharmony_ci
12981cb0ef41Sopenharmony_ciIf `timeout` is 0, then the existing idle timeout is disabled.
12991cb0ef41Sopenharmony_ci
13001cb0ef41Sopenharmony_ciThe optional `callback` parameter will be added as a one-time listener for the
13011cb0ef41Sopenharmony_ci[`'timeout'`][] event.
13021cb0ef41Sopenharmony_ci
13031cb0ef41Sopenharmony_ci### `socket.timeout`
13041cb0ef41Sopenharmony_ci
13051cb0ef41Sopenharmony_ci<!-- YAML
13061cb0ef41Sopenharmony_ciadded: v10.7.0
13071cb0ef41Sopenharmony_ci-->
13081cb0ef41Sopenharmony_ci
13091cb0ef41Sopenharmony_ci* {number|undefined}
13101cb0ef41Sopenharmony_ci
13111cb0ef41Sopenharmony_ciThe socket timeout in milliseconds as set by [`socket.setTimeout()`][].
13121cb0ef41Sopenharmony_ciIt is `undefined` if a timeout has not been set.
13131cb0ef41Sopenharmony_ci
13141cb0ef41Sopenharmony_ci### `socket.unref()`
13151cb0ef41Sopenharmony_ci
13161cb0ef41Sopenharmony_ci<!-- YAML
13171cb0ef41Sopenharmony_ciadded: v0.9.1
13181cb0ef41Sopenharmony_ci-->
13191cb0ef41Sopenharmony_ci
13201cb0ef41Sopenharmony_ci* Returns: {net.Socket} The socket itself.
13211cb0ef41Sopenharmony_ci
13221cb0ef41Sopenharmony_ciCalling `unref()` on a socket will allow the program to exit if this is the only
13231cb0ef41Sopenharmony_ciactive socket in the event system. If the socket is already `unref`ed calling
13241cb0ef41Sopenharmony_ci`unref()` again will have no effect.
13251cb0ef41Sopenharmony_ci
13261cb0ef41Sopenharmony_ci### `socket.write(data[, encoding][, callback])`
13271cb0ef41Sopenharmony_ci
13281cb0ef41Sopenharmony_ci<!-- YAML
13291cb0ef41Sopenharmony_ciadded: v0.1.90
13301cb0ef41Sopenharmony_ci-->
13311cb0ef41Sopenharmony_ci
13321cb0ef41Sopenharmony_ci* `data` {string|Buffer|Uint8Array}
13331cb0ef41Sopenharmony_ci* `encoding` {string} Only used when data is `string`. **Default:** `utf8`.
13341cb0ef41Sopenharmony_ci* `callback` {Function}
13351cb0ef41Sopenharmony_ci* Returns: {boolean}
13361cb0ef41Sopenharmony_ci
13371cb0ef41Sopenharmony_ciSends data on the socket. The second parameter specifies the encoding in the
13381cb0ef41Sopenharmony_cicase of a string. It defaults to UTF8 encoding.
13391cb0ef41Sopenharmony_ci
13401cb0ef41Sopenharmony_ciReturns `true` if the entire data was flushed successfully to the kernel
13411cb0ef41Sopenharmony_cibuffer. Returns `false` if all or part of the data was queued in user memory.
13421cb0ef41Sopenharmony_ci[`'drain'`][] will be emitted when the buffer is again free.
13431cb0ef41Sopenharmony_ci
13441cb0ef41Sopenharmony_ciThe optional `callback` parameter will be executed when the data is finally
13451cb0ef41Sopenharmony_ciwritten out, which may not be immediately.
13461cb0ef41Sopenharmony_ci
13471cb0ef41Sopenharmony_ciSee `Writable` stream [`write()`][stream_writable_write] method for more
13481cb0ef41Sopenharmony_ciinformation.
13491cb0ef41Sopenharmony_ci
13501cb0ef41Sopenharmony_ci### `socket.readyState`
13511cb0ef41Sopenharmony_ci
13521cb0ef41Sopenharmony_ci<!-- YAML
13531cb0ef41Sopenharmony_ciadded: v0.5.0
13541cb0ef41Sopenharmony_ci-->
13551cb0ef41Sopenharmony_ci
13561cb0ef41Sopenharmony_ci* {string}
13571cb0ef41Sopenharmony_ci
13581cb0ef41Sopenharmony_ciThis property represents the state of the connection as a string.
13591cb0ef41Sopenharmony_ci
13601cb0ef41Sopenharmony_ci* If the stream is connecting `socket.readyState` is `opening`.
13611cb0ef41Sopenharmony_ci* If the stream is readable and writable, it is `open`.
13621cb0ef41Sopenharmony_ci* If the stream is readable and not writable, it is `readOnly`.
13631cb0ef41Sopenharmony_ci* If the stream is not readable and writable, it is `writeOnly`.
13641cb0ef41Sopenharmony_ci
13651cb0ef41Sopenharmony_ci## `net.connect()`
13661cb0ef41Sopenharmony_ci
13671cb0ef41Sopenharmony_ciAliases to
13681cb0ef41Sopenharmony_ci[`net.createConnection()`][`net.createConnection()`].
13691cb0ef41Sopenharmony_ci
13701cb0ef41Sopenharmony_ciPossible signatures:
13711cb0ef41Sopenharmony_ci
13721cb0ef41Sopenharmony_ci* [`net.connect(options[, connectListener])`][`net.connect(options)`]
13731cb0ef41Sopenharmony_ci* [`net.connect(path[, connectListener])`][`net.connect(path)`] for [IPC][]
13741cb0ef41Sopenharmony_ci  connections.
13751cb0ef41Sopenharmony_ci* [`net.connect(port[, host][, connectListener])`][`net.connect(port, host)`]
13761cb0ef41Sopenharmony_ci  for TCP connections.
13771cb0ef41Sopenharmony_ci
13781cb0ef41Sopenharmony_ci### `net.connect(options[, connectListener])`
13791cb0ef41Sopenharmony_ci
13801cb0ef41Sopenharmony_ci<!-- YAML
13811cb0ef41Sopenharmony_ciadded: v0.7.0
13821cb0ef41Sopenharmony_ci-->
13831cb0ef41Sopenharmony_ci
13841cb0ef41Sopenharmony_ci* `options` {Object}
13851cb0ef41Sopenharmony_ci* `connectListener` {Function}
13861cb0ef41Sopenharmony_ci* Returns: {net.Socket}
13871cb0ef41Sopenharmony_ci
13881cb0ef41Sopenharmony_ciAlias to
13891cb0ef41Sopenharmony_ci[`net.createConnection(options[, connectListener])`][`net.createConnection(options)`].
13901cb0ef41Sopenharmony_ci
13911cb0ef41Sopenharmony_ci### `net.connect(path[, connectListener])`
13921cb0ef41Sopenharmony_ci
13931cb0ef41Sopenharmony_ci<!-- YAML
13941cb0ef41Sopenharmony_ciadded: v0.1.90
13951cb0ef41Sopenharmony_ci-->
13961cb0ef41Sopenharmony_ci
13971cb0ef41Sopenharmony_ci* `path` {string}
13981cb0ef41Sopenharmony_ci* `connectListener` {Function}
13991cb0ef41Sopenharmony_ci* Returns: {net.Socket}
14001cb0ef41Sopenharmony_ci
14011cb0ef41Sopenharmony_ciAlias to
14021cb0ef41Sopenharmony_ci[`net.createConnection(path[, connectListener])`][`net.createConnection(path)`].
14031cb0ef41Sopenharmony_ci
14041cb0ef41Sopenharmony_ci### `net.connect(port[, host][, connectListener])`
14051cb0ef41Sopenharmony_ci
14061cb0ef41Sopenharmony_ci<!-- YAML
14071cb0ef41Sopenharmony_ciadded: v0.1.90
14081cb0ef41Sopenharmony_ci-->
14091cb0ef41Sopenharmony_ci
14101cb0ef41Sopenharmony_ci* `port` {number}
14111cb0ef41Sopenharmony_ci* `host` {string}
14121cb0ef41Sopenharmony_ci* `connectListener` {Function}
14131cb0ef41Sopenharmony_ci* Returns: {net.Socket}
14141cb0ef41Sopenharmony_ci
14151cb0ef41Sopenharmony_ciAlias to
14161cb0ef41Sopenharmony_ci[`net.createConnection(port[, host][, connectListener])`][`net.createConnection(port, host)`].
14171cb0ef41Sopenharmony_ci
14181cb0ef41Sopenharmony_ci## `net.createConnection()`
14191cb0ef41Sopenharmony_ci
14201cb0ef41Sopenharmony_ciA factory function, which creates a new [`net.Socket`][],
14211cb0ef41Sopenharmony_ciimmediately initiates connection with [`socket.connect()`][],
14221cb0ef41Sopenharmony_cithen returns the `net.Socket` that starts the connection.
14231cb0ef41Sopenharmony_ci
14241cb0ef41Sopenharmony_ciWhen the connection is established, a [`'connect'`][] event will be emitted
14251cb0ef41Sopenharmony_cion the returned socket. The last parameter `connectListener`, if supplied,
14261cb0ef41Sopenharmony_ciwill be added as a listener for the [`'connect'`][] event **once**.
14271cb0ef41Sopenharmony_ci
14281cb0ef41Sopenharmony_ciPossible signatures:
14291cb0ef41Sopenharmony_ci
14301cb0ef41Sopenharmony_ci* [`net.createConnection(options[, connectListener])`][`net.createConnection(options)`]
14311cb0ef41Sopenharmony_ci* [`net.createConnection(path[, connectListener])`][`net.createConnection(path)`]
14321cb0ef41Sopenharmony_ci  for [IPC][] connections.
14331cb0ef41Sopenharmony_ci* [`net.createConnection(port[, host][, connectListener])`][`net.createConnection(port, host)`]
14341cb0ef41Sopenharmony_ci  for TCP connections.
14351cb0ef41Sopenharmony_ci
14361cb0ef41Sopenharmony_ciThe [`net.connect()`][] function is an alias to this function.
14371cb0ef41Sopenharmony_ci
14381cb0ef41Sopenharmony_ci### `net.createConnection(options[, connectListener])`
14391cb0ef41Sopenharmony_ci
14401cb0ef41Sopenharmony_ci<!-- YAML
14411cb0ef41Sopenharmony_ciadded: v0.1.90
14421cb0ef41Sopenharmony_ci-->
14431cb0ef41Sopenharmony_ci
14441cb0ef41Sopenharmony_ci* `options` {Object} Required. Will be passed to both the
14451cb0ef41Sopenharmony_ci  [`new net.Socket([options])`][`new net.Socket(options)`] call and the
14461cb0ef41Sopenharmony_ci  [`socket.connect(options[, connectListener])`][`socket.connect(options)`]
14471cb0ef41Sopenharmony_ci  method.
14481cb0ef41Sopenharmony_ci* `connectListener` {Function} Common parameter of the
14491cb0ef41Sopenharmony_ci  [`net.createConnection()`][] functions. If supplied, will be added as
14501cb0ef41Sopenharmony_ci  a listener for the [`'connect'`][] event on the returned socket once.
14511cb0ef41Sopenharmony_ci* Returns: {net.Socket} The newly created socket used to start the connection.
14521cb0ef41Sopenharmony_ci
14531cb0ef41Sopenharmony_ciFor available options, see
14541cb0ef41Sopenharmony_ci[`new net.Socket([options])`][`new net.Socket(options)`]
14551cb0ef41Sopenharmony_ciand [`socket.connect(options[, connectListener])`][`socket.connect(options)`].
14561cb0ef41Sopenharmony_ci
14571cb0ef41Sopenharmony_ciAdditional options:
14581cb0ef41Sopenharmony_ci
14591cb0ef41Sopenharmony_ci* `timeout` {number} If set, will be used to call
14601cb0ef41Sopenharmony_ci  [`socket.setTimeout(timeout)`][] after the socket is created, but before
14611cb0ef41Sopenharmony_ci  it starts the connection.
14621cb0ef41Sopenharmony_ci
14631cb0ef41Sopenharmony_ciFollowing is an example of a client of the echo server described
14641cb0ef41Sopenharmony_ciin the [`net.createServer()`][] section:
14651cb0ef41Sopenharmony_ci
14661cb0ef41Sopenharmony_ci```js
14671cb0ef41Sopenharmony_ciconst net = require('node:net');
14681cb0ef41Sopenharmony_ciconst client = net.createConnection({ port: 8124 }, () => {
14691cb0ef41Sopenharmony_ci  // 'connect' listener.
14701cb0ef41Sopenharmony_ci  console.log('connected to server!');
14711cb0ef41Sopenharmony_ci  client.write('world!\r\n');
14721cb0ef41Sopenharmony_ci});
14731cb0ef41Sopenharmony_ciclient.on('data', (data) => {
14741cb0ef41Sopenharmony_ci  console.log(data.toString());
14751cb0ef41Sopenharmony_ci  client.end();
14761cb0ef41Sopenharmony_ci});
14771cb0ef41Sopenharmony_ciclient.on('end', () => {
14781cb0ef41Sopenharmony_ci  console.log('disconnected from server');
14791cb0ef41Sopenharmony_ci});
14801cb0ef41Sopenharmony_ci```
14811cb0ef41Sopenharmony_ci
14821cb0ef41Sopenharmony_ciTo connect on the socket `/tmp/echo.sock`:
14831cb0ef41Sopenharmony_ci
14841cb0ef41Sopenharmony_ci```js
14851cb0ef41Sopenharmony_ciconst client = net.createConnection({ path: '/tmp/echo.sock' });
14861cb0ef41Sopenharmony_ci```
14871cb0ef41Sopenharmony_ci
14881cb0ef41Sopenharmony_ci### `net.createConnection(path[, connectListener])`
14891cb0ef41Sopenharmony_ci
14901cb0ef41Sopenharmony_ci<!-- YAML
14911cb0ef41Sopenharmony_ciadded: v0.1.90
14921cb0ef41Sopenharmony_ci-->
14931cb0ef41Sopenharmony_ci
14941cb0ef41Sopenharmony_ci* `path` {string} Path the socket should connect to. Will be passed to
14951cb0ef41Sopenharmony_ci  [`socket.connect(path[, connectListener])`][`socket.connect(path)`].
14961cb0ef41Sopenharmony_ci  See [Identifying paths for IPC connections][].
14971cb0ef41Sopenharmony_ci* `connectListener` {Function} Common parameter of the
14981cb0ef41Sopenharmony_ci  [`net.createConnection()`][] functions, an "once" listener for the
14991cb0ef41Sopenharmony_ci  `'connect'` event on the initiating socket. Will be passed to
15001cb0ef41Sopenharmony_ci  [`socket.connect(path[, connectListener])`][`socket.connect(path)`].
15011cb0ef41Sopenharmony_ci* Returns: {net.Socket} The newly created socket used to start the connection.
15021cb0ef41Sopenharmony_ci
15031cb0ef41Sopenharmony_ciInitiates an [IPC][] connection.
15041cb0ef41Sopenharmony_ci
15051cb0ef41Sopenharmony_ciThis function creates a new [`net.Socket`][] with all options set to default,
15061cb0ef41Sopenharmony_ciimmediately initiates connection with
15071cb0ef41Sopenharmony_ci[`socket.connect(path[, connectListener])`][`socket.connect(path)`],
15081cb0ef41Sopenharmony_cithen returns the `net.Socket` that starts the connection.
15091cb0ef41Sopenharmony_ci
15101cb0ef41Sopenharmony_ci### `net.createConnection(port[, host][, connectListener])`
15111cb0ef41Sopenharmony_ci
15121cb0ef41Sopenharmony_ci<!-- YAML
15131cb0ef41Sopenharmony_ciadded: v0.1.90
15141cb0ef41Sopenharmony_ci-->
15151cb0ef41Sopenharmony_ci
15161cb0ef41Sopenharmony_ci* `port` {number} Port the socket should connect to. Will be passed to
15171cb0ef41Sopenharmony_ci  [`socket.connect(port[, host][, connectListener])`][`socket.connect(port)`].
15181cb0ef41Sopenharmony_ci* `host` {string} Host the socket should connect to. Will be passed to
15191cb0ef41Sopenharmony_ci  [`socket.connect(port[, host][, connectListener])`][`socket.connect(port)`].
15201cb0ef41Sopenharmony_ci  **Default:** `'localhost'`.
15211cb0ef41Sopenharmony_ci* `connectListener` {Function} Common parameter of the
15221cb0ef41Sopenharmony_ci  [`net.createConnection()`][] functions, an "once" listener for the
15231cb0ef41Sopenharmony_ci  `'connect'` event on the initiating socket. Will be passed to
15241cb0ef41Sopenharmony_ci  [`socket.connect(port[, host][, connectListener])`][`socket.connect(port)`].
15251cb0ef41Sopenharmony_ci* Returns: {net.Socket} The newly created socket used to start the connection.
15261cb0ef41Sopenharmony_ci
15271cb0ef41Sopenharmony_ciInitiates a TCP connection.
15281cb0ef41Sopenharmony_ci
15291cb0ef41Sopenharmony_ciThis function creates a new [`net.Socket`][] with all options set to default,
15301cb0ef41Sopenharmony_ciimmediately initiates connection with
15311cb0ef41Sopenharmony_ci[`socket.connect(port[, host][, connectListener])`][`socket.connect(port)`],
15321cb0ef41Sopenharmony_cithen returns the `net.Socket` that starts the connection.
15331cb0ef41Sopenharmony_ci
15341cb0ef41Sopenharmony_ci## `net.createServer([options][, connectionListener])`
15351cb0ef41Sopenharmony_ci
15361cb0ef41Sopenharmony_ci<!-- YAML
15371cb0ef41Sopenharmony_ciadded: v0.5.0
15381cb0ef41Sopenharmony_cichanges:
15391cb0ef41Sopenharmony_ci  - version: v18.17.0
15401cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/47405
15411cb0ef41Sopenharmony_ci    description: The `highWaterMark` option is supported now.
15421cb0ef41Sopenharmony_ci  - version:
15431cb0ef41Sopenharmony_ci    - v17.7.0
15441cb0ef41Sopenharmony_ci    - v16.15.0
15451cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/41310
15461cb0ef41Sopenharmony_ci    description: The `noDelay`, `keepAlive`, and `keepAliveInitialDelay`
15471cb0ef41Sopenharmony_ci                 options are supported now.
15481cb0ef41Sopenharmony_ci-->
15491cb0ef41Sopenharmony_ci
15501cb0ef41Sopenharmony_ci* `options` {Object}
15511cb0ef41Sopenharmony_ci  * `allowHalfOpen` {boolean} If set to `false`, then the socket will
15521cb0ef41Sopenharmony_ci    automatically end the writable side when the readable side ends.
15531cb0ef41Sopenharmony_ci    **Default:** `false`.
15541cb0ef41Sopenharmony_ci  * `highWaterMark` {number} Optionally overrides all [`net.Socket`][]s'
15551cb0ef41Sopenharmony_ci    `readableHighWaterMark` and `writableHighWaterMark`.
15561cb0ef41Sopenharmony_ci    **Default:** See [`stream.getDefaultHighWaterMark()`][].
15571cb0ef41Sopenharmony_ci  * `pauseOnConnect` {boolean} Indicates whether the socket should be
15581cb0ef41Sopenharmony_ci    paused on incoming connections. **Default:** `false`.
15591cb0ef41Sopenharmony_ci  * `noDelay` {boolean} If set to `true`, it disables the use of Nagle's algorithm immediately
15601cb0ef41Sopenharmony_ci    after a new incoming connection is received. **Default:** `false`.
15611cb0ef41Sopenharmony_ci  * `keepAlive` {boolean} If set to `true`, it enables keep-alive functionality on the socket
15621cb0ef41Sopenharmony_ci    immediately after a new incoming connection is received, similarly on what is done in
15631cb0ef41Sopenharmony_ci    [`socket.setKeepAlive([enable][, initialDelay])`][`socket.setKeepAlive(enable, initialDelay)`].
15641cb0ef41Sopenharmony_ci    **Default:** `false`.
15651cb0ef41Sopenharmony_ci  * `keepAliveInitialDelay` {number} If set to a positive number, it sets the initial delay before
15661cb0ef41Sopenharmony_ci    the first keepalive probe is sent on an idle socket.**Default:** `0`.
15671cb0ef41Sopenharmony_ci
15681cb0ef41Sopenharmony_ci* `connectionListener` {Function} Automatically set as a listener for the
15691cb0ef41Sopenharmony_ci  [`'connection'`][] event.
15701cb0ef41Sopenharmony_ci
15711cb0ef41Sopenharmony_ci* Returns: {net.Server}
15721cb0ef41Sopenharmony_ci
15731cb0ef41Sopenharmony_ciCreates a new TCP or [IPC][] server.
15741cb0ef41Sopenharmony_ci
15751cb0ef41Sopenharmony_ciIf `allowHalfOpen` is set to `true`, when the other end of the socket
15761cb0ef41Sopenharmony_cisignals the end of transmission, the server will only send back the end of
15771cb0ef41Sopenharmony_citransmission when [`socket.end()`][] is explicitly called. For example, in the
15781cb0ef41Sopenharmony_cicontext of TCP, when a FIN packed is received, a FIN packed is sent
15791cb0ef41Sopenharmony_ciback only when [`socket.end()`][] is explicitly called. Until then the
15801cb0ef41Sopenharmony_ciconnection is half-closed (non-readable but still writable). See [`'end'`][]
15811cb0ef41Sopenharmony_cievent and [RFC 1122][half-closed] (section 4.2.2.13) for more information.
15821cb0ef41Sopenharmony_ci
15831cb0ef41Sopenharmony_ciIf `pauseOnConnect` is set to `true`, then the socket associated with each
15841cb0ef41Sopenharmony_ciincoming connection will be paused, and no data will be read from its handle.
15851cb0ef41Sopenharmony_ciThis allows connections to be passed between processes without any data being
15861cb0ef41Sopenharmony_ciread by the original process. To begin reading data from a paused socket, call
15871cb0ef41Sopenharmony_ci[`socket.resume()`][].
15881cb0ef41Sopenharmony_ci
15891cb0ef41Sopenharmony_ciThe server can be a TCP server or an [IPC][] server, depending on what it
15901cb0ef41Sopenharmony_ci[`listen()`][`server.listen()`] to.
15911cb0ef41Sopenharmony_ci
15921cb0ef41Sopenharmony_ciHere is an example of a TCP echo server which listens for connections
15931cb0ef41Sopenharmony_cion port 8124:
15941cb0ef41Sopenharmony_ci
15951cb0ef41Sopenharmony_ci```js
15961cb0ef41Sopenharmony_ciconst net = require('node:net');
15971cb0ef41Sopenharmony_ciconst server = net.createServer((c) => {
15981cb0ef41Sopenharmony_ci  // 'connection' listener.
15991cb0ef41Sopenharmony_ci  console.log('client connected');
16001cb0ef41Sopenharmony_ci  c.on('end', () => {
16011cb0ef41Sopenharmony_ci    console.log('client disconnected');
16021cb0ef41Sopenharmony_ci  });
16031cb0ef41Sopenharmony_ci  c.write('hello\r\n');
16041cb0ef41Sopenharmony_ci  c.pipe(c);
16051cb0ef41Sopenharmony_ci});
16061cb0ef41Sopenharmony_ciserver.on('error', (err) => {
16071cb0ef41Sopenharmony_ci  throw err;
16081cb0ef41Sopenharmony_ci});
16091cb0ef41Sopenharmony_ciserver.listen(8124, () => {
16101cb0ef41Sopenharmony_ci  console.log('server bound');
16111cb0ef41Sopenharmony_ci});
16121cb0ef41Sopenharmony_ci```
16131cb0ef41Sopenharmony_ci
16141cb0ef41Sopenharmony_ciTest this by using `telnet`:
16151cb0ef41Sopenharmony_ci
16161cb0ef41Sopenharmony_ci```console
16171cb0ef41Sopenharmony_ci$ telnet localhost 8124
16181cb0ef41Sopenharmony_ci```
16191cb0ef41Sopenharmony_ci
16201cb0ef41Sopenharmony_ciTo listen on the socket `/tmp/echo.sock`:
16211cb0ef41Sopenharmony_ci
16221cb0ef41Sopenharmony_ci```js
16231cb0ef41Sopenharmony_ciserver.listen('/tmp/echo.sock', () => {
16241cb0ef41Sopenharmony_ci  console.log('server bound');
16251cb0ef41Sopenharmony_ci});
16261cb0ef41Sopenharmony_ci```
16271cb0ef41Sopenharmony_ci
16281cb0ef41Sopenharmony_ciUse `nc` to connect to a Unix domain socket server:
16291cb0ef41Sopenharmony_ci
16301cb0ef41Sopenharmony_ci```console
16311cb0ef41Sopenharmony_ci$ nc -U /tmp/echo.sock
16321cb0ef41Sopenharmony_ci```
16331cb0ef41Sopenharmony_ci
16341cb0ef41Sopenharmony_ci## `net.getDefaultAutoSelectFamily()`
16351cb0ef41Sopenharmony_ci
16361cb0ef41Sopenharmony_ci<!-- YAML
16371cb0ef41Sopenharmony_ciadded: v19.4.0
16381cb0ef41Sopenharmony_ci-->
16391cb0ef41Sopenharmony_ci
16401cb0ef41Sopenharmony_ciGets the current default value of the `autoSelectFamily` option of [`socket.connect(options)`][].
16411cb0ef41Sopenharmony_ci
16421cb0ef41Sopenharmony_ci* Returns: {boolean} The current default value of the `autoSelectFamily` option.
16431cb0ef41Sopenharmony_ci
16441cb0ef41Sopenharmony_ci## `net.setDefaultAutoSelectFamily(value)`
16451cb0ef41Sopenharmony_ci
16461cb0ef41Sopenharmony_ci<!-- YAML
16471cb0ef41Sopenharmony_ciadded: v19.4.0
16481cb0ef41Sopenharmony_ci-->
16491cb0ef41Sopenharmony_ci
16501cb0ef41Sopenharmony_ciSets the default value of the `autoSelectFamily` option of [`socket.connect(options)`][].
16511cb0ef41Sopenharmony_ci
16521cb0ef41Sopenharmony_ci* `value` {boolean} The new default value. The initial default value is `false`.
16531cb0ef41Sopenharmony_ci
16541cb0ef41Sopenharmony_ci## `net.getDefaultAutoSelectFamilyAttemptTimeout()`
16551cb0ef41Sopenharmony_ci
16561cb0ef41Sopenharmony_ci<!-- YAML
16571cb0ef41Sopenharmony_ciadded: v18.18.0
16581cb0ef41Sopenharmony_ci-->
16591cb0ef41Sopenharmony_ci
16601cb0ef41Sopenharmony_ciGets the current default value of the `autoSelectFamilyAttemptTimeout` option of [`socket.connect(options)`][].
16611cb0ef41Sopenharmony_ci
16621cb0ef41Sopenharmony_ci* Returns: {number} The current default value of the `autoSelectFamilyAttemptTimeout` option.
16631cb0ef41Sopenharmony_ci
16641cb0ef41Sopenharmony_ci## `net.setDefaultAutoSelectFamilyAttemptTimeout(value)`
16651cb0ef41Sopenharmony_ci
16661cb0ef41Sopenharmony_ci<!-- YAML
16671cb0ef41Sopenharmony_ciadded: v18.18.0
16681cb0ef41Sopenharmony_ci-->
16691cb0ef41Sopenharmony_ci
16701cb0ef41Sopenharmony_ciSets the default value of the `autoSelectFamilyAttemptTimeout` option of [`socket.connect(options)`][].
16711cb0ef41Sopenharmony_ci
16721cb0ef41Sopenharmony_ci* `value` {number} The new default value, which must be a positive number. If the number is less than `10`,
16731cb0ef41Sopenharmony_ci  the value `10` is used instead. The initial default value is `250`.
16741cb0ef41Sopenharmony_ci
16751cb0ef41Sopenharmony_ci## `net.isIP(input)`
16761cb0ef41Sopenharmony_ci
16771cb0ef41Sopenharmony_ci<!-- YAML
16781cb0ef41Sopenharmony_ciadded: v0.3.0
16791cb0ef41Sopenharmony_ci-->
16801cb0ef41Sopenharmony_ci
16811cb0ef41Sopenharmony_ci* `input` {string}
16821cb0ef41Sopenharmony_ci* Returns: {integer}
16831cb0ef41Sopenharmony_ci
16841cb0ef41Sopenharmony_ciReturns `6` if `input` is an IPv6 address. Returns `4` if `input` is an IPv4
16851cb0ef41Sopenharmony_ciaddress in [dot-decimal notation][] with no leading zeroes. Otherwise, returns
16861cb0ef41Sopenharmony_ci`0`.
16871cb0ef41Sopenharmony_ci
16881cb0ef41Sopenharmony_ci```js
16891cb0ef41Sopenharmony_cinet.isIP('::1'); // returns 6
16901cb0ef41Sopenharmony_cinet.isIP('127.0.0.1'); // returns 4
16911cb0ef41Sopenharmony_cinet.isIP('127.000.000.001'); // returns 0
16921cb0ef41Sopenharmony_cinet.isIP('127.0.0.1/24'); // returns 0
16931cb0ef41Sopenharmony_cinet.isIP('fhqwhgads'); // returns 0
16941cb0ef41Sopenharmony_ci```
16951cb0ef41Sopenharmony_ci
16961cb0ef41Sopenharmony_ci## `net.isIPv4(input)`
16971cb0ef41Sopenharmony_ci
16981cb0ef41Sopenharmony_ci<!-- YAML
16991cb0ef41Sopenharmony_ciadded: v0.3.0
17001cb0ef41Sopenharmony_ci-->
17011cb0ef41Sopenharmony_ci
17021cb0ef41Sopenharmony_ci* `input` {string}
17031cb0ef41Sopenharmony_ci* Returns: {boolean}
17041cb0ef41Sopenharmony_ci
17051cb0ef41Sopenharmony_ciReturns `true` if `input` is an IPv4 address in [dot-decimal notation][] with no
17061cb0ef41Sopenharmony_cileading zeroes. Otherwise, returns `false`.
17071cb0ef41Sopenharmony_ci
17081cb0ef41Sopenharmony_ci```js
17091cb0ef41Sopenharmony_cinet.isIPv4('127.0.0.1'); // returns true
17101cb0ef41Sopenharmony_cinet.isIPv4('127.000.000.001'); // returns false
17111cb0ef41Sopenharmony_cinet.isIPv4('127.0.0.1/24'); // returns false
17121cb0ef41Sopenharmony_cinet.isIPv4('fhqwhgads'); // returns false
17131cb0ef41Sopenharmony_ci```
17141cb0ef41Sopenharmony_ci
17151cb0ef41Sopenharmony_ci## `net.isIPv6(input)`
17161cb0ef41Sopenharmony_ci
17171cb0ef41Sopenharmony_ci<!-- YAML
17181cb0ef41Sopenharmony_ciadded: v0.3.0
17191cb0ef41Sopenharmony_ci-->
17201cb0ef41Sopenharmony_ci
17211cb0ef41Sopenharmony_ci* `input` {string}
17221cb0ef41Sopenharmony_ci* Returns: {boolean}
17231cb0ef41Sopenharmony_ci
17241cb0ef41Sopenharmony_ciReturns `true` if `input` is an IPv6 address. Otherwise, returns `false`.
17251cb0ef41Sopenharmony_ci
17261cb0ef41Sopenharmony_ci```js
17271cb0ef41Sopenharmony_cinet.isIPv6('::1'); // returns true
17281cb0ef41Sopenharmony_cinet.isIPv6('fhqwhgads'); // returns false
17291cb0ef41Sopenharmony_ci```
17301cb0ef41Sopenharmony_ci
17311cb0ef41Sopenharmony_ci[IPC]: #ipc-support
17321cb0ef41Sopenharmony_ci[Identifying paths for IPC connections]: #identifying-paths-for-ipc-connections
17331cb0ef41Sopenharmony_ci[RFC 8305]: https://www.rfc-editor.org/rfc/rfc8305.txt
17341cb0ef41Sopenharmony_ci[Readable Stream]: stream.md#class-streamreadable
17351cb0ef41Sopenharmony_ci[`'close'`]: #event-close
17361cb0ef41Sopenharmony_ci[`'connect'`]: #event-connect
17371cb0ef41Sopenharmony_ci[`'connection'`]: #event-connection
17381cb0ef41Sopenharmony_ci[`'data'`]: #event-data
17391cb0ef41Sopenharmony_ci[`'drain'`]: #event-drain
17401cb0ef41Sopenharmony_ci[`'end'`]: #event-end
17411cb0ef41Sopenharmony_ci[`'error'`]: #event-error_1
17421cb0ef41Sopenharmony_ci[`'listening'`]: #event-listening
17431cb0ef41Sopenharmony_ci[`'timeout'`]: #event-timeout
17441cb0ef41Sopenharmony_ci[`EventEmitter`]: events.md#class-eventemitter
17451cb0ef41Sopenharmony_ci[`child_process.fork()`]: child_process.md#child_processforkmodulepath-args-options
17461cb0ef41Sopenharmony_ci[`dns.lookup()`]: dns.md#dnslookuphostname-options-callback
17471cb0ef41Sopenharmony_ci[`dns.lookup()` hints]: dns.md#supported-getaddrinfo-flags
17481cb0ef41Sopenharmony_ci[`net.Server`]: #class-netserver
17491cb0ef41Sopenharmony_ci[`net.Socket`]: #class-netsocket
17501cb0ef41Sopenharmony_ci[`net.connect()`]: #netconnect
17511cb0ef41Sopenharmony_ci[`net.connect(options)`]: #netconnectoptions-connectlistener
17521cb0ef41Sopenharmony_ci[`net.connect(path)`]: #netconnectpath-connectlistener
17531cb0ef41Sopenharmony_ci[`net.connect(port, host)`]: #netconnectport-host-connectlistener
17541cb0ef41Sopenharmony_ci[`net.createConnection()`]: #netcreateconnection
17551cb0ef41Sopenharmony_ci[`net.createConnection(options)`]: #netcreateconnectionoptions-connectlistener
17561cb0ef41Sopenharmony_ci[`net.createConnection(path)`]: #netcreateconnectionpath-connectlistener
17571cb0ef41Sopenharmony_ci[`net.createConnection(port, host)`]: #netcreateconnectionport-host-connectlistener
17581cb0ef41Sopenharmony_ci[`net.createServer()`]: #netcreateserveroptions-connectionlistener
17591cb0ef41Sopenharmony_ci[`net.setDefaultAutoSelectFamily(value)`]: #netsetdefaultautoselectfamilyvalue
17601cb0ef41Sopenharmony_ci[`net.setDefaultAutoSelectFamilyAttemptTimeout(value)`]: #netsetdefaultautoselectfamilyattempttimeoutvalue
17611cb0ef41Sopenharmony_ci[`new net.Socket(options)`]: #new-netsocketoptions
17621cb0ef41Sopenharmony_ci[`readable.setEncoding()`]: stream.md#readablesetencodingencoding
17631cb0ef41Sopenharmony_ci[`server.close()`]: #serverclosecallback
17641cb0ef41Sopenharmony_ci[`server.listen()`]: #serverlisten
17651cb0ef41Sopenharmony_ci[`server.listen(handle)`]: #serverlistenhandle-backlog-callback
17661cb0ef41Sopenharmony_ci[`server.listen(options)`]: #serverlistenoptions-callback
17671cb0ef41Sopenharmony_ci[`server.listen(path)`]: #serverlistenpath-backlog-callback
17681cb0ef41Sopenharmony_ci[`server.listen(port)`]: #serverlistenport-host-backlog-callback
17691cb0ef41Sopenharmony_ci[`socket(7)`]: https://man7.org/linux/man-pages/man7/socket.7.html
17701cb0ef41Sopenharmony_ci[`socket.connect()`]: #socketconnect
17711cb0ef41Sopenharmony_ci[`socket.connect(options)`]: #socketconnectoptions-connectlistener
17721cb0ef41Sopenharmony_ci[`socket.connect(path)`]: #socketconnectpath-connectlistener
17731cb0ef41Sopenharmony_ci[`socket.connect(port)`]: #socketconnectport-host-connectlistener
17741cb0ef41Sopenharmony_ci[`socket.connecting`]: #socketconnecting
17751cb0ef41Sopenharmony_ci[`socket.destroy()`]: #socketdestroyerror
17761cb0ef41Sopenharmony_ci[`socket.end()`]: #socketenddata-encoding-callback
17771cb0ef41Sopenharmony_ci[`socket.pause()`]: #socketpause
17781cb0ef41Sopenharmony_ci[`socket.resume()`]: #socketresume
17791cb0ef41Sopenharmony_ci[`socket.setEncoding()`]: #socketsetencodingencoding
17801cb0ef41Sopenharmony_ci[`socket.setKeepAlive(enable, initialDelay)`]: #socketsetkeepaliveenable-initialdelay
17811cb0ef41Sopenharmony_ci[`socket.setTimeout()`]: #socketsettimeouttimeout-callback
17821cb0ef41Sopenharmony_ci[`socket.setTimeout(timeout)`]: #socketsettimeouttimeout-callback
17831cb0ef41Sopenharmony_ci[`stream.getDefaultHighWaterMark()`]: stream.md#streamgetdefaulthighwatermarkobjectmode
17841cb0ef41Sopenharmony_ci[`writable.destroy()`]: stream.md#writabledestroyerror
17851cb0ef41Sopenharmony_ci[`writable.destroyed`]: stream.md#writabledestroyed
17861cb0ef41Sopenharmony_ci[`writable.end()`]: stream.md#writableendchunk-encoding-callback
17871cb0ef41Sopenharmony_ci[`writable.writableLength`]: stream.md#writablewritablelength
17881cb0ef41Sopenharmony_ci[dot-decimal notation]: https://en.wikipedia.org/wiki/Dot-decimal_notation
17891cb0ef41Sopenharmony_ci[half-closed]: https://tools.ietf.org/html/rfc1122
17901cb0ef41Sopenharmony_ci[stream_writable_write]: stream.md#writablewritechunk-encoding-callback
17911cb0ef41Sopenharmony_ci[unspecified IPv4 address]: https://en.wikipedia.org/wiki/0.0.0.0
17921cb0ef41Sopenharmony_ci[unspecified IPv6 address]: https://en.wikipedia.org/wiki/IPv6_address#Unspecified_address
1793