/usr/share/doc/nodejs/html/api
{ "type": "module", "source": "doc/api/net.md", "modules": [ { "textRaw": "Net", "name": "net", "introduced_in": "v0.10.0", "desc": "<!--lint disable maximum-line-length-->\n<blockquote>\n<p>Stability: 2 - Stable</p>\n</blockquote>\n<p><strong>Source Code:</strong> <a href=\"https://github.com/nodejs/node/blob/v16.20.2/lib/net.js\">lib/net.js</a></p>\n<p>The <code>node:net</code> module provides an asynchronous network API for creating stream-based\nTCP or <a href=\"#ipc-support\">IPC</a> servers (<a href=\"#netcreateserveroptions-connectionlistener\"><code>net.createServer()</code></a>) and clients\n(<a href=\"#netcreateconnection\"><code>net.createConnection()</code></a>).</p>\n<p>It can be accessed using:</p>\n<pre><code class=\"language-js\">const net = require('node:net');\n</code></pre>", "modules": [ { "textRaw": "IPC support", "name": "ipc_support", "desc": "<p>The <code>node:net</code> module supports IPC with named pipes on Windows, and Unix domain\nsockets on other operating systems.</p>", "modules": [ { "textRaw": "Identifying paths for IPC connections", "name": "identifying_paths_for_ipc_connections", "desc": "<p><a href=\"#netconnect\"><code>net.connect()</code></a>, <a href=\"#netcreateconnection\"><code>net.createConnection()</code></a>, <a href=\"#serverlisten\"><code>server.listen()</code></a>, and\n<a href=\"#socketconnect\"><code>socket.connect()</code></a> take a <code>path</code> parameter to identify IPC endpoints.</p>\n<p>On Unix, the local domain is also known as the Unix domain. The path is a\nfilesystem pathname. It gets truncated to an OS-dependent length of\n<code>sizeof(sockaddr_un.sun_path) - 1</code>. Typical values are 107 bytes on Linux and\n103 bytes on macOS. If a Node.js API abstraction creates the Unix domain socket,\nit will unlink the Unix domain socket as well. For example,\n<a href=\"#netcreateserveroptions-connectionlistener\"><code>net.createServer()</code></a> may create a Unix domain socket and\n<a href=\"#serverclosecallback\"><code>server.close()</code></a> will unlink it. But if a user creates the Unix domain\nsocket outside of these abstractions, the user will need to remove it. The same\napplies when a Node.js API creates a Unix domain socket but the program then\ncrashes. In short, a Unix domain socket will be visible in the filesystem and\nwill persist until unlinked.</p>\n<p>On Windows, the local domain is implemented using a named pipe. The path <em>must</em>\nrefer to an entry in <code>\\\\?\\pipe\\</code> or <code>\\\\.\\pipe\\</code>. Any characters are permitted,\nbut the latter may do some processing of pipe names, such as resolving <code>..</code>\nsequences. Despite how it might look, the pipe namespace is flat. Pipes will\n<em>not persist</em>. They are removed when the last reference to them is closed.\nUnlike Unix domain sockets, Windows will close and remove the pipe when the\nowning process exits.</p>\n<p>JavaScript string escaping requires paths to be specified with extra backslash\nescaping such as:</p>\n<pre><code class=\"language-js\">net.createServer().listen(\n path.join('\\\\\\\\?\\\\pipe', process.cwd(), 'myctl'));\n</code></pre>", "type": "module", "displayName": "Identifying paths for IPC connections" } ], "type": "module", "displayName": "IPC support" } ], "classes": [ { "textRaw": "Class: `net.BlockList`", "type": "class", "name": "net.BlockList", "meta": { "added": [ "v15.0.0" ], "changes": [] }, "desc": "<p>The <code>BlockList</code> object can be used with some network APIs to specify rules for\ndisabling inbound or outbound access to specific IP addresses, IP ranges, or\nIP subnets.</p>", "methods": [ { "textRaw": "`blockList.addAddress(address[, type])`", "type": "method", "name": "addAddress", "meta": { "added": [ "v15.0.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`address` {string|net.SocketAddress} An IPv4 or IPv6 address.", "name": "address", "type": "string|net.SocketAddress", "desc": "An IPv4 or IPv6 address." }, { "textRaw": "`type` {string} Either `'ipv4'` or `'ipv6'`. **Default:** `'ipv4'`.", "name": "type", "type": "string", "default": "`'ipv4'`", "desc": "Either `'ipv4'` or `'ipv6'`." } ] } ], "desc": "<p>Adds a rule to block the given IP address.</p>" }, { "textRaw": "`blockList.addRange(start, end[, type])`", "type": "method", "name": "addRange", "meta": { "added": [ "v15.0.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`start` {string|net.SocketAddress} The starting IPv4 or IPv6 address in the range.", "name": "start", "type": "string|net.SocketAddress", "desc": "The starting IPv4 or IPv6 address in the range." }, { "textRaw": "`end` {string|net.SocketAddress} The ending IPv4 or IPv6 address in the range.", "name": "end", "type": "string|net.SocketAddress", "desc": "The ending IPv4 or IPv6 address in the range." }, { "textRaw": "`type` {string} Either `'ipv4'` or `'ipv6'`. **Default:** `'ipv4'`.", "name": "type", "type": "string", "default": "`'ipv4'`", "desc": "Either `'ipv4'` or `'ipv6'`." } ] } ], "desc": "<p>Adds a rule to block a range of IP addresses from <code>start</code> (inclusive) to\n<code>end</code> (inclusive).</p>" }, { "textRaw": "`blockList.addSubnet(net, prefix[, type])`", "type": "method", "name": "addSubnet", "meta": { "added": [ "v15.0.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`net` {string|net.SocketAddress} The network IPv4 or IPv6 address.", "name": "net", "type": "string|net.SocketAddress", "desc": "The network IPv4 or IPv6 address." }, { "textRaw": "`prefix` {number} The number of CIDR prefix bits. For IPv4, this must be a value between `0` and `32`. For IPv6, this must be between `0` and `128`.", "name": "prefix", "type": "number", "desc": "The number of CIDR prefix bits. For IPv4, this must be a value between `0` and `32`. For IPv6, this must be between `0` and `128`." }, { "textRaw": "`type` {string} Either `'ipv4'` or `'ipv6'`. **Default:** `'ipv4'`.", "name": "type", "type": "string", "default": "`'ipv4'`", "desc": "Either `'ipv4'` or `'ipv6'`." } ] } ], "desc": "<p>Adds a rule to block a range of IP addresses specified as a subnet mask.</p>" }, { "textRaw": "`blockList.check(address[, type])`", "type": "method", "name": "check", "meta": { "added": [ "v15.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [ { "textRaw": "`address` {string|net.SocketAddress} The IP address to check", "name": "address", "type": "string|net.SocketAddress", "desc": "The IP address to check" }, { "textRaw": "`type` {string} Either `'ipv4'` or `'ipv6'`. **Default:** `'ipv4'`.", "name": "type", "type": "string", "default": "`'ipv4'`", "desc": "Either `'ipv4'` or `'ipv6'`." } ] } ], "desc": "<p>Returns <code>true</code> if the given IP address matches any of the rules added to the\n<code>BlockList</code>.</p>\n<pre><code class=\"language-js\">const blockList = new net.BlockList();\nblockList.addAddress('123.123.123.123');\nblockList.addRange('10.0.0.1', '10.0.0.10');\nblockList.addSubnet('8592:757c:efae:4e45::', 64, 'ipv6');\n\nconsole.log(blockList.check('123.123.123.123')); // Prints: true\nconsole.log(blockList.check('10.0.0.3')); // Prints: true\nconsole.log(blockList.check('222.111.111.222')); // Prints: false\n\n// IPv6 notation for IPv4 addresses works:\nconsole.log(blockList.check('::ffff:7b7b:7b7b', 'ipv6')); // Prints: true\nconsole.log(blockList.check('::ffff:123.123.123.123', 'ipv6')); // Prints: true\n</code></pre>" } ], "properties": [ { "textRaw": "`rules` Type: {string\\[]}", "type": "string\\[]", "name": "Type", "meta": { "added": [ "v15.0.0" ], "changes": [] }, "desc": "<p>The list of rules added to the blocklist.</p>" } ] }, { "textRaw": "Class: `net.SocketAddress`", "type": "class", "name": "net.SocketAddress", "meta": { "added": [ "v15.14.0" ], "changes": [] }, "properties": [ { "textRaw": "`address` Type {string}", "type": "string", "name": "Type", "meta": { "added": [ "v15.14.0" ], "changes": [] }, "desc": "Either `'ipv4'` or `'ipv6'`." }, { "textRaw": "`family` Type {string} Either `'ipv4'` or `'ipv6'`.", "type": "string", "name": "Type", "meta": { "added": [ "v15.14.0" ], "changes": [] }, "desc": "Either `'ipv4'` or `'ipv6'`." }, { "textRaw": "`flowlabel` Type {number}", "type": "number", "name": "Type", "meta": { "added": [ "v15.14.0" ], "changes": [] } }, { "textRaw": "`port` Type {number}", "type": "number", "name": "Type", "meta": { "added": [ "v15.14.0" ], "changes": [] } } ], "signatures": [ { "params": [ { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`address` {string} The network address as either an IPv4 or IPv6 string. **Default**: `'127.0.0.1'` if `family` is `'ipv4'`; `'::'` if `family` is `'ipv6'`.", "name": "address", "type": "string", "desc": "The network address as either an IPv4 or IPv6 string. **Default**: `'127.0.0.1'` if `family` is `'ipv4'`; `'::'` if `family` is `'ipv6'`." }, { "textRaw": "`family` {string} One of either `'ipv4'` or `'ipv6'`. **Default**: `'ipv4'`.", "name": "family", "type": "string", "desc": "One of either `'ipv4'` or `'ipv6'`. **Default**: `'ipv4'`." }, { "textRaw": "`flowlabel` {number} An IPv6 flow-label used only if `family` is `'ipv6'`.", "name": "flowlabel", "type": "number", "desc": "An IPv6 flow-label used only if `family` is `'ipv6'`." }, { "textRaw": "`port` {number} An IP port.", "name": "port", "type": "number", "desc": "An IP port." } ] } ] } ] }, { "textRaw": "Class: `net.Server`", "type": "class", "name": "net.Server", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "desc": "<ul>\n<li>Extends: <a href=\"events.html#class-eventemitter\" class=\"type\"><EventEmitter></a></li>\n</ul>\n<p>This class is used to create a TCP or <a href=\"#ipc-support\">IPC</a> server.</p>", "events": [ { "textRaw": "Event: `'close'`", "type": "event", "name": "close", "meta": { "added": [ "v0.5.0" ], "changes": [] }, "params": [], "desc": "<p>Emitted when the server closes. If connections exist, this\nevent is not emitted until all connections are ended.</p>" }, { "textRaw": "Event: `'connection'`", "type": "event", "name": "connection", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "params": [ { "textRaw": "{net.Socket} The connection object", "type": "net.Socket", "desc": "The connection object" } ], "desc": "<p>Emitted when a new connection is made. <code>socket</code> is an instance of\n<code>net.Socket</code>.</p>" }, { "textRaw": "Event: `'error'`", "type": "event", "name": "error", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "params": [ { "textRaw": "{Error}", "type": "Error" } ], "desc": "<p>Emitted when an error occurs. Unlike <a href=\"#class-netsocket\"><code>net.Socket</code></a>, the <a href=\"#event-close\"><code>'close'</code></a>\nevent will <strong>not</strong> be emitted directly following this event unless\n<a href=\"#serverclosecallback\"><code>server.close()</code></a> is manually called. See the example in discussion of\n<a href=\"#serverlisten\"><code>server.listen()</code></a>.</p>" }, { "textRaw": "Event: `'listening'`", "type": "event", "name": "listening", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "params": [], "desc": "<p>Emitted when the server has been bound after calling <a href=\"#serverlisten\"><code>server.listen()</code></a>.</p>" }, { "textRaw": "Event: `'drop'`", "type": "event", "name": "drop", "meta": { "added": [ "v16.17.0" ], "changes": [] }, "params": [], "desc": "<p>When the number of connections reaches the threshold of <code>server.maxConnections</code>,\nthe server will drop new connections and emit <code>'drop'</code> event instead. If it is a\nTCP server, the argument is as follows, otherwise the argument is <code>undefined</code>.</p>\n<ul>\n<li><code>data</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object\" class=\"type\"><Object></a> The argument passed to event listener.\n<ul>\n<li><code>localAddress</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\"><string></a> Local address.</li>\n<li><code>localPort</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\"><number></a> Local port.</li>\n<li><code>localFamily</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\"><string></a> Local family.</li>\n<li><code>remoteAddress</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\"><string></a> Remote address.</li>\n<li><code>remotePort</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\"><number></a> Remote port.</li>\n<li><code>remoteFamily</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\"><string></a> Remote IP family. <code>'IPv4'</code> or <code>'IPv6'</code>.</li>\n</ul>\n</li>\n</ul>" } ], "methods": [ { "textRaw": "`server.address()`", "type": "method", "name": "address", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Object|string|null}", "name": "return", "type": "Object|string|null" }, "params": [] } ], "desc": "<p>Returns the bound <code>address</code>, the address <code>family</code> name, and <code>port</code> of the server\nas reported by the operating system if listening on an IP socket\n(useful to find which port was assigned when getting an OS-assigned address):\n<code>{ port: 12346, family: 'IPv4', address: '127.0.0.1' }</code>.</p>\n<p>For a server listening on a pipe or Unix domain socket, the name is returned\nas a string.</p>\n<pre><code class=\"language-js\">const server = net.createServer((socket) => {\n socket.end('goodbye\\n');\n}).on('error', (err) => {\n // Handle errors here.\n throw err;\n});\n\n// Grab an arbitrary unused port.\nserver.listen(() => {\n console.log('opened server on', server.address());\n});\n</code></pre>\n<p><code>server.address()</code> returns <code>null</code> before the <code>'listening'</code> event has been\nemitted or after calling <code>server.close()</code>.</p>" }, { "textRaw": "`server.close([callback])`", "type": "method", "name": "close", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Server}", "name": "return", "type": "net.Server" }, "params": [ { "textRaw": "`callback` {Function} Called when the server is closed.", "name": "callback", "type": "Function", "desc": "Called when the server is closed." } ] } ], "desc": "<p>Stops the server from accepting new connections and keeps existing\nconnections. This function is asynchronous, the server is finally closed\nwhen all connections are ended and the server emits a <a href=\"#event-close\"><code>'close'</code></a> event.\nThe optional <code>callback</code> will be called once the <code>'close'</code> event occurs. Unlike\nthat event, it will be called with an <code>Error</code> as its only argument if the server\nwas not open when it was closed.</p>" }, { "textRaw": "`server.getConnections(callback)`", "type": "method", "name": "getConnections", "meta": { "added": [ "v0.9.7" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Server}", "name": "return", "type": "net.Server" }, "params": [ { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function" } ] } ], "desc": "<p>Asynchronously get the number of concurrent connections on the server. Works\nwhen sockets were sent to forks.</p>\n<p>Callback should take two arguments <code>err</code> and <code>count</code>.</p>" }, { "textRaw": "`server.listen()`", "type": "method", "name": "listen", "signatures": [ { "params": [] } ], "desc": "<p>Start a server listening for connections. A <code>net.Server</code> can be a TCP or\nan <a href=\"#ipc-support\">IPC</a> server depending on what it listens to.</p>\n<p>Possible signatures:</p>\n<ul>\n<li><a href=\"#serverlistenhandle-backlog-callback\"><code>server.listen(handle[, backlog][, callback])</code></a></li>\n<li><a href=\"#serverlistenoptions-callback\"><code>server.listen(options[, callback])</code></a></li>\n<li><a href=\"#serverlistenpath-backlog-callback\"><code>server.listen(path[, backlog][, callback])</code></a>\nfor <a href=\"#ipc-support\">IPC</a> servers</li>\n<li><a href=\"#serverlistenport-host-backlog-callback\"><code>server.listen([port[, host[, backlog]]][, callback])</code></a>\nfor TCP servers</li>\n</ul>\n<p>This function is asynchronous. When the server starts listening, the\n<a href=\"#event-listening\"><code>'listening'</code></a> event will be emitted. The last parameter <code>callback</code>\nwill be added as a listener for the <a href=\"#event-listening\"><code>'listening'</code></a> event.</p>\n<p>All <code>listen()</code> methods can take a <code>backlog</code> parameter to specify the maximum\nlength of the queue of pending connections. The actual length will be determined\nby the OS through sysctl settings such as <code>tcp_max_syn_backlog</code> and <code>somaxconn</code>\non Linux. The default value of this parameter is 511 (not 512).</p>\n<p>All <a href=\"#class-netsocket\"><code>net.Socket</code></a> are set to <code>SO_REUSEADDR</code> (see <a href=\"https://man7.org/linux/man-pages/man7/socket.7.html\"><code>socket(7)</code></a> for\ndetails).</p>\n<p>The <code>server.listen()</code> method can be called again if and only if there was an\nerror during the first <code>server.listen()</code> call or <code>server.close()</code> has been\ncalled. Otherwise, an <code>ERR_SERVER_ALREADY_LISTEN</code> error will be thrown.</p>\n<p>One of the most common errors raised when listening is <code>EADDRINUSE</code>.\nThis happens when another server is already listening on the requested\n<code>port</code>/<code>path</code>/<code>handle</code>. One way to handle this would be to retry\nafter a certain amount of time:</p>\n<pre><code class=\"language-js\">server.on('error', (e) => {\n if (e.code === 'EADDRINUSE') {\n console.log('Address in use, retrying...');\n setTimeout(() => {\n server.close();\n server.listen(PORT, HOST);\n }, 1000);\n }\n});\n</code></pre>", "methods": [ { "textRaw": "`server.listen(handle[, backlog][, callback])`", "type": "method", "name": "listen", "meta": { "added": [ "v0.5.10" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Server}", "name": "return", "type": "net.Server" }, "params": [ { "textRaw": "`handle` {Object}", "name": "handle", "type": "Object" }, { "textRaw": "`backlog` {number} Common parameter of [`server.listen()`][] functions", "name": "backlog", "type": "number", "desc": "Common parameter of [`server.listen()`][] functions" }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function" } ] } ], "desc": "<p>Start a server listening for connections on a given <code>handle</code> that has\nalready been bound to a port, a Unix domain socket, or a Windows named pipe.</p>\n<p>The <code>handle</code> object can be either a server, a socket (anything with an\nunderlying <code>_handle</code> member), or an object with an <code>fd</code> member that is a\nvalid file descriptor.</p>\n<p>Listening on a file descriptor is not supported on Windows.</p>" }, { "textRaw": "`server.listen(options[, callback])`", "type": "method", "name": "listen", "meta": { "added": [ "v0.11.14" ], "changes": [ { "version": "v15.6.0", "pr-url": "https://github.com/nodejs/node/pull/36623", "description": "AbortSignal support was added." }, { "version": "v11.4.0", "pr-url": "https://github.com/nodejs/node/pull/23798", "description": "The `ipv6Only` option is supported." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Server}", "name": "return", "type": "net.Server" }, "params": [ { "textRaw": "`options` {Object} Required. Supports the following properties:", "name": "options", "type": "Object", "desc": "Required. Supports the following properties:", "options": [ { "textRaw": "`port` {number}", "name": "port", "type": "number" }, { "textRaw": "`host` {string}", "name": "host", "type": "string" }, { "textRaw": "`path` {string} Will be ignored if `port` is specified. See [Identifying paths for IPC connections][].", "name": "path", "type": "string", "desc": "Will be ignored if `port` is specified. See [Identifying paths for IPC connections][]." }, { "textRaw": "`backlog` {number} Common parameter of [`server.listen()`][] functions.", "name": "backlog", "type": "number", "desc": "Common parameter of [`server.listen()`][] functions." }, { "textRaw": "`exclusive` {boolean} **Default:** `false`", "name": "exclusive", "type": "boolean", "default": "`false`" }, { "textRaw": "`readableAll` {boolean} For IPC servers makes the pipe readable for all users. **Default:** `false`.", "name": "readableAll", "type": "boolean", "default": "`false`", "desc": "For IPC servers makes the pipe readable for all users." }, { "textRaw": "`writableAll` {boolean} For IPC servers makes the pipe writable for all users. **Default:** `false`.", "name": "writableAll", "type": "boolean", "default": "`false`", "desc": "For IPC servers makes the pipe writable for all users." }, { "textRaw": "`ipv6Only` {boolean} For TCP servers, setting `ipv6Only` to `true` will disable dual-stack support, i.e., binding to host `::` won't make `0.0.0.0` be bound. **Default:** `false`.", "name": "ipv6Only", "type": "boolean", "default": "`false`", "desc": "For TCP servers, setting `ipv6Only` to `true` will disable dual-stack support, i.e., binding to host `::` won't make `0.0.0.0` be bound." }, { "textRaw": "`signal` {AbortSignal} An AbortSignal that may be used to close a listening server.", "name": "signal", "type": "AbortSignal", "desc": "An AbortSignal that may be used to close a listening server." } ] }, { "textRaw": "`callback` {Function} functions.", "name": "callback", "type": "Function", "desc": "functions." } ] } ], "desc": "<p>If <code>port</code> is specified, it behaves the same as\n<a href=\"#serverlistenport-host-backlog-callback\"><code>server.listen([port[, host[, backlog]]][, callback])</code></a>.\nOtherwise, if <code>path</code> is specified, it behaves the same as\n<a href=\"#serverlistenpath-backlog-callback\"><code>server.listen(path[, backlog][, callback])</code></a>.\nIf none of them is specified, an error will be thrown.</p>\n<p>If <code>exclusive</code> is <code>false</code> (default), then cluster workers will use the same\nunderlying handle, allowing connection handling duties to be shared. When\n<code>exclusive</code> is <code>true</code>, the handle is not shared, and attempted port sharing\nresults in an error. An example which listens on an exclusive port is\nshown below.</p>\n<pre><code class=\"language-js\">server.listen({\n host: 'localhost',\n port: 80,\n exclusive: true\n});\n</code></pre>\n<p>When <code>exclusive</code> is <code>true</code> and the underlying handle is shared, it is\npossible that several workers query a handle with different backlogs.\nIn this case, the first <code>backlog</code> passed to the master process will be used.</p>\n<p>Starting an IPC server as root may cause the server path to be inaccessible for\nunprivileged users. Using <code>readableAll</code> and <code>writableAll</code> will make the server\naccessible for all users.</p>\n<p>If the <code>signal</code> option is enabled, calling <code>.abort()</code> on the corresponding\n<code>AbortController</code> is similar to calling <code>.close()</code> on the server:</p>\n<pre><code class=\"language-js\">const controller = new AbortController();\nserver.listen({\n host: 'localhost',\n port: 80,\n signal: controller.signal\n});\n// Later, when you want to close the server.\ncontroller.abort();\n</code></pre>" }, { "textRaw": "`server.listen(path[, backlog][, callback])`", "type": "method", "name": "listen", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Server}", "name": "return", "type": "net.Server" }, "params": [ { "textRaw": "`path` {string} Path the server should listen to. See [Identifying paths for IPC connections][].", "name": "path", "type": "string", "desc": "Path the server should listen to. See [Identifying paths for IPC connections][]." }, { "textRaw": "`backlog` {number} Common parameter of [`server.listen()`][] functions.", "name": "backlog", "type": "number", "desc": "Common parameter of [`server.listen()`][] functions." }, { "textRaw": "`callback` {Function}.", "name": "callback", "type": "Function", "desc": "." } ] } ], "desc": "<p>Start an <a href=\"#ipc-support\">IPC</a> server listening for connections on the given <code>path</code>.</p>" }, { "textRaw": "`server.listen([port[, host[, backlog]]][, callback])`", "type": "method", "name": "listen", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Server}", "name": "return", "type": "net.Server" }, "params": [ { "textRaw": "`port` {number}", "name": "port", "type": "number" }, { "textRaw": "`host` {string}", "name": "host", "type": "string" }, { "textRaw": "`backlog` {number} Common parameter of [`server.listen()`][] functions.", "name": "backlog", "type": "number", "desc": "Common parameter of [`server.listen()`][] functions." }, { "textRaw": "`callback` {Function}.", "name": "callback", "type": "Function", "desc": "." } ] } ], "desc": "<p>Start a TCP server listening for connections on the given <code>port</code> and <code>host</code>.</p>\n<p>If <code>port</code> is omitted or is 0, the operating system will assign an arbitrary\nunused port, which can be retrieved by using <code>server.address().port</code>\nafter the <a href=\"#event-listening\"><code>'listening'</code></a> event has been emitted.</p>\n<p>If <code>host</code> is omitted, the server will accept connections on the\n<a href=\"https://en.wikipedia.org/wiki/IPv6_address#Unspecified_address\">unspecified IPv6 address</a> (<code>::</code>) when IPv6 is available, or the\n<a href=\"https://en.wikipedia.org/wiki/0.0.0.0\">unspecified IPv4 address</a> (<code>0.0.0.0</code>) otherwise.</p>\n<p>In most operating systems, listening to the <a href=\"https://en.wikipedia.org/wiki/IPv6_address#Unspecified_address\">unspecified IPv6 address</a> (<code>::</code>)\nmay cause the <code>net.Server</code> to also listen on the <a href=\"https://en.wikipedia.org/wiki/0.0.0.0\">unspecified IPv4 address</a>\n(<code>0.0.0.0</code>).</p>" } ] }, { "textRaw": "`server.ref()`", "type": "method", "name": "ref", "meta": { "added": [ "v0.9.1" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Server}", "name": "return", "type": "net.Server" }, "params": [] } ], "desc": "<p>Opposite of <code>unref()</code>, calling <code>ref()</code> on a previously <code>unref</code>ed server will\n<em>not</em> let the program exit if it's the only server left (the default behavior).\nIf the server is <code>ref</code>ed calling <code>ref()</code> again will have no effect.</p>" }, { "textRaw": "`server.unref()`", "type": "method", "name": "unref", "meta": { "added": [ "v0.9.1" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Server}", "name": "return", "type": "net.Server" }, "params": [] } ], "desc": "<p>Calling <code>unref()</code> on a server will allow the program to exit if this is the only\nactive server in the event system. If the server is already <code>unref</code>ed calling\n<code>unref()</code> again will have no effect.</p>" } ], "properties": [ { "textRaw": "`listening` {boolean} Indicates whether or not the server is listening for connections.", "type": "boolean", "name": "listening", "meta": { "added": [ "v5.7.0" ], "changes": [] }, "desc": "Indicates whether or not the server is listening for connections." }, { "textRaw": "`maxConnections` {integer}", "type": "integer", "name": "maxConnections", "meta": { "added": [ "v0.2.0" ], "changes": [] }, "desc": "<p>Set this property to reject connections when the server's connection count gets\nhigh.</p>\n<p>It is not recommended to use this option once a socket has been sent to a child\nwith <a href=\"child_process.html#child_processforkmodulepath-args-options\"><code>child_process.fork()</code></a>.</p>" } ], "signatures": [ { "return": { "textRaw": "Returns: {net.Server}", "name": "return", "type": "net.Server" }, "params": [ { "textRaw": "`options` {Object} See [`net.createServer([options][, connectionListener])`][`net.createServer()`].", "name": "options", "type": "Object", "desc": "See [`net.createServer([options][, connectionListener])`][`net.createServer()`]." }, { "textRaw": "`connectionListener` {Function} Automatically set as a listener for the [`'connection'`][] event.", "name": "connectionListener", "type": "Function", "desc": "Automatically set as a listener for the [`'connection'`][] event." } ], "desc": "<p><code>net.Server</code> is an <a href=\"events.html#class-eventemitter\"><code>EventEmitter</code></a> with the following events:</p>" } ] }, { "textRaw": "Class: `net.Socket`", "type": "class", "name": "net.Socket", "meta": { "added": [ "v0.3.4" ], "changes": [] }, "desc": "<ul>\n<li>Extends: <a href=\"stream.html#class-streamduplex\" class=\"type\"><stream.Duplex></a></li>\n</ul>\n<p>This class is an abstraction of a TCP socket or a streaming <a href=\"#ipc-support\">IPC</a> endpoint\n(uses named pipes on Windows, and Unix domain sockets otherwise). It is also\nan <a href=\"events.html#class-eventemitter\"><code>EventEmitter</code></a>.</p>\n<p>A <code>net.Socket</code> can be created by the user and used directly to interact with\na server. For example, it is returned by <a href=\"#netcreateconnection\"><code>net.createConnection()</code></a>,\nso the user can use it to talk to the server.</p>\n<p>It can also be created by Node.js and passed to the user when a connection\nis received. For example, it is passed to the listeners of a\n<a href=\"#event-connection\"><code>'connection'</code></a> event emitted on a <a href=\"#class-netserver\"><code>net.Server</code></a>, so the user can use\nit to interact with the client.</p>", "events": [ { "textRaw": "Event: `'close'`", "type": "event", "name": "close", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "params": [ { "textRaw": "`hadError` {boolean} `true` if the socket had a transmission error.", "name": "hadError", "type": "boolean", "desc": "`true` if the socket had a transmission error." } ], "desc": "<p>Emitted once the socket is fully closed. The argument <code>hadError</code> is a boolean\nwhich says if the socket was closed due to a transmission error.</p>" }, { "textRaw": "Event: `'connect'`", "type": "event", "name": "connect", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "params": [], "desc": "<p>Emitted when a socket connection is successfully established.\nSee <a href=\"#netcreateconnection\"><code>net.createConnection()</code></a>.</p>" }, { "textRaw": "Event: `'data'`", "type": "event", "name": "data", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "params": [ { "textRaw": "{Buffer|string}", "type": "Buffer|string" } ], "desc": "<p>Emitted when data is received. The argument <code>data</code> will be a <code>Buffer</code> or\n<code>String</code>. Encoding of data is set by <a href=\"#socketsetencodingencoding\"><code>socket.setEncoding()</code></a>.</p>\n<p>The data will be lost if there is no listener when a <code>Socket</code>\nemits a <code>'data'</code> event.</p>" }, { "textRaw": "Event: `'drain'`", "type": "event", "name": "drain", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "params": [], "desc": "<p>Emitted when the write buffer becomes empty. Can be used to throttle uploads.</p>\n<p>See also: the return values of <code>socket.write()</code>.</p>" }, { "textRaw": "Event: `'end'`", "type": "event", "name": "end", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "params": [], "desc": "<p>Emitted when the other end of the socket signals the end of transmission, thus\nending the readable side of the socket.</p>\n<p>By default (<code>allowHalfOpen</code> is <code>false</code>) the socket will send an end of\ntransmission packet back and destroy its file descriptor once it has written out\nits pending write queue. However, if <code>allowHalfOpen</code> is set to <code>true</code>, the\nsocket will not automatically <a href=\"#socketenddata-encoding-callback\"><code>end()</code></a> its writable side,\nallowing the user to write arbitrary amounts of data. The user must call\n<a href=\"#socketenddata-encoding-callback\"><code>end()</code></a> explicitly to close the connection (i.e. sending a\nFIN packet back).</p>" }, { "textRaw": "Event: `'error'`", "type": "event", "name": "error", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "params": [ { "textRaw": "{Error}", "type": "Error" } ], "desc": "<p>Emitted when an error occurs. The <code>'close'</code> event will be called directly\nfollowing this event.</p>" }, { "textRaw": "Event: `'lookup'`", "type": "event", "name": "lookup", "meta": { "added": [ "v0.11.3" ], "changes": [ { "version": "v5.10.0", "pr-url": "https://github.com/nodejs/node/pull/5598", "description": "The `host` parameter is supported now." } ] }, "params": [], "desc": "<p>Emitted after resolving the host name but before connecting.\nNot applicable to Unix sockets.</p>\n<ul>\n<li><code>err</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error\" class=\"type\"><Error></a> | <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type\" class=\"type\"><null></a> The error object. See <a href=\"dns.html#dnslookuphostname-options-callback\"><code>dns.lookup()</code></a>.</li>\n<li><code>address</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\"><string></a> The IP address.</li>\n<li><code>family</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\"><string></a> | <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type\" class=\"type\"><null></a> The address type. See <a href=\"dns.html#dnslookuphostname-options-callback\"><code>dns.lookup()</code></a>.</li>\n<li><code>host</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\"><string></a> The host name.</li>\n</ul>" }, { "textRaw": "Event: `'ready'`", "type": "event", "name": "ready", "meta": { "added": [ "v9.11.0" ], "changes": [] }, "params": [], "desc": "<p>Emitted when a socket is ready to be used.</p>\n<p>Triggered immediately after <code>'connect'</code>.</p>" }, { "textRaw": "Event: `'timeout'`", "type": "event", "name": "timeout", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "params": [], "desc": "<p>Emitted if the socket times out from inactivity. This is only to notify that\nthe socket has been idle. The user must manually close the connection.</p>\n<p>See also: <a href=\"#socketsettimeouttimeout-callback\"><code>socket.setTimeout()</code></a>.</p>" } ], "methods": [ { "textRaw": "`socket.address()`", "type": "method", "name": "address", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Object}", "name": "return", "type": "Object" }, "params": [] } ], "desc": "<p>Returns the bound <code>address</code>, the address <code>family</code> name and <code>port</code> of the\nsocket as reported by the operating system:\n<code>{ port: 12346, family: 'IPv4', address: '127.0.0.1' }</code></p>" }, { "textRaw": "`socket.connect()`", "type": "method", "name": "connect", "signatures": [ { "params": [] } ], "desc": "<p>Initiate a connection on a given socket.</p>\n<p>Possible signatures:</p>\n<ul>\n<li><a href=\"#socketconnectoptions-connectlistener\"><code>socket.connect(options[, connectListener])</code></a></li>\n<li><a href=\"#socketconnectpath-connectlistener\"><code>socket.connect(path[, connectListener])</code></a>\nfor <a href=\"#ipc-support\">IPC</a> connections.</li>\n<li><a href=\"#socketconnectport-host-connectlistener\"><code>socket.connect(port[, host][, connectListener])</code></a>\nfor TCP connections.</li>\n<li>Returns: <a href=\"net.html#class-netsocket\" class=\"type\"><net.Socket></a> The socket itself.</li>\n</ul>\n<p>This function is asynchronous. When the connection is established, the\n<a href=\"#event-connect\"><code>'connect'</code></a> event will be emitted. If there is a problem connecting,\ninstead of a <a href=\"#event-connect\"><code>'connect'</code></a> event, an <a href=\"#event-error_1\"><code>'error'</code></a> event will be emitted with\nthe error passed to the <a href=\"#event-error_1\"><code>'error'</code></a> listener.\nThe last parameter <code>connectListener</code>, if supplied, will be added as a listener\nfor the <a href=\"#event-connect\"><code>'connect'</code></a> event <strong>once</strong>.</p>\n<p>This function should only be used for reconnecting a socket after\n<code>'close'</code> has been emitted or otherwise it may lead to undefined\nbehavior.</p>", "methods": [ { "textRaw": "`socket.connect(options[, connectListener])`", "type": "method", "name": "connect", "meta": { "added": [ "v0.1.90" ], "changes": [ { "version": "v16.15.0", "pr-url": "https://github.com/nodejs/node/pull/41310", "description": "The `noDelay`, `keepAlive`, and `keepAliveInitialDelay` options are supported now." }, { "version": "v12.10.0", "pr-url": "https://github.com/nodejs/node/pull/25436", "description": "Added `onread` option." }, { "version": "v6.0.0", "pr-url": "https://github.com/nodejs/node/pull/6021", "description": "The `hints` option defaults to `0` in all cases now. Previously, in the absence of the `family` option it would default to `dns.ADDRCONFIG | dns.V4MAPPED`." }, { "version": "v5.11.0", "pr-url": "https://github.com/nodejs/node/pull/6000", "description": "The `hints` option is supported now." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The socket itself.", "name": "return", "type": "net.Socket", "desc": "The socket itself." }, "params": [ { "textRaw": "`options` {Object}", "name": "options", "type": "Object" }, { "textRaw": "`connectListener` {Function} Common parameter of [`socket.connect()`][] methods. Will be added as a listener for the [`'connect'`][] event once.", "name": "connectListener", "type": "Function", "desc": "Common parameter of [`socket.connect()`][] methods. Will be added as a listener for the [`'connect'`][] event once." } ] } ], "desc": "<p>Initiate a connection on a given socket. Normally this method is not needed,\nthe socket should be created and opened with <a href=\"#netcreateconnection\"><code>net.createConnection()</code></a>. Use\nthis only when implementing a custom Socket.</p>\n<p>For TCP connections, available <code>options</code> are:</p>\n<ul>\n<li><code>port</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\"><number></a> Required. Port the socket should connect to.</li>\n<li><code>host</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\"><string></a> Host the socket should connect to. <strong>Default:</strong> <code>'localhost'</code>.</li>\n<li><code>localAddress</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\"><string></a> Local address the socket should connect from.</li>\n<li><code>localPort</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\"><number></a> Local port the socket should connect from.</li>\n<li><code>family</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\"><number></a>: Version of IP stack. Must be <code>4</code>, <code>6</code>, or <code>0</code>. The value\n<code>0</code> indicates that both IPv4 and IPv6 addresses are allowed. <strong>Default:</strong> <code>0</code>.</li>\n<li><code>hints</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\"><number></a> Optional <a href=\"dns.html#supported-getaddrinfo-flags\"><code>dns.lookup()</code> hints</a>.</li>\n<li><code>lookup</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function\" class=\"type\"><Function></a> Custom lookup function. <strong>Default:</strong> <a href=\"dns.html#dnslookuphostname-options-callback\"><code>dns.lookup()</code></a>.</li>\n<li><code>noDelay</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type\" class=\"type\"><boolean></a> If set to <code>true</code>, it disables the use of Nagle's algorithm immediately\nafter the socket is established. <strong>Default:</strong> <code>false</code>.</li>\n<li><code>keepAlive</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type\" class=\"type\"><boolean></a> If set to <code>true</code>, it enables keep-alive functionality on the socket\nimmediately after the connection is established, similarly on what is done in\n<a href=\"#socketsetkeepaliveenable-initialdelay\"><code>socket.setKeepAlive([enable][, initialDelay])</code></a>.\n<strong>Default:</strong> <code>false</code>.</li>\n<li><code>keepAliveInitialDelay</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\"><number></a> If set to a positive number, it sets the initial delay before\nthe first keepalive probe is sent on an idle socket.<strong>Default:</strong> <code>0</code>.</li>\n</ul>\n<p>For <a href=\"#ipc-support\">IPC</a> connections, available <code>options</code> are:</p>\n<ul>\n<li><code>path</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\"><string></a> Required. Path the client should connect to.\nSee <a href=\"#identifying-paths-for-ipc-connections\">Identifying paths for IPC connections</a>. If provided, the TCP-specific\noptions above are ignored.</li>\n</ul>\n<p>For both types, available <code>options</code> include:</p>\n<ul>\n<li><code>onread</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object\" class=\"type\"><Object></a> If specified, incoming data is stored in a single <code>buffer</code>\nand passed to the supplied <code>callback</code> when data arrives on the socket.\nThis will cause the streaming functionality to not provide any data.\nThe socket will emit events like <code>'error'</code>, <code>'end'</code>, and <code>'close'</code>\nas usual. Methods like <code>pause()</code> and <code>resume()</code> will also behave as\nexpected.\n<ul>\n<li><code>buffer</code> <a href=\"buffer.html#class-buffer\" class=\"type\"><Buffer></a> | <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array\" class=\"type\"><Uint8Array></a> | <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function\" class=\"type\"><Function></a> Either a reusable chunk of memory to\nuse for storing incoming data or a function that returns such.</li>\n<li><code>callback</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function\" class=\"type\"><Function></a> This function is called for every chunk of incoming\ndata. Two arguments are passed to it: the number of bytes written to\n<code>buffer</code> and a reference to <code>buffer</code>. Return <code>false</code> from this function to\nimplicitly <code>pause()</code> the socket. This function will be executed in the\nglobal context.</li>\n</ul>\n</li>\n</ul>\n<p>Following is an example of a client using the <code>onread</code> option:</p>\n<pre><code class=\"language-js\">const net = require('node:net');\nnet.connect({\n port: 80,\n onread: {\n // Reuses a 4KiB Buffer for every read from the socket.\n buffer: Buffer.alloc(4 * 1024),\n callback: function(nread, buf) {\n // Received data is available in `buf` from 0 to `nread`.\n console.log(buf.toString('utf8', 0, nread));\n }\n }\n});\n</code></pre>" }, { "textRaw": "`socket.connect(path[, connectListener])`", "type": "method", "name": "connect", "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The socket itself.", "name": "return", "type": "net.Socket", "desc": "The socket itself." }, "params": [ { "textRaw": "`path` {string} Path the client should connect to. See [Identifying paths for IPC connections][].", "name": "path", "type": "string", "desc": "Path the client should connect to. See [Identifying paths for IPC connections][]." }, { "textRaw": "`connectListener` {Function} Common parameter of [`socket.connect()`][] methods. Will be added as a listener for the [`'connect'`][] event once.", "name": "connectListener", "type": "Function", "desc": "Common parameter of [`socket.connect()`][] methods. Will be added as a listener for the [`'connect'`][] event once." } ] } ], "desc": "<p>Initiate an <a href=\"#ipc-support\">IPC</a> connection on the given socket.</p>\n<p>Alias to\n<a href=\"#socketconnectoptions-connectlistener\"><code>socket.connect(options[, connectListener])</code></a>\ncalled with <code>{ path: path }</code> as <code>options</code>.</p>" }, { "textRaw": "`socket.connect(port[, host][, connectListener])`", "type": "method", "name": "connect", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The socket itself.", "name": "return", "type": "net.Socket", "desc": "The socket itself." }, "params": [ { "textRaw": "`port` {number} Port the client should connect to.", "name": "port", "type": "number", "desc": "Port the client should connect to." }, { "textRaw": "`host` {string} Host the client should connect to.", "name": "host", "type": "string", "desc": "Host the client should connect to." }, { "textRaw": "`connectListener` {Function} Common parameter of [`socket.connect()`][] methods. Will be added as a listener for the [`'connect'`][] event once.", "name": "connectListener", "type": "Function", "desc": "Common parameter of [`socket.connect()`][] methods. Will be added as a listener for the [`'connect'`][] event once." } ] } ], "desc": "<p>Initiate a TCP connection on the given socket.</p>\n<p>Alias to\n<a href=\"#socketconnectoptions-connectlistener\"><code>socket.connect(options[, connectListener])</code></a>\ncalled with <code>{port: port, host: host}</code> as <code>options</code>.</p>" } ] }, { "textRaw": "`socket.destroy([error])`", "type": "method", "name": "destroy", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket}", "name": "return", "type": "net.Socket" }, "params": [ { "textRaw": "`error` {Object}", "name": "error", "type": "Object" } ] } ], "desc": "<p>Ensures that no more I/O activity happens on this socket.\nDestroys the stream and closes the connection.</p>\n<p>See <a href=\"stream.html#writabledestroyerror\"><code>writable.destroy()</code></a> for further details.</p>" }, { "textRaw": "`socket.end([data[, encoding]][, callback])`", "type": "method", "name": "end", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The socket itself.", "name": "return", "type": "net.Socket", "desc": "The socket itself." }, "params": [ { "textRaw": "`data` {string|Buffer|Uint8Array}", "name": "data", "type": "string|Buffer|Uint8Array" }, { "textRaw": "`encoding` {string} Only used when data is `string`. **Default:** `'utf8'`.", "name": "encoding", "type": "string", "default": "`'utf8'`", "desc": "Only used when data is `string`." }, { "textRaw": "`callback` {Function} Optional callback for when the socket is finished.", "name": "callback", "type": "Function", "desc": "Optional callback for when the socket is finished." } ] } ], "desc": "<p>Half-closes the socket. i.e., it sends a FIN packet. It is possible the\nserver will still send some data.</p>\n<p>See <a href=\"stream.html#writableendchunk-encoding-callback\"><code>writable.end()</code></a> for further details.</p>" }, { "textRaw": "`socket.pause()`", "type": "method", "name": "pause", "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The socket itself.", "name": "return", "type": "net.Socket", "desc": "The socket itself." }, "params": [] } ], "desc": "<p>Pauses the reading of data. That is, <a href=\"#event-data\"><code>'data'</code></a> events will not be emitted.\nUseful to throttle back an upload.</p>" }, { "textRaw": "`socket.ref()`", "type": "method", "name": "ref", "meta": { "added": [ "v0.9.1" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The socket itself.", "name": "return", "type": "net.Socket", "desc": "The socket itself." }, "params": [] } ], "desc": "<p>Opposite of <code>unref()</code>, calling <code>ref()</code> on a previously <code>unref</code>ed socket will\n<em>not</em> let the program exit if it's the only socket left (the default behavior).\nIf the socket is <code>ref</code>ed calling <code>ref</code> again will have no effect.</p>" }, { "textRaw": "`socket.resetAndDestroy()`", "type": "method", "name": "resetAndDestroy", "meta": { "added": [ "v16.17.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket}", "name": "return", "type": "net.Socket" }, "params": [] } ], "desc": "<p>Close the TCP connection by sending an RST packet and destroy the stream.\nIf this TCP socket is in connecting status, it will send an RST packet and destroy this TCP socket once it is connected.\nOtherwise, it will call <code>socket.destroy</code> with an <code>ERR_SOCKET_CLOSED</code> Error.\nIf this is not a TCP socket (for example, a pipe), calling this method will immediately throw an <code>ERR_INVALID_HANDLE_TYPE</code> Error.</p>" }, { "textRaw": "`socket.resume()`", "type": "method", "name": "resume", "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The socket itself.", "name": "return", "type": "net.Socket", "desc": "The socket itself." }, "params": [] } ], "desc": "<p>Resumes reading after a call to <a href=\"#socketpause\"><code>socket.pause()</code></a>.</p>" }, { "textRaw": "`socket.setEncoding([encoding])`", "type": "method", "name": "setEncoding", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The socket itself.", "name": "return", "type": "net.Socket", "desc": "The socket itself." }, "params": [ { "textRaw": "`encoding` {string}", "name": "encoding", "type": "string" } ] } ], "desc": "<p>Set the encoding for the socket as a <a href=\"stream.html#class-streamreadable\">Readable Stream</a>. See\n<a href=\"stream.html#readablesetencodingencoding\"><code>readable.setEncoding()</code></a> for more information.</p>" }, { "textRaw": "`socket.setKeepAlive([enable][, initialDelay])`", "type": "method", "name": "setKeepAlive", "meta": { "added": [ "v0.1.92" ], "changes": [ { "version": [ "v13.12.0", "v12.17.0" ], "pr-url": "https://github.com/nodejs/node/pull/32204", "description": "New defaults for `TCP_KEEPCNT` and `TCP_KEEPINTVL` socket options were added." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The socket itself.", "name": "return", "type": "net.Socket", "desc": "The socket itself." }, "params": [ { "textRaw": "`enable` {boolean} **Default:** `false`", "name": "enable", "type": "boolean", "default": "`false`" }, { "textRaw": "`initialDelay` {number} **Default:** `0`", "name": "initialDelay", "type": "number", "default": "`0`" } ] } ], "desc": "<p>Enable/disable keep-alive functionality, and optionally set the initial\ndelay before the first keepalive probe is sent on an idle socket.</p>\n<p>Set <code>initialDelay</code> (in milliseconds) to set the delay between the last\ndata packet received and the first keepalive probe. Setting <code>0</code> for\n<code>initialDelay</code> will leave the value unchanged from the default\n(or previous) setting.</p>\n<p>Enabling the keep-alive functionality will set the following socket options:</p>\n<ul>\n<li><code>SO_KEEPALIVE=1</code></li>\n<li><code>TCP_KEEPIDLE=initialDelay</code></li>\n<li><code>TCP_KEEPCNT=10</code></li>\n<li><code>TCP_KEEPINTVL=1</code></li>\n</ul>" }, { "textRaw": "`socket.setNoDelay([noDelay])`", "type": "method", "name": "setNoDelay", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The socket itself.", "name": "return", "type": "net.Socket", "desc": "The socket itself." }, "params": [ { "textRaw": "`noDelay` {boolean} **Default:** `true`", "name": "noDelay", "type": "boolean", "default": "`true`" } ] } ], "desc": "<p>Enable/disable the use of Nagle's algorithm.</p>\n<p>When a TCP connection is created, it will have Nagle's algorithm enabled.</p>\n<p>Nagle's algorithm delays data before it is sent via the network. It attempts\nto optimize throughput at the expense of latency.</p>\n<p>Passing <code>true</code> for <code>noDelay</code> or not passing an argument will disable Nagle's\nalgorithm for the socket. Passing <code>false</code> for <code>noDelay</code> will enable Nagle's\nalgorithm.</p>" }, { "textRaw": "`socket.setTimeout(timeout[, callback])`", "type": "method", "name": "setTimeout", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The socket itself.", "name": "return", "type": "net.Socket", "desc": "The socket itself." }, "params": [ { "textRaw": "`timeout` {number}", "name": "timeout", "type": "number" }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function" } ] } ], "desc": "<p>Sets the socket to timeout after <code>timeout</code> milliseconds of inactivity on\nthe socket. By default <code>net.Socket</code> do not have a timeout.</p>\n<p>When an idle timeout is triggered the socket will receive a <a href=\"#event-timeout\"><code>'timeout'</code></a>\nevent but the connection will not be severed. The user must manually call\n<a href=\"#socketenddata-encoding-callback\"><code>socket.end()</code></a> or <a href=\"#socketdestroyerror\"><code>socket.destroy()</code></a> to end the connection.</p>\n<pre><code class=\"language-js\">socket.setTimeout(3000);\nsocket.on('timeout', () => {\n console.log('socket timeout');\n socket.end();\n});\n</code></pre>\n<p>If <code>timeout</code> is 0, then the existing idle timeout is disabled.</p>\n<p>The optional <code>callback</code> parameter will be added as a one-time listener for the\n<a href=\"#event-timeout\"><code>'timeout'</code></a> event.</p>" }, { "textRaw": "`socket.unref()`", "type": "method", "name": "unref", "meta": { "added": [ "v0.9.1" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The socket itself.", "name": "return", "type": "net.Socket", "desc": "The socket itself." }, "params": [] } ], "desc": "<p>Calling <code>unref()</code> on a socket will allow the program to exit if this is the only\nactive socket in the event system. If the socket is already <code>unref</code>ed calling\n<code>unref()</code> again will have no effect.</p>" }, { "textRaw": "`socket.write(data[, encoding][, callback])`", "type": "method", "name": "write", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [ { "textRaw": "`data` {string|Buffer|Uint8Array}", "name": "data", "type": "string|Buffer|Uint8Array" }, { "textRaw": "`encoding` {string} Only used when data is `string`. **Default:** `utf8`.", "name": "encoding", "type": "string", "default": "`utf8`", "desc": "Only used when data is `string`." }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function" } ] } ], "desc": "<p>Sends data on the socket. The second parameter specifies the encoding in the\ncase of a string. It defaults to UTF8 encoding.</p>\n<p>Returns <code>true</code> if the entire data was flushed successfully to the kernel\nbuffer. Returns <code>false</code> if all or part of the data was queued in user memory.\n<a href=\"#event-drain\"><code>'drain'</code></a> will be emitted when the buffer is again free.</p>\n<p>The optional <code>callback</code> parameter will be executed when the data is finally\nwritten out, which may not be immediately.</p>\n<p>See <code>Writable</code> stream <a href=\"stream.html#writablewritechunk-encoding-callback\"><code>write()</code></a> method for more\ninformation.</p>" } ], "properties": [ { "textRaw": "`bufferSize` {integer}", "type": "integer", "name": "bufferSize", "meta": { "added": [ "v0.3.8" ], "deprecated": [ "v14.6.0" ], "changes": [] }, "stability": 0, "stabilityText": "Deprecated: Use [`writable.writableLength`][] instead.", "desc": "<p>This property shows the number of characters buffered for writing. The buffer\nmay contain strings whose length after encoding is not yet known. So this number\nis only an approximation of the number of bytes in the buffer.</p>\n<p><code>net.Socket</code> has the property that <code>socket.write()</code> always works. This is to\nhelp users get up and running quickly. The computer cannot always keep up\nwith the amount of data that is written to a socket. The network connection\nsimply might be too slow. Node.js will internally queue up the data written to a\nsocket and send it out over the wire when it is possible.</p>\n<p>The consequence of this internal buffering is that memory may grow.\nUsers who experience large or growing <code>bufferSize</code> should attempt to\n\"throttle\" the data flows in their program with\n<a href=\"#socketpause\"><code>socket.pause()</code></a> and <a href=\"#socketresume\"><code>socket.resume()</code></a>.</p>" }, { "textRaw": "`bytesRead` {integer}", "type": "integer", "name": "bytesRead", "meta": { "added": [ "v0.5.3" ], "changes": [] }, "desc": "<p>The amount of received bytes.</p>" }, { "textRaw": "`bytesWritten` {integer}", "type": "integer", "name": "bytesWritten", "meta": { "added": [ "v0.5.3" ], "changes": [] }, "desc": "<p>The amount of bytes sent.</p>" }, { "textRaw": "`connecting` {boolean}", "type": "boolean", "name": "connecting", "meta": { "added": [ "v6.1.0" ], "changes": [] }, "desc": "<p>If <code>true</code>,\n<a href=\"#socketconnectoptions-connectlistener\"><code>socket.connect(options[, connectListener])</code></a> was\ncalled and has not yet finished. It will stay <code>true</code> until the socket becomes\nconnected, then it is set to <code>false</code> and the <code>'connect'</code> event is emitted. Note\nthat the\n<a href=\"#socketconnectoptions-connectlistener\"><code>socket.connect(options[, connectListener])</code></a>\ncallback is a listener for the <code>'connect'</code> event.</p>" }, { "textRaw": "`destroyed` {boolean} Indicates if the connection is destroyed or not. Once a connection is destroyed no further data can be transferred using it.", "type": "boolean", "name": "destroyed", "desc": "<p>See <a href=\"stream.html#writabledestroyed\"><code>writable.destroyed</code></a> for further details.</p>", "shortDesc": "Indicates if the connection is destroyed or not. Once a connection is destroyed no further data can be transferred using it." }, { "textRaw": "`localAddress` {string}", "type": "string", "name": "localAddress", "meta": { "added": [ "v0.9.6" ], "changes": [] }, "desc": "<p>The string representation of the local IP address the remote client is\nconnecting on. For example, in a server listening on <code>'0.0.0.0'</code>, if a client\nconnects on <code>'192.168.1.1'</code>, the value of <code>socket.localAddress</code> would be\n<code>'192.168.1.1'</code>.</p>" }, { "textRaw": "`localPort` {integer}", "type": "integer", "name": "localPort", "meta": { "added": [ "v0.9.6" ], "changes": [] }, "desc": "<p>The numeric representation of the local port. For example, <code>80</code> or <code>21</code>.</p>" }, { "textRaw": "`localFamily` {string}", "type": "string", "name": "localFamily", "meta": { "added": [ "v16.18.0" ], "changes": [] }, "desc": "<p>The string representation of the local IP family. <code>'IPv4'</code> or <code>'IPv6'</code>.</p>" }, { "textRaw": "`pending` {boolean}", "type": "boolean", "name": "pending", "meta": { "added": [ "v11.2.0", "v10.16.0" ], "changes": [] }, "desc": "<p>This is <code>true</code> if the socket is not connected yet, either because <code>.connect()</code>\nhas not yet been called or because it is still in the process of connecting\n(see <a href=\"#socketconnecting\"><code>socket.connecting</code></a>).</p>" }, { "textRaw": "`remoteAddress` {string}", "type": "string", "name": "remoteAddress", "meta": { "added": [ "v0.5.10" ], "changes": [] }, "desc": "<p>The string representation of the remote IP address. For example,\n<code>'74.125.127.100'</code> or <code>'2001:4860:a005::68'</code>. Value may be <code>undefined</code> if\nthe socket is destroyed (for example, if the client disconnected).</p>" }, { "textRaw": "`remoteFamily` {string}", "type": "string", "name": "remoteFamily", "meta": { "added": [ "v0.11.14" ], "changes": [] }, "desc": "<p>The string representation of the remote IP family. <code>'IPv4'</code> or <code>'IPv6'</code>.</p>" }, { "textRaw": "`remotePort` {integer}", "type": "integer", "name": "remotePort", "meta": { "added": [ "v0.5.10" ], "changes": [] }, "desc": "<p>The numeric representation of the remote port. For example, <code>80</code> or <code>21</code>.</p>" }, { "textRaw": "`timeout` {number|undefined}", "type": "number|undefined", "name": "timeout", "meta": { "added": [ "v10.7.0" ], "changes": [] }, "desc": "<p>The socket timeout in milliseconds as set by <a href=\"#socketsettimeouttimeout-callback\"><code>socket.setTimeout()</code></a>.\nIt is <code>undefined</code> if a timeout has not been set.</p>" }, { "textRaw": "`readyState` {string}", "type": "string", "name": "readyState", "meta": { "added": [ "v0.5.0" ], "changes": [] }, "desc": "<p>This property represents the state of the connection as a string.</p>\n<ul>\n<li>If the stream is connecting <code>socket.readyState</code> is <code>opening</code>.</li>\n<li>If the stream is readable and writable, it is <code>open</code>.</li>\n<li>If the stream is readable and not writable, it is <code>readOnly</code>.</li>\n<li>If the stream is not readable and writable, it is <code>writeOnly</code>.</li>\n</ul>" } ], "signatures": [ { "return": { "textRaw": "Returns: {net.Socket}", "name": "return", "type": "net.Socket" }, "params": [ { "textRaw": "`options` {Object} Available options are:", "name": "options", "type": "Object", "desc": "Available options are:", "options": [ { "textRaw": "`fd` {number} If specified, wrap around an existing socket with the given file descriptor, otherwise a new socket will be created.", "name": "fd", "type": "number", "desc": "If specified, wrap around an existing socket with the given file descriptor, otherwise a new socket will be created." }, { "textRaw": "`allowHalfOpen` {boolean} If set to `false`, then the socket will automatically end the writable side when the readable side ends. See [`net.createServer()`][] and the [`'end'`][] event for details. **Default:** `false`.", "name": "allowHalfOpen", "type": "boolean", "default": "`false`", "desc": "If set to `false`, then the socket will automatically end the writable side when the readable side ends. See [`net.createServer()`][] and the [`'end'`][] event for details." }, { "textRaw": "`readable` {boolean} Allow reads on the socket when an `fd` is passed, otherwise ignored. **Default:** `false`.", "name": "readable", "type": "boolean", "default": "`false`", "desc": "Allow reads on the socket when an `fd` is passed, otherwise ignored." }, { "textRaw": "`writable` {boolean} Allow writes on the socket when an `fd` is passed, otherwise ignored. **Default:** `false`.", "name": "writable", "type": "boolean", "default": "`false`", "desc": "Allow writes on the socket when an `fd` is passed, otherwise ignored." }, { "textRaw": "`signal` {AbortSignal} An Abort signal that may be used to destroy the socket.", "name": "signal", "type": "AbortSignal", "desc": "An Abort signal that may be used to destroy the socket." } ] } ], "desc": "<p>Creates a new socket object.</p>\n<p>The newly created socket can be either a TCP socket or a streaming <a href=\"#ipc-support\">IPC</a>\nendpoint, depending on what it <a href=\"#socketconnect\"><code>connect()</code></a> to.</p>" } ] } ], "methods": [ { "textRaw": "`net.connect()`", "type": "method", "name": "connect", "signatures": [ { "params": [] } ], "desc": "<p>Aliases to\n<a href=\"#netcreateconnection\"><code>net.createConnection()</code></a>.</p>\n<p>Possible signatures:</p>\n<ul>\n<li><a href=\"#netconnectoptions-connectlistener\"><code>net.connect(options[, connectListener])</code></a></li>\n<li><a href=\"#netconnectpath-connectlistener\"><code>net.connect(path[, connectListener])</code></a> for <a href=\"#ipc-support\">IPC</a>\nconnections.</li>\n<li><a href=\"#netconnectport-host-connectlistener\"><code>net.connect(port[, host][, connectListener])</code></a>\nfor TCP connections.</li>\n</ul>", "methods": [ { "textRaw": "`net.connect(options[, connectListener])`", "type": "method", "name": "connect", "meta": { "added": [ "v0.7.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket}", "name": "return", "type": "net.Socket" }, "params": [ { "textRaw": "`options` {Object}", "name": "options", "type": "Object" }, { "textRaw": "`connectListener` {Function}", "name": "connectListener", "type": "Function" } ] } ], "desc": "<p>Alias to\n<a href=\"#netcreateconnectionoptions-connectlistener\"><code>net.createConnection(options[, connectListener])</code></a>.</p>" }, { "textRaw": "`net.connect(path[, connectListener])`", "type": "method", "name": "connect", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket}", "name": "return", "type": "net.Socket" }, "params": [ { "textRaw": "`path` {string}", "name": "path", "type": "string" }, { "textRaw": "`connectListener` {Function}", "name": "connectListener", "type": "Function" } ] } ], "desc": "<p>Alias to\n<a href=\"#netcreateconnectionpath-connectlistener\"><code>net.createConnection(path[, connectListener])</code></a>.</p>" }, { "textRaw": "`net.connect(port[, host][, connectListener])`", "type": "method", "name": "connect", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket}", "name": "return", "type": "net.Socket" }, "params": [ { "textRaw": "`port` {number}", "name": "port", "type": "number" }, { "textRaw": "`host` {string}", "name": "host", "type": "string" }, { "textRaw": "`connectListener` {Function}", "name": "connectListener", "type": "Function" } ] } ], "desc": "<p>Alias to\n<a href=\"#netcreateconnectionport-host-connectlistener\"><code>net.createConnection(port[, host][, connectListener])</code></a>.</p>" } ] }, { "textRaw": "`net.createConnection()`", "type": "method", "name": "createConnection", "signatures": [ { "params": [] } ], "desc": "<p>A factory function, which creates a new <a href=\"#class-netsocket\"><code>net.Socket</code></a>,\nimmediately initiates connection with <a href=\"#socketconnect\"><code>socket.connect()</code></a>,\nthen returns the <code>net.Socket</code> that starts the connection.</p>\n<p>When the connection is established, a <a href=\"#event-connect\"><code>'connect'</code></a> event will be emitted\non the returned socket. The last parameter <code>connectListener</code>, if supplied,\nwill be added as a listener for the <a href=\"#event-connect\"><code>'connect'</code></a> event <strong>once</strong>.</p>\n<p>Possible signatures:</p>\n<ul>\n<li><a href=\"#netcreateconnectionoptions-connectlistener\"><code>net.createConnection(options[, connectListener])</code></a></li>\n<li><a href=\"#netcreateconnectionpath-connectlistener\"><code>net.createConnection(path[, connectListener])</code></a>\nfor <a href=\"#ipc-support\">IPC</a> connections.</li>\n<li><a href=\"#netcreateconnectionport-host-connectlistener\"><code>net.createConnection(port[, host][, connectListener])</code></a>\nfor TCP connections.</li>\n</ul>\n<p>The <a href=\"#netconnect\"><code>net.connect()</code></a> function is an alias to this function.</p>", "methods": [ { "textRaw": "`net.createConnection(options[, connectListener])`", "type": "method", "name": "createConnection", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The newly created socket used to start the connection.", "name": "return", "type": "net.Socket", "desc": "The newly created socket used to start the connection." }, "params": [ { "textRaw": "`options` {Object} Required. Will be passed to both the [`new net.Socket([options])`][`new net.Socket(options)`] call and the [`socket.connect(options[, connectListener])`][`socket.connect(options)`] method.", "name": "options", "type": "Object", "desc": "Required. Will be passed to both the [`new net.Socket([options])`][`new net.Socket(options)`] call and the [`socket.connect(options[, connectListener])`][`socket.connect(options)`] method." }, { "textRaw": "`connectListener` {Function} Common parameter of the [`net.createConnection()`][] functions. If supplied, will be added as a listener for the [`'connect'`][] event on the returned socket once.", "name": "connectListener", "type": "Function", "desc": "Common parameter of the [`net.createConnection()`][] functions. If supplied, will be added as a listener for the [`'connect'`][] event on the returned socket once." } ] } ], "desc": "<p>For available options, see\n<a href=\"#new-netsocketoptions\"><code>new net.Socket([options])</code></a>\nand <a href=\"#socketconnectoptions-connectlistener\"><code>socket.connect(options[, connectListener])</code></a>.</p>\n<p>Additional options:</p>\n<ul>\n<li><code>timeout</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\"><number></a> If set, will be used to call\n<a href=\"#socketsettimeouttimeout-callback\"><code>socket.setTimeout(timeout)</code></a> after the socket is created, but before\nit starts the connection.</li>\n</ul>\n<p>Following is an example of a client of the echo server described\nin the <a href=\"#netcreateserveroptions-connectionlistener\"><code>net.createServer()</code></a> section:</p>\n<pre><code class=\"language-js\">const net = require('node:net');\nconst client = net.createConnection({ port: 8124 }, () => {\n // 'connect' listener.\n console.log('connected to server!');\n client.write('world!\\r\\n');\n});\nclient.on('data', (data) => {\n console.log(data.toString());\n client.end();\n});\nclient.on('end', () => {\n console.log('disconnected from server');\n});\n</code></pre>\n<p>To connect on the socket <code>/tmp/echo.sock</code>:</p>\n<pre><code class=\"language-js\">const client = net.createConnection({ path: '/tmp/echo.sock' });\n</code></pre>" }, { "textRaw": "`net.createConnection(path[, connectListener])`", "type": "method", "name": "createConnection", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The newly created socket used to start the connection.", "name": "return", "type": "net.Socket", "desc": "The newly created socket used to start the connection." }, "params": [ { "textRaw": "`path` {string} Path the socket should connect to. Will be passed to [`socket.connect(path[, connectListener])`][`socket.connect(path)`]. See [Identifying paths for IPC connections][].", "name": "path", "type": "string", "desc": "Path the socket should connect to. Will be passed to [`socket.connect(path[, connectListener])`][`socket.connect(path)`]. See [Identifying paths for IPC connections][]." }, { "textRaw": "`connectListener` {Function} Common parameter of the [`net.createConnection()`][] functions, an \"once\" listener for the `'connect'` event on the initiating socket. Will be passed to [`socket.connect(path[, connectListener])`][`socket.connect(path)`].", "name": "connectListener", "type": "Function", "desc": "Common parameter of the [`net.createConnection()`][] functions, an \"once\" listener for the `'connect'` event on the initiating socket. Will be passed to [`socket.connect(path[, connectListener])`][`socket.connect(path)`]." } ] } ], "desc": "<p>Initiates an <a href=\"#ipc-support\">IPC</a> connection.</p>\n<p>This function creates a new <a href=\"#class-netsocket\"><code>net.Socket</code></a> with all options set to default,\nimmediately initiates connection with\n<a href=\"#socketconnectpath-connectlistener\"><code>socket.connect(path[, connectListener])</code></a>,\nthen returns the <code>net.Socket</code> that starts the connection.</p>" }, { "textRaw": "`net.createConnection(port[, host][, connectListener])`", "type": "method", "name": "createConnection", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Socket} The newly created socket used to start the connection.", "name": "return", "type": "net.Socket", "desc": "The newly created socket used to start the connection." }, "params": [ { "textRaw": "`port` {number} Port the socket should connect to. Will be passed to [`socket.connect(port[, host][, connectListener])`][`socket.connect(port)`].", "name": "port", "type": "number", "desc": "Port the socket should connect to. Will be passed to [`socket.connect(port[, host][, connectListener])`][`socket.connect(port)`]." }, { "textRaw": "`host` {string} Host the socket should connect to. Will be passed to [`socket.connect(port[, host][, connectListener])`][`socket.connect(port)`]. **Default:** `'localhost'`.", "name": "host", "type": "string", "default": "`'localhost'`", "desc": "Host the socket should connect to. Will be passed to [`socket.connect(port[, host][, connectListener])`][`socket.connect(port)`]." }, { "textRaw": "`connectListener` {Function} Common parameter of the [`net.createConnection()`][] functions, an \"once\" listener for the `'connect'` event on the initiating socket. Will be passed to [`socket.connect(port[, host][, connectListener])`][`socket.connect(port)`].", "name": "connectListener", "type": "Function", "desc": "Common parameter of the [`net.createConnection()`][] functions, an \"once\" listener for the `'connect'` event on the initiating socket. Will be passed to [`socket.connect(port[, host][, connectListener])`][`socket.connect(port)`]." } ] } ], "desc": "<p>Initiates a TCP connection.</p>\n<p>This function creates a new <a href=\"#class-netsocket\"><code>net.Socket</code></a> with all options set to default,\nimmediately initiates connection with\n<a href=\"#socketconnectport-host-connectlistener\"><code>socket.connect(port[, host][, connectListener])</code></a>,\nthen returns the <code>net.Socket</code> that starts the connection.</p>" } ] }, { "textRaw": "`net.createServer([options][, connectionListener])`", "type": "method", "name": "createServer", "meta": { "added": [ "v0.5.0" ], "changes": [ { "version": [ "v17.7.0", "v16.15.0" ], "pr-url": "https://github.com/nodejs/node/pull/41310", "description": "The `noDelay`, `keepAlive`, and `keepAliveInitialDelay` options are supported now." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {net.Server}", "name": "return", "type": "net.Server" }, "params": [ { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`allowHalfOpen` {boolean} If set to `false`, then the socket will automatically end the writable side when the readable side ends. **Default:** `false`.", "name": "allowHalfOpen", "type": "boolean", "default": "`false`", "desc": "If set to `false`, then the socket will automatically end the writable side when the readable side ends." }, { "textRaw": "`pauseOnConnect` {boolean} Indicates whether the socket should be paused on incoming connections. **Default:** `false`.", "name": "pauseOnConnect", "type": "boolean", "default": "`false`", "desc": "Indicates whether the socket should be paused on incoming connections." }, { "textRaw": "`noDelay` {boolean} If set to `true`, it disables the use of Nagle's algorithm immediately after a new incoming connection is received. **Default:** `false`.", "name": "noDelay", "type": "boolean", "default": "`false`", "desc": "If set to `true`, it disables the use of Nagle's algorithm immediately after a new incoming connection is received." }, { "textRaw": "`keepAlive` {boolean} If set to `true`, it enables keep-alive functionality on the socket immediately after a new incoming connection is received, similarly on what is done in [`socket.setKeepAlive([enable][, initialDelay])`][`socket.setKeepAlive(enable, initialDelay)`]. **Default:** `false`.", "name": "keepAlive", "type": "boolean", "default": "`false`", "desc": "If set to `true`, it enables keep-alive functionality on the socket immediately after a new incoming connection is received, similarly on what is done in [`socket.setKeepAlive([enable][, initialDelay])`][`socket.setKeepAlive(enable, initialDelay)`]." }, { "textRaw": "`keepAliveInitialDelay` {number} If set to a positive number, it sets the initial delay before the first keepalive probe is sent on an idle socket.**Default:** `0`.", "name": "keepAliveInitialDelay", "type": "number", "default": "`0`", "desc": "If set to a positive number, it sets the initial delay before the first keepalive probe is sent on an idle socket." } ] }, { "textRaw": "`connectionListener` {Function} Automatically set as a listener for the [`'connection'`][] event.", "name": "connectionListener", "type": "Function", "desc": "Automatically set as a listener for the [`'connection'`][] event." } ] } ], "desc": "<p>Creates a new TCP or <a href=\"#ipc-support\">IPC</a> server.</p>\n<p>If <code>allowHalfOpen</code> is set to <code>true</code>, when the other end of the socket\nsignals the end of transmission, the server will only send back the end of\ntransmission when <a href=\"#socketenddata-encoding-callback\"><code>socket.end()</code></a> is explicitly called. For example, in the\ncontext of TCP, when a FIN packed is received, a FIN packed is sent\nback only when <a href=\"#socketenddata-encoding-callback\"><code>socket.end()</code></a> is explicitly called. Until then the\nconnection is half-closed (non-readable but still writable). See <a href=\"#event-end\"><code>'end'</code></a>\nevent and <a href=\"https://tools.ietf.org/html/rfc1122\">RFC 1122</a> (section 4.2.2.13) for more information.</p>\n<p>If <code>pauseOnConnect</code> is set to <code>true</code>, then the socket associated with each\nincoming connection will be paused, and no data will be read from its handle.\nThis allows connections to be passed between processes without any data being\nread by the original process. To begin reading data from a paused socket, call\n<a href=\"#socketresume\"><code>socket.resume()</code></a>.</p>\n<p>The server can be a TCP server or an <a href=\"#ipc-support\">IPC</a> server, depending on what it\n<a href=\"#serverlisten\"><code>listen()</code></a> to.</p>\n<p>Here is an example of a TCP echo server which listens for connections\non port 8124:</p>\n<pre><code class=\"language-js\">const net = require('node:net');\nconst server = net.createServer((c) => {\n // 'connection' listener.\n console.log('client connected');\n c.on('end', () => {\n console.log('client disconnected');\n });\n c.write('hello\\r\\n');\n c.pipe(c);\n});\nserver.on('error', (err) => {\n throw err;\n});\nserver.listen(8124, () => {\n console.log('server bound');\n});\n</code></pre>\n<p>Test this by using <code>telnet</code>:</p>\n<pre><code class=\"language-console\">$ telnet localhost 8124\n</code></pre>\n<p>To listen on the socket <code>/tmp/echo.sock</code>:</p>\n<pre><code class=\"language-js\">server.listen('/tmp/echo.sock', () => {\n console.log('server bound');\n});\n</code></pre>\n<p>Use <code>nc</code> to connect to a Unix domain socket server:</p>\n<pre><code class=\"language-console\">$ nc -U /tmp/echo.sock\n</code></pre>" }, { "textRaw": "`net.isIP(input)`", "type": "method", "name": "isIP", "meta": { "added": [ "v0.3.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {integer}", "name": "return", "type": "integer" }, "params": [ { "textRaw": "`input` {string}", "name": "input", "type": "string" } ] } ], "desc": "<p>Returns <code>6</code> if <code>input</code> is an IPv6 address. Returns <code>4</code> if <code>input</code> is an IPv4\naddress in <a href=\"https://en.wikipedia.org/wiki/Dot-decimal_notation\">dot-decimal notation</a> with no leading zeroes. Otherwise, returns\n<code>0</code>.</p>\n<pre><code class=\"language-js\">net.isIP('::1'); // returns 6\nnet.isIP('127.0.0.1'); // returns 4\nnet.isIP('127.000.000.001'); // returns 0\nnet.isIP('127.0.0.1/24'); // returns 0\nnet.isIP('fhqwhgads'); // returns 0\n</code></pre>" }, { "textRaw": "`net.isIPv4(input)`", "type": "method", "name": "isIPv4", "meta": { "added": [ "v0.3.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [ { "textRaw": "`input` {string}", "name": "input", "type": "string" } ] } ], "desc": "<p>Returns <code>true</code> if <code>input</code> is an IPv4 address in <a href=\"https://en.wikipedia.org/wiki/Dot-decimal_notation\">dot-decimal notation</a> with no\nleading zeroes. Otherwise, returns <code>false</code>.</p>\n<pre><code class=\"language-js\">net.isIPv4('127.0.0.1'); // returns true\nnet.isIPv4('127.000.000.001'); // returns false\nnet.isIPv4('127.0.0.1/24'); // returns false\nnet.isIPv4('fhqwhgads'); // returns false\n</code></pre>" }, { "textRaw": "`net.isIPv6(input)`", "type": "method", "name": "isIPv6", "meta": { "added": [ "v0.3.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [ { "textRaw": "`input` {string}", "name": "input", "type": "string" } ] } ], "desc": "<p>Returns <code>true</code> if <code>input</code> is an IPv6 address. Otherwise, returns <code>false</code>.</p>\n<pre><code class=\"language-js\">net.isIPv6('::1'); // returns true\nnet.isIPv6('fhqwhgads'); // returns false\n</code></pre>" } ], "type": "module", "displayName": "Net" } ] }
.
Edit
..
Edit
addons.html
Edit
addons.json
Edit
addons.md
Edit
all.html
Edit
all.json
Edit
assert.html
Edit
assert.json
Edit
assert.md
Edit
assets
Edit
async_context.html
Edit
async_context.json
Edit
async_context.md
Edit
async_hooks.html
Edit
async_hooks.json
Edit
async_hooks.md
Edit
buffer.html
Edit
buffer.json
Edit
buffer.md
Edit
child_process.html
Edit
child_process.json
Edit
child_process.md
Edit
cli.html
Edit
cli.json
Edit
cli.md
Edit
cluster.html
Edit
cluster.json
Edit
cluster.md
Edit
console.html
Edit
console.json
Edit
console.md
Edit
corepack.html
Edit
corepack.json
Edit
corepack.md
Edit
crypto.html
Edit
crypto.json
Edit
crypto.md
Edit
debugger.html
Edit
debugger.json
Edit
debugger.md
Edit
deprecations.html
Edit
deprecations.json
Edit
deprecations.md
Edit
dgram.html
Edit
dgram.json
Edit
dgram.md
Edit
diagnostics_channel.html
Edit
diagnostics_channel.json
Edit
diagnostics_channel.md
Edit
dns.html
Edit
dns.json
Edit
dns.md
Edit
documentation.html
Edit
documentation.json
Edit
documentation.md
Edit
domain.html
Edit
domain.json
Edit
domain.md
Edit
embedding.html
Edit
embedding.json
Edit
embedding.md
Edit
errors.html
Edit
errors.json
Edit
errors.md
Edit
esm.html
Edit
esm.json
Edit
esm.md
Edit
events.html
Edit
events.json
Edit
events.md
Edit
fs.html
Edit
fs.json
Edit
fs.md
Edit
globals.html
Edit
globals.json
Edit
globals.md
Edit
http.html
Edit
http.json
Edit
http.md
Edit
http2.html
Edit
http2.json
Edit
http2.md
Edit
https.html
Edit
https.json
Edit
https.md
Edit
index.html
Edit
index.json
Edit
index.md
Edit
inspector.html
Edit
inspector.json
Edit
inspector.md
Edit
intl.html
Edit
intl.json
Edit
intl.md
Edit
module.html
Edit
module.json
Edit
module.md
Edit
modules.html
Edit
modules.json
Edit
modules.md
Edit
n-api.html
Edit
n-api.json
Edit
n-api.md
Edit
net.html
Edit
net.json
Edit
net.md
Edit
os.html
Edit
os.json
Edit
os.md
Edit
packages.html
Edit
packages.json
Edit
packages.md
Edit
path.html
Edit
path.json
Edit
path.md
Edit
perf_hooks.html
Edit
perf_hooks.json
Edit
perf_hooks.md
Edit
permissions.html
Edit
permissions.json
Edit
permissions.md
Edit
policy.html
Edit
policy.json
Edit
policy.md
Edit
process.html
Edit
process.json
Edit
process.md
Edit
punycode.html
Edit
punycode.json
Edit
punycode.md
Edit
querystring.html
Edit
querystring.json
Edit
querystring.md
Edit
readline.html
Edit
readline.json
Edit
readline.md
Edit
repl.html
Edit
repl.json
Edit
repl.md
Edit
report.html
Edit
report.json
Edit
report.md
Edit
stream.html
Edit
stream.json
Edit
stream.md
Edit
string_decoder.html
Edit
string_decoder.json
Edit
string_decoder.md
Edit
synopsis.html
Edit
synopsis.json
Edit
synopsis.md
Edit
test.html
Edit
test.json
Edit
test.md
Edit
timers.html
Edit
timers.json
Edit
timers.md
Edit
tls.html
Edit
tls.json
Edit
tls.md
Edit
tracing.html
Edit
tracing.json
Edit
tracing.md
Edit
tty.html
Edit
tty.json
Edit
tty.md
Edit
url.html
Edit
url.json
Edit
url.md
Edit
util.html
Edit
util.json
Edit
util.md
Edit
v8.html
Edit
v8.json
Edit
v8.md
Edit
vm.html
Edit
vm.json
Edit
vm.md
Edit
wasi.html
Edit
wasi.json
Edit
wasi.md
Edit
webcrypto.html
Edit
webcrypto.json
Edit
webcrypto.md
Edit
webstreams.html
Edit
webstreams.json
Edit
webstreams.md
Edit
worker_threads.html
Edit
worker_threads.json
Edit
worker_threads.md
Edit
zlib.html
Edit
zlib.json
Edit
zlib.md
Edit