1<!DOCTYPE html> 2<html lang="en"> 3<head> 4 <meta charset="utf-8"> 5 <meta name="viewport" content="width=device-width"> 6 <meta name="nodejs.org:node-version" content="v14.21.2"> 7 <title>Net | Node.js v14.21.2 Documentation</title> 8 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 9 <link rel="stylesheet" href="assets/style.css"> 10 <link rel="stylesheet" href="assets/hljs.css"> 11 <link rel="canonical" href="https://nodejs.org/api/net.html"> 12</head> 13<body class="alt apidoc" id="api-section-net"> 14 <div id="content" class="clearfix"> 15 <div id="column2" class="interior"> 16 <div id="intro" class="interior"> 17 <a href="/" title="Go back to the home page"> 18 Node.js 19 </a> 20 </div> 21 <ul> 22<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 23<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 24</ul> 25<hr class="line"> 26<ul> 27<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 28<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 29<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 30<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 31<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 32<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 33<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 34<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 35<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 36<li><a href="console.html" class="nav-console">Console</a></li> 37<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 38<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 39<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 40<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 41<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 42<li><a href="dns.html" class="nav-dns">DNS</a></li> 43<li><a href="domain.html" class="nav-domain">Domain</a></li> 44<li><a href="errors.html" class="nav-errors">Errors</a></li> 45<li><a href="events.html" class="nav-events">Events</a></li> 46<li><a href="fs.html" class="nav-fs">File system</a></li> 47<li><a href="globals.html" class="nav-globals">Globals</a></li> 48<li><a href="http.html" class="nav-http">HTTP</a></li> 49<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 50<li><a href="https.html" class="nav-https">HTTPS</a></li> 51<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 52<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 53<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 54<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 55<li><a href="module.html" class="nav-module">Modules: <code>module</code> API</a></li> 56<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 57<li><a href="net.html" class="nav-net active">Net</a></li> 58<li><a href="os.html" class="nav-os">OS</a></li> 59<li><a href="path.html" class="nav-path">Path</a></li> 60<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 61<li><a href="policy.html" class="nav-policy">Policies</a></li> 62<li><a href="process.html" class="nav-process">Process</a></li> 63<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 64<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 65<li><a href="readline.html" class="nav-readline">Readline</a></li> 66<li><a href="repl.html" class="nav-repl">REPL</a></li> 67<li><a href="report.html" class="nav-report">Report</a></li> 68<li><a href="stream.html" class="nav-stream">Stream</a></li> 69<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 70<li><a href="timers.html" class="nav-timers">Timers</a></li> 71<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 72<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 73<li><a href="tty.html" class="nav-tty">TTY</a></li> 74<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 75<li><a href="url.html" class="nav-url">URL</a></li> 76<li><a href="util.html" class="nav-util">Utilities</a></li> 77<li><a href="v8.html" class="nav-v8">V8</a></li> 78<li><a href="vm.html" class="nav-vm">VM</a></li> 79<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 80<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 81<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 82</ul> 83<hr class="line"> 84<ul> 85<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 86</ul> 87 </div> 88 89 <div id="column1" data-id="net" class="interior"> 90 <header> 91 <div class="header-container"> 92 <h1>Node.js v14.21.2 documentation</h1> 93 <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 94 <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 95 <path fill="none" d="M0 0h24v24H0z" /> 96 <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/> 97 <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/> 98 </svg> 99 <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 100 <path d="M0 0h24v24H0z" fill="none" /> 101 <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/> 102 </svg> 103 </button> 104 </div> 105 <div id="gtoc"> 106 <ul> 107 <li> 108 <a href="index.html">Index</a> 109 </li> 110 <li> 111 <a href="all.html">View on single page</a> 112 </li> 113 <li> 114 <a href="net.json">View as JSON</a> 115 </li> 116 117 <li class="version-picker"> 118 <a href="#">View another version <span>▼</span></a> 119 <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v19.x/api/net.html">19.x</a></li> 120<li><a href="https://nodejs.org/docs/latest-v18.x/api/net.html">18.x <b>LTS</b></a></li> 121<li><a href="https://nodejs.org/docs/latest-v17.x/api/net.html">17.x</a></li> 122<li><a href="https://nodejs.org/docs/latest-v16.x/api/net.html">16.x <b>LTS</b></a></li> 123<li><a href="https://nodejs.org/docs/latest-v15.x/api/net.html">15.x</a></li> 124<li><a href="https://nodejs.org/docs/latest-v14.x/api/net.html">14.x <b>LTS</b></a></li> 125<li><a href="https://nodejs.org/docs/latest-v13.x/api/net.html">13.x</a></li> 126<li><a href="https://nodejs.org/docs/latest-v12.x/api/net.html">12.x</a></li> 127<li><a href="https://nodejs.org/docs/latest-v11.x/api/net.html">11.x</a></li> 128<li><a href="https://nodejs.org/docs/latest-v10.x/api/net.html">10.x</a></li> 129<li><a href="https://nodejs.org/docs/latest-v9.x/api/net.html">9.x</a></li> 130<li><a href="https://nodejs.org/docs/latest-v8.x/api/net.html">8.x</a></li> 131<li><a href="https://nodejs.org/docs/latest-v7.x/api/net.html">7.x</a></li> 132<li><a href="https://nodejs.org/docs/latest-v6.x/api/net.html">6.x</a></li> 133<li><a href="https://nodejs.org/docs/latest-v5.x/api/net.html">5.x</a></li> 134<li><a href="https://nodejs.org/docs/latest-v4.x/api/net.html">4.x</a></li> 135<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/net.html">0.12.x</a></li> 136<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/net.html">0.10.x</a></li></ol> 137 </li> 138 139 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/net.md">Edit on GitHub</a></li> 140 </ul> 141 </div> 142 <hr> 143 </header> 144 145 <details id="toc" open><summary>Table of contents</summary><ul> 146<li><span class="stability_2"><a href="#net_net">Net</a></span> 147<ul> 148<li><a href="#net_ipc_support">IPC support</a> 149<ul> 150<li><a href="#net_identifying_paths_for_ipc_connections">Identifying paths for IPC connections</a></li> 151</ul> 152</li> 153<li><a href="#net_class_net_blocklist">Class: <code>net.BlockList</code></a> 154<ul> 155<li><a href="#net_blocklist_addaddress_address_type"><code>blockList.addAddress(address[, type])</code></a></li> 156<li><a href="#net_blocklist_addrange_start_end_type"><code>blockList.addRange(start, end[, type])</code></a></li> 157<li><a href="#net_blocklist_addsubnet_net_prefix_type"><code>blockList.addSubnet(net, prefix[, type])</code></a></li> 158<li><a href="#net_blocklist_check_address_type"><code>blockList.check(address[, type])</code></a></li> 159<li><a href="#net_blocklist_rules"><code>blockList.rules</code></a></li> 160</ul> 161</li> 162<li><a href="#net_class_net_socketaddress">Class: <code>net.SocketAddress</code></a> 163<ul> 164<li><a href="#net_new_net_socketaddress_options"><code>new net.SocketAddress([options])</code></a></li> 165<li><a href="#net_socketaddress_address"><code>socketaddress.address</code></a></li> 166<li><a href="#net_socketaddress_family"><code>socketaddress.family</code></a></li> 167<li><a href="#net_socketaddress_flowlabel"><code>socketaddress.flowlabel</code></a></li> 168<li><a href="#net_socketaddress_port"><code>socketaddress.port</code></a></li> 169</ul> 170</li> 171<li><a href="#net_class_net_server">Class: <code>net.Server</code></a> 172<ul> 173<li><a href="#net_new_net_server_options_connectionlistener"><code>new net.Server([options][, connectionListener])</code></a></li> 174<li><a href="#net_event_close">Event: <code>'close'</code></a></li> 175<li><a href="#net_event_connection">Event: <code>'connection'</code></a></li> 176<li><a href="#net_event_error">Event: <code>'error'</code></a></li> 177<li><a href="#net_event_listening">Event: <code>'listening'</code></a></li> 178<li><a href="#net_server_address"><code>server.address()</code></a></li> 179<li><a href="#net_server_close_callback"><code>server.close([callback])</code></a></li> 180<li><span class="stability_0"><a href="#net_server_connections"><code>server.connections</code></a></span></li> 181<li><a href="#net_server_getconnections_callback"><code>server.getConnections(callback)</code></a></li> 182<li><a href="#net_server_listen"><code>server.listen()</code></a> 183<ul> 184<li><a href="#net_server_listen_handle_backlog_callback"><code>server.listen(handle[, backlog][, callback])</code></a></li> 185<li><a href="#net_server_listen_options_callback"><code>server.listen(options[, callback])</code></a></li> 186<li><a href="#net_server_listen_path_backlog_callback"><code>server.listen(path[, backlog][, callback])</code></a></li> 187<li><a href="#net_server_listen_port_host_backlog_callback"><code>server.listen([port[, host[, backlog]]][, callback])</code></a></li> 188</ul> 189</li> 190<li><a href="#net_server_listening"><code>server.listening</code></a></li> 191<li><a href="#net_server_maxconnections"><code>server.maxConnections</code></a></li> 192<li><a href="#net_server_ref"><code>server.ref()</code></a></li> 193<li><a href="#net_server_unref"><code>server.unref()</code></a></li> 194</ul> 195</li> 196<li><a href="#net_class_net_socket">Class: <code>net.Socket</code></a> 197<ul> 198<li><a href="#net_new_net_socket_options"><code>new net.Socket([options])</code></a></li> 199<li><a href="#net_event_close_1">Event: <code>'close'</code></a></li> 200<li><a href="#net_event_connect">Event: <code>'connect'</code></a></li> 201<li><a href="#net_event_data">Event: <code>'data'</code></a></li> 202<li><a href="#net_event_drain">Event: <code>'drain'</code></a></li> 203<li><a href="#net_event_end">Event: <code>'end'</code></a></li> 204<li><a href="#net_event_error_1">Event: <code>'error'</code></a></li> 205<li><a href="#net_event_lookup">Event: <code>'lookup'</code></a></li> 206<li><a href="#net_event_ready">Event: <code>'ready'</code></a></li> 207<li><a href="#net_event_timeout">Event: <code>'timeout'</code></a></li> 208<li><a href="#net_socket_address"><code>socket.address()</code></a></li> 209<li><span class="stability_0"><a href="#net_socket_buffersize"><code>socket.bufferSize</code></a></span></li> 210<li><a href="#net_socket_bytesread"><code>socket.bytesRead</code></a></li> 211<li><a href="#net_socket_byteswritten"><code>socket.bytesWritten</code></a></li> 212<li><a href="#net_socket_connect"><code>socket.connect()</code></a> 213<ul> 214<li><a href="#net_socket_connect_options_connectlistener"><code>socket.connect(options[, connectListener])</code></a></li> 215<li><a href="#net_socket_connect_path_connectlistener"><code>socket.connect(path[, connectListener])</code></a></li> 216<li><a href="#net_socket_connect_port_host_connectlistener"><code>socket.connect(port[, host][, connectListener])</code></a></li> 217</ul> 218</li> 219<li><a href="#net_socket_connecting"><code>socket.connecting</code></a></li> 220<li><a href="#net_socket_destroy_error"><code>socket.destroy([error])</code></a></li> 221<li><a href="#net_socket_destroyed"><code>socket.destroyed</code></a></li> 222<li><a href="#net_socket_end_data_encoding_callback"><code>socket.end([data[, encoding]][, callback])</code></a></li> 223<li><a href="#net_socket_localaddress"><code>socket.localAddress</code></a></li> 224<li><a href="#net_socket_localport"><code>socket.localPort</code></a></li> 225<li><a href="#net_socket_pause"><code>socket.pause()</code></a></li> 226<li><a href="#net_socket_pending"><code>socket.pending</code></a></li> 227<li><a href="#net_socket_ref"><code>socket.ref()</code></a></li> 228<li><a href="#net_socket_remoteaddress"><code>socket.remoteAddress</code></a></li> 229<li><a href="#net_socket_remotefamily"><code>socket.remoteFamily</code></a></li> 230<li><a href="#net_socket_remoteport"><code>socket.remotePort</code></a></li> 231<li><a href="#net_socket_resume"><code>socket.resume()</code></a></li> 232<li><a href="#net_socket_setencoding_encoding"><code>socket.setEncoding([encoding])</code></a></li> 233<li><a href="#net_socket_setkeepalive_enable_initialdelay"><code>socket.setKeepAlive([enable][, initialDelay])</code></a></li> 234<li><a href="#net_socket_setnodelay_nodelay"><code>socket.setNoDelay([noDelay])</code></a></li> 235<li><a href="#net_socket_settimeout_timeout_callback"><code>socket.setTimeout(timeout[, callback])</code></a></li> 236<li><a href="#net_socket_timeout"><code>socket.timeout</code></a></li> 237<li><a href="#net_socket_unref"><code>socket.unref()</code></a></li> 238<li><a href="#net_socket_write_data_encoding_callback"><code>socket.write(data[, encoding][, callback])</code></a></li> 239<li><a href="#net_socket_readystate"><code>socket.readyState</code></a></li> 240</ul> 241</li> 242<li><a href="#net_net_connect"><code>net.connect()</code></a> 243<ul> 244<li><a href="#net_net_connect_options_connectlistener"><code>net.connect(options[, connectListener])</code></a></li> 245<li><a href="#net_net_connect_path_connectlistener"><code>net.connect(path[, connectListener])</code></a></li> 246<li><a href="#net_net_connect_port_host_connectlistener"><code>net.connect(port[, host][, connectListener])</code></a></li> 247</ul> 248</li> 249<li><a href="#net_net_createconnection"><code>net.createConnection()</code></a> 250<ul> 251<li><a href="#net_net_createconnection_options_connectlistener"><code>net.createConnection(options[, connectListener])</code></a></li> 252<li><a href="#net_net_createconnection_path_connectlistener"><code>net.createConnection(path[, connectListener])</code></a></li> 253<li><a href="#net_net_createconnection_port_host_connectlistener"><code>net.createConnection(port[, host][, connectListener])</code></a></li> 254</ul> 255</li> 256<li><a href="#net_net_createserver_options_connectionlistener"><code>net.createServer([options][, connectionListener])</code></a></li> 257<li><a href="#net_net_isip_input"><code>net.isIP(input)</code></a></li> 258<li><a href="#net_net_isipv4_input"><code>net.isIPv4(input)</code></a></li> 259<li><a href="#net_net_isipv6_input"><code>net.isIPv6(input)</code></a></li> 260</ul> 261</li> 262</ul></details> 263 264 <div id="apicontent"> 265 <h2>Net<span><a class="mark" href="#net_net" id="net_net">#</a></span></h2> 266 267<!--lint disable maximum-line-length--> 268<p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p> 269<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v14.21.2/lib/net.js">lib/net.js</a></p> 270<p>The <code>net</code> module provides an asynchronous network API for creating stream-based 271TCP or <a href="#net_ipc_support">IPC</a> servers (<a href="#net_net_createserver_options_connectionlistener"><code>net.createServer()</code></a>) and clients 272(<a href="#net_net_createconnection"><code>net.createConnection()</code></a>).</p> 273<p>It can be accessed using:</p> 274<pre><code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">'net'</span>);</code></pre> 275<section><h3>IPC support<span><a class="mark" href="#net_ipc_support" id="net_ipc_support">#</a></span></h3> 276<p>The <code>net</code> module supports IPC with named pipes on Windows, and Unix domain 277sockets on other operating systems.</p> 278<h4>Identifying paths for IPC connections<span><a class="mark" href="#net_identifying_paths_for_ipc_connections" id="net_identifying_paths_for_ipc_connections">#</a></span></h4> 279<p><a href="#net_net_connect"><code>net.connect()</code></a>, <a href="#net_net_createconnection"><code>net.createConnection()</code></a>, <a href="#net_server_listen"><code>server.listen()</code></a> and 280<a href="#net_socket_connect"><code>socket.connect()</code></a> take a <code>path</code> parameter to identify IPC endpoints.</p> 281<p>On Unix, the local domain is also known as the Unix domain. The path is a 282filesystem pathname. It gets truncated to an OS-dependent length of 283<code>sizeof(sockaddr_un.sun_path) - 1</code>. Typical values are 107 bytes on Linux and 284103 bytes on macOS. If a Node.js API abstraction creates the Unix domain socket, 285it will unlink the Unix domain socket as well. For example, 286<a href="#net_net_createserver_options_connectionlistener"><code>net.createServer()</code></a> may create a Unix domain socket and 287<a href="#net_server_close_callback"><code>server.close()</code></a> will unlink it. But if a user creates the Unix domain 288socket outside of these abstractions, the user will need to remove it. The same 289applies when a Node.js API creates a Unix domain socket but the program then 290crashes. In short, a Unix domain socket will be visible in the filesystem and 291will persist until unlinked.</p> 292<p>On Windows, the local domain is implemented using a named pipe. The path <em>must</em> 293refer to an entry in <code>\\?\pipe\</code> or <code>\\.\pipe\</code>. Any characters are permitted, 294but the latter may do some processing of pipe names, such as resolving <code>..</code> 295sequences. Despite how it might look, the pipe namespace is flat. Pipes will 296<em>not persist</em>. They are removed when the last reference to them is closed. 297Unlike Unix domain sockets, Windows will close and remove the pipe when the 298owning process exits.</p> 299<p>JavaScript string escaping requires paths to be specified with extra backslash 300escaping such as:</p> 301<pre><code class="language-js">net.<span class="hljs-title function_">createServer</span>().<span class="hljs-title function_">listen</span>( 302 path.<span class="hljs-title function_">join</span>(<span class="hljs-string">'\\\\?\\pipe'</span>, process.<span class="hljs-title function_">cwd</span>(), <span class="hljs-string">'myctl'</span>));</code></pre> 303</section><section><h3>Class: <code>net.BlockList</code><span><a class="mark" href="#net_class_net_blocklist" id="net_class_net_blocklist">#</a></span></h3> 304<div class="api_metadata"> 305<span>Added in: v14.18.0</span> 306</div> 307<p>The <code>BlockList</code> object can be used with some network APIs to specify rules for 308disabling inbound or outbound access to specific IP addresses, IP ranges, or 309IP subnets.</p> 310<h4><code>blockList.addAddress(address[, type])</code><span><a class="mark" href="#net_blocklist_addaddress_address_type" id="net_blocklist_addaddress_address_type">#</a></span></h4> 311<div class="api_metadata"> 312<span>Added in: v14.18.0</span> 313</div> 314<ul> 315<li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="net.html#net_class_net_socketaddress" class="type"><net.SocketAddress></a> An IPv4 or IPv6 address.</li> 316<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Either <code>'ipv4'</code> or <code>'ipv6'</code>. <strong>Default:</strong> <code>'ipv4'</code>.</li> 317</ul> 318<p>Adds a rule to block the given IP address.</p> 319<h4><code>blockList.addRange(start, end[, type])</code><span><a class="mark" href="#net_blocklist_addrange_start_end_type" id="net_blocklist_addrange_start_end_type">#</a></span></h4> 320<div class="api_metadata"> 321<span>Added in: v14.18.0</span> 322</div> 323<ul> 324<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="net.html#net_class_net_socketaddress" class="type"><net.SocketAddress></a> The starting IPv4 or IPv6 address in the 325range.</li> 326<li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="net.html#net_class_net_socketaddress" class="type"><net.SocketAddress></a> The ending IPv4 or IPv6 address in the range.</li> 327<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Either <code>'ipv4'</code> or <code>'ipv6'</code>. <strong>Default:</strong> <code>'ipv4'</code>.</li> 328</ul> 329<p>Adds a rule to block a range of IP addresses from <code>start</code> (inclusive) to 330<code>end</code> (inclusive).</p> 331<h4><code>blockList.addSubnet(net, prefix[, type])</code><span><a class="mark" href="#net_blocklist_addsubnet_net_prefix_type" id="net_blocklist_addsubnet_net_prefix_type">#</a></span></h4> 332<div class="api_metadata"> 333<span>Added in: v14.18.0</span> 334</div> 335<ul> 336<li><code>net</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="net.html#net_class_net_socketaddress" class="type"><net.SocketAddress></a> The network IPv4 or IPv6 address.</li> 337<li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of CIDR prefix bits. For IPv4, this 338must be a value between <code>0</code> and <code>32</code>. For IPv6, this must be between 339<code>0</code> and <code>128</code>.</li> 340<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Either <code>'ipv4'</code> or <code>'ipv6'</code>. <strong>Default</strong>: <code>'ipv4'</code>.</li> 341</ul> 342<p>Adds a rule to block a range of IP addresses specified as a subnet mask.</p> 343<h4><code>blockList.check(address[, type])</code><span><a class="mark" href="#net_blocklist_check_address_type" id="net_blocklist_check_address_type">#</a></span></h4> 344<div class="api_metadata"> 345<span>Added in: v14.18.0</span> 346</div> 347<ul> 348<li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="net.html#net_class_net_socketaddress" class="type"><net.SocketAddress></a> The IP address to check</li> 349<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Either <code>'ipv4'</code> or <code>'ipv6'</code>. <strong>Default:</strong> <code>'ipv4'</code>.</li> 350<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 351</ul> 352<p>Returns <code>true</code> if the given IP address matches any of the rules added to the 353<code>BlockList</code>.</p> 354<pre><code class="language-js"><span class="hljs-keyword">const</span> blockList = <span class="hljs-keyword">new</span> net.<span class="hljs-title class_">BlockList</span>(); 355blockList.<span class="hljs-title function_">addAddress</span>(<span class="hljs-string">'123.123.123.123'</span>); 356blockList.<span class="hljs-title function_">addRange</span>(<span class="hljs-string">'10.0.0.1'</span>, <span class="hljs-string">'10.0.0.10'</span>); 357blockList.<span class="hljs-title function_">addSubnet</span>(<span class="hljs-string">'8592:757c:efae:4e45::'</span>, <span class="hljs-number">64</span>, <span class="hljs-string">'ipv6'</span>); 358 359<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(blockList.<span class="hljs-title function_">check</span>(<span class="hljs-string">'123.123.123.123'</span>)); <span class="hljs-comment">// Prints: true</span> 360<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(blockList.<span class="hljs-title function_">check</span>(<span class="hljs-string">'10.0.0.3'</span>)); <span class="hljs-comment">// Prints: true</span> 361<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(blockList.<span class="hljs-title function_">check</span>(<span class="hljs-string">'222.111.111.222'</span>)); <span class="hljs-comment">// Prints: false</span> 362 363<span class="hljs-comment">// IPv6 notation for IPv4 addresses works:</span> 364<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(blockList.<span class="hljs-title function_">check</span>(<span class="hljs-string">'::ffff:7b7b:7b7b'</span>, <span class="hljs-string">'ipv6'</span>)); <span class="hljs-comment">// Prints: true</span> 365<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(blockList.<span class="hljs-title function_">check</span>(<span class="hljs-string">'::ffff:123.123.123.123'</span>, <span class="hljs-string">'ipv6'</span>)); <span class="hljs-comment">// Prints: true</span></code></pre> 366<h4><code>blockList.rules</code><span><a class="mark" href="#net_blocklist_rules" id="net_blocklist_rules">#</a></span></h4> 367<div class="api_metadata"> 368<span>Added in: v14.18.0</span> 369</div> 370<ul> 371<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 372</ul> 373<p>The list of rules added to the blocklist.</p> 374</section><section><h3>Class: <code>net.SocketAddress</code><span><a class="mark" href="#net_class_net_socketaddress" id="net_class_net_socketaddress">#</a></span></h3> 375<div class="api_metadata"> 376<span>Added in: v14.18.0</span> 377</div> 378<h4><code>new net.SocketAddress([options])</code><span><a class="mark" href="#net_new_net_socketaddress_options" id="net_new_net_socketaddress_options">#</a></span></h4> 379<div class="api_metadata"> 380<span>Added in: v14.18.0</span> 381</div> 382<ul> 383<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 384<ul> 385<li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The network address as either an IPv4 or IPv6 string. 386<strong>Default</strong>: <code>'127.0.0.1'</code> if <code>family</code> is <code>'ipv4'</code>; <code>'::'</code> if <code>family</code> is 387<code>'ipv6'</code>.</li> 388<li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> One of either <code>'ipv4'</code> or 'ipv6'<code>. **Default**: </code>'ipv4'`.</li> 389<li><code>flowlabel</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> An IPv6 flow-label used only if <code>family</code> is <code>'ipv6'</code>.</li> 390<li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> An IP port.</li> 391</ul> 392</li> 393</ul> 394<h4><code>socketaddress.address</code><span><a class="mark" href="#net_socketaddress_address" id="net_socketaddress_address">#</a></span></h4> 395<div class="api_metadata"> 396<span>Added in: v14.18.0</span> 397</div> 398<ul> 399<li>Type <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 400</ul> 401<h4><code>socketaddress.family</code><span><a class="mark" href="#net_socketaddress_family" id="net_socketaddress_family">#</a></span></h4> 402<div class="api_metadata"> 403<span>Added in: v14.18.0</span> 404</div> 405<ul> 406<li>Type <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Either <code>'ipv4'</code> or <code>'ipv6'</code>.</li> 407</ul> 408<h4><code>socketaddress.flowlabel</code><span><a class="mark" href="#net_socketaddress_flowlabel" id="net_socketaddress_flowlabel">#</a></span></h4> 409<div class="api_metadata"> 410<span>Added in: v14.18.0</span> 411</div> 412<ul> 413<li>Type <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 414</ul> 415<h4><code>socketaddress.port</code><span><a class="mark" href="#net_socketaddress_port" id="net_socketaddress_port">#</a></span></h4> 416<div class="api_metadata"> 417<span>Added in: v14.18.0</span> 418</div> 419<ul> 420<li>Type <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 421</ul> 422</section><section><h3>Class: <code>net.Server</code><span><a class="mark" href="#net_class_net_server" id="net_class_net_server">#</a></span></h3> 423<div class="api_metadata"> 424<span>Added in: v0.1.90</span> 425</div> 426<ul> 427<li>Extends: <a href="events.html#events_class_eventemitter" class="type"><EventEmitter></a></li> 428</ul> 429<p>This class is used to create a TCP or <a href="#net_ipc_support">IPC</a> server.</p> 430<h4><code>new net.Server([options][, connectionListener])</code><span><a class="mark" href="#net_new_net_server_options_connectionlistener" id="net_new_net_server_options_connectionlistener">#</a></span></h4> 431<ul> 432<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> See 433<a href="#net_net_createserver_options_connectionlistener"><code>net.createServer([options][, connectionListener])</code></a>.</li> 434<li><code>connectionListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Automatically set as a listener for the 435<a href="#net_event_connection"><code>'connection'</code></a> event.</li> 436<li>Returns: <a href="net.html#net_class_net_server" class="type"><net.Server></a></li> 437</ul> 438<p><code>net.Server</code> is an <a href="events.html#events_class_eventemitter"><code>EventEmitter</code></a> with the following events:</p> 439<h4>Event: <code>'close'</code><span><a class="mark" href="#net_event_close" id="net_event_close">#</a></span></h4> 440<div class="api_metadata"> 441<span>Added in: v0.5.0</span> 442</div> 443<p>Emitted when the server closes. If connections exist, this 444event is not emitted until all connections are ended.</p> 445<h4>Event: <code>'connection'</code><span><a class="mark" href="#net_event_connection" id="net_event_connection">#</a></span></h4> 446<div class="api_metadata"> 447<span>Added in: v0.1.90</span> 448</div> 449<ul> 450<li><a href="net.html#net_class_net_socket" class="type"><net.Socket></a> The connection object</li> 451</ul> 452<p>Emitted when a new connection is made. <code>socket</code> is an instance of 453<code>net.Socket</code>.</p> 454<h4>Event: <code>'error'</code><span><a class="mark" href="#net_event_error" id="net_event_error">#</a></span></h4> 455<div class="api_metadata"> 456<span>Added in: v0.1.90</span> 457</div> 458<ul> 459<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 460</ul> 461<p>Emitted when an error occurs. Unlike <a href="#net_class_net_socket"><code>net.Socket</code></a>, the <a href="#net_event_close"><code>'close'</code></a> 462event will <strong>not</strong> be emitted directly following this event unless 463<a href="#net_server_close_callback"><code>server.close()</code></a> is manually called. See the example in discussion of 464<a href="#net_server_listen"><code>server.listen()</code></a>.</p> 465<h4>Event: <code>'listening'</code><span><a class="mark" href="#net_event_listening" id="net_event_listening">#</a></span></h4> 466<div class="api_metadata"> 467<span>Added in: v0.1.90</span> 468</div> 469<p>Emitted when the server has been bound after calling <a href="#net_server_listen"><code>server.listen()</code></a>.</p> 470<h4><code>server.address()</code><span><a class="mark" href="#net_server_address" id="net_server_address">#</a></span></h4> 471<div class="api_metadata"> 472<span>Added in: v0.1.90</span> 473</div> 474<ul> 475<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <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></li> 476</ul> 477<p>Returns the bound <code>address</code>, the address <code>family</code> name, and <code>port</code> of the server 478as reported by the operating system if listening on an IP socket 479(useful to find which port was assigned when getting an OS-assigned address): 480<code>{ port: 12346, family: 'IPv4', address: '127.0.0.1' }</code>.</p> 481<p>For a server listening on a pipe or Unix domain socket, the name is returned 482as a string.</p> 483<pre><code class="language-js"><span class="hljs-keyword">const</span> server = net.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">socket</span>) =></span> { 484 socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'goodbye\n'</span>); 485}).<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 486 <span class="hljs-comment">// Handle errors here.</span> 487 <span class="hljs-keyword">throw</span> err; 488}); 489 490<span class="hljs-comment">// Grab an arbitrary unused port.</span> 491server.<span class="hljs-title function_">listen</span>(<span class="hljs-function">() =></span> { 492 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'opened server on'</span>, server.<span class="hljs-title function_">address</span>()); 493});</code></pre> 494<p><code>server.address()</code> returns <code>null</code> before the <code>'listening'</code> event has been 495emitted or after calling <code>server.close()</code>.</p> 496<h4><code>server.close([callback])</code><span><a class="mark" href="#net_server_close_callback" id="net_server_close_callback">#</a></span></h4> 497<div class="api_metadata"> 498<span>Added in: v0.1.90</span> 499</div> 500<ul> 501<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Called when the server is closed.</li> 502<li>Returns: <a href="net.html#net_class_net_server" class="type"><net.Server></a></li> 503</ul> 504<p>Stops the server from accepting new connections and keeps existing 505connections. This function is asynchronous, the server is finally closed 506when all connections are ended and the server emits a <a href="#net_event_close"><code>'close'</code></a> event. 507The optional <code>callback</code> will be called once the <code>'close'</code> event occurs. Unlike 508that event, it will be called with an <code>Error</code> as its only argument if the server 509was not open when it was closed.</p> 510<h4><code>server.connections</code><span><a class="mark" href="#net_server_connections" id="net_server_connections">#</a></span></h4> 511<div class="api_metadata"> 512<span>Added in: v0.2.0</span><span>Deprecated since: v0.9.7</span> 513</div> 514<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="#net_server_getconnections_callback"><code>server.getConnections()</code></a> instead.</div><p></p> 515<ul> 516<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a></li> 517</ul> 518<p>The number of concurrent connections on the server.</p> 519<p>This becomes <code>null</code> when sending a socket to a child with 520<a href="child_process.html#child_process_child_process_fork_modulepath_args_options"><code>child_process.fork()</code></a>. To poll forks and get current number of active 521connections, use asynchronous <a href="#net_server_getconnections_callback"><code>server.getConnections()</code></a> instead.</p> 522<h4><code>server.getConnections(callback)</code><span><a class="mark" href="#net_server_getconnections_callback" id="net_server_getconnections_callback">#</a></span></h4> 523<div class="api_metadata"> 524<span>Added in: v0.9.7</span> 525</div> 526<ul> 527<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 528<li>Returns: <a href="net.html#net_class_net_server" class="type"><net.Server></a></li> 529</ul> 530<p>Asynchronously get the number of concurrent connections on the server. Works 531when sockets were sent to forks.</p> 532<p>Callback should take two arguments <code>err</code> and <code>count</code>.</p> 533<h4><code>server.listen()</code><span><a class="mark" href="#net_server_listen" id="net_server_listen">#</a></span></h4> 534<p>Start a server listening for connections. A <code>net.Server</code> can be a TCP or 535an <a href="#net_ipc_support">IPC</a> server depending on what it listens to.</p> 536<p>Possible signatures:</p> 537<!--lint disable no-undefined-references--> 538<ul> 539<li><a href="#net_server_listen_handle_backlog_callback"><code>server.listen(handle[, backlog][, callback])</code></a></li> 540<li><a href="#net_server_listen_options_callback"><code>server.listen(options[, callback])</code></a></li> 541<li><a href="#net_server_listen_path_backlog_callback"><code>server.listen(path[, backlog][, callback])</code></a> 542for <a href="#net_ipc_support">IPC</a> servers</li> 543<li> 544<a href="#net_server_listen_port_host_backlog_callback"> 545<code>server.listen([port[, host[, backlog]]][, callback])</code></a> 546for TCP servers 547</li> 548</ul> 549<!--lint enable no-undefined-references--> 550<p>This function is asynchronous. When the server starts listening, the 551<a href="#net_event_listening"><code>'listening'</code></a> event will be emitted. The last parameter <code>callback</code> 552will be added as a listener for the <a href="#net_event_listening"><code>'listening'</code></a> event.</p> 553<p>All <code>listen()</code> methods can take a <code>backlog</code> parameter to specify the maximum 554length of the queue of pending connections. The actual length will be determined 555by the OS through sysctl settings such as <code>tcp_max_syn_backlog</code> and <code>somaxconn</code> 556on Linux. The default value of this parameter is 511 (not 512).</p> 557<p>All <a href="#net_class_net_socket"><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 558details).</p> 559<p>The <code>server.listen()</code> method can be called again if and only if there was an 560error during the first <code>server.listen()</code> call or <code>server.close()</code> has been 561called. Otherwise, an <code>ERR_SERVER_ALREADY_LISTEN</code> error will be thrown.</p> 562<p>One of the most common errors raised when listening is <code>EADDRINUSE</code>. 563This happens when another server is already listening on the requested 564<code>port</code>/<code>path</code>/<code>handle</code>. One way to handle this would be to retry 565after a certain amount of time:</p> 566<pre><code class="language-js">server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> { 567 <span class="hljs-keyword">if</span> (e.<span class="hljs-property">code</span> === <span class="hljs-string">'EADDRINUSE'</span>) { 568 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Address in use, retrying...'</span>); 569 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> { 570 server.<span class="hljs-title function_">close</span>(); 571 server.<span class="hljs-title function_">listen</span>(<span class="hljs-variable constant_">PORT</span>, <span class="hljs-variable constant_">HOST</span>); 572 }, <span class="hljs-number">1000</span>); 573 } 574});</code></pre> 575<h5><code>server.listen(handle[, backlog][, callback])</code><span><a class="mark" href="#net_server_listen_handle_backlog_callback" id="net_server_listen_handle_backlog_callback">#</a></span></h5> 576<div class="api_metadata"> 577<span>Added in: v0.5.10</span> 578</div> 579<ul> 580<li><code>handle</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 581<li><code>backlog</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Common parameter of <a href="#net_server_listen"><code>server.listen()</code></a> functions</li> 582<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 583<li>Returns: <a href="net.html#net_class_net_server" class="type"><net.Server></a></li> 584</ul> 585<p>Start a server listening for connections on a given <code>handle</code> that has 586already been bound to a port, a Unix domain socket, or a Windows named pipe.</p> 587<p>The <code>handle</code> object can be either a server, a socket (anything with an 588underlying <code>_handle</code> member), or an object with an <code>fd</code> member that is a 589valid file descriptor.</p> 590<p>Listening on a file descriptor is not supported on Windows.</p> 591<h5><code>server.listen(options[, callback])</code><span><a class="mark" href="#net_server_listen_options_callback" id="net_server_listen_options_callback">#</a></span></h5> 592<div class="api_metadata"> 593<details class="changelog"><summary>History</summary> 594<table> 595<tbody><tr><th>Version</th><th>Changes</th></tr> 596<tr><td>v11.4.0</td> 597<td><p>The <code>ipv6Only</code> option is supported.</p></td></tr> 598<tr><td>v0.11.14</td> 599<td><p><span>Added in: v0.11.14</span></p></td></tr> 600</tbody></table> 601</details> 602</div> 603<ul> 604<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Required. Supports the following properties: 605<ul> 606<li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 607<li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 608<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Will be ignored if <code>port</code> is specified. See 609<a href="#net_identifying_paths_for_ipc_connections">Identifying paths for IPC connections</a>.</li> 610<li><code>backlog</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Common parameter of <a href="#net_server_listen"><code>server.listen()</code></a> 611functions.</li> 612<li><code>exclusive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li> 613<li><code>readableAll</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> For IPC servers makes the pipe readable 614for all users. <strong>Default:</strong> <code>false</code>.</li> 615<li><code>writableAll</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> For IPC servers makes the pipe writable 616for all users. <strong>Default:</strong> <code>false</code>.</li> 617<li><code>ipv6Only</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> For TCP servers, setting <code>ipv6Only</code> to <code>true</code> will 618disable dual-stack support, i.e., binding to host <code>::</code> won't make 619<code>0.0.0.0</code> be bound. <strong>Default:</strong> <code>false</code>.</li> 620</ul> 621</li> 622<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 623functions.</li> 624<li>Returns: <a href="net.html#net_class_net_server" class="type"><net.Server></a></li> 625</ul> 626<!--lint disable no-undefined-references--> 627<p>If <code>port</code> is specified, it behaves the same as 628<a href="#net_server_listen_port_host_backlog_callback"> 629<code>server.listen([port[, host[, backlog]]][, callback])</code></a>. 630Otherwise, if <code>path</code> is specified, it behaves the same as 631<a href="#net_server_listen_path_backlog_callback"><code>server.listen(path[, backlog][, callback])</code></a>. 632If none of them is specified, an error will be thrown.</p> 633<!--lint enable no-undefined-references--> 634<p>If <code>exclusive</code> is <code>false</code> (default), then cluster workers will use the same 635underlying handle, allowing connection handling duties to be shared. When 636<code>exclusive</code> is <code>true</code>, the handle is not shared, and attempted port sharing 637results in an error. An example which listens on an exclusive port is 638shown below.</p> 639<pre><code class="language-js">server.<span class="hljs-title function_">listen</span>({ 640 <span class="hljs-attr">host</span>: <span class="hljs-string">'localhost'</span>, 641 <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>, 642 <span class="hljs-attr">exclusive</span>: <span class="hljs-literal">true</span> 643});</code></pre> 644<p>Starting an IPC server as root may cause the server path to be inaccessible for 645unprivileged users. Using <code>readableAll</code> and <code>writableAll</code> will make the server 646accessible for all users.</p> 647<h5><code>server.listen(path[, backlog][, callback])</code><span><a class="mark" href="#net_server_listen_path_backlog_callback" id="net_server_listen_path_backlog_callback">#</a></span></h5> 648<div class="api_metadata"> 649<span>Added in: v0.1.90</span> 650</div> 651<ul> 652<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Path the server should listen to. See 653<a href="#net_identifying_paths_for_ipc_connections">Identifying paths for IPC connections</a>.</li> 654<li><code>backlog</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Common parameter of <a href="#net_server_listen"><code>server.listen()</code></a> functions.</li> 655<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>.</li> 656<li>Returns: <a href="net.html#net_class_net_server" class="type"><net.Server></a></li> 657</ul> 658<p>Start an <a href="#net_ipc_support">IPC</a> server listening for connections on the given <code>path</code>.</p> 659<h5><code>server.listen([port[, host[, backlog]]][, callback])</code><span><a class="mark" href="#net_server_listen_port_host_backlog_callback" id="net_server_listen_port_host_backlog_callback">#</a></span></h5> 660<div class="api_metadata"> 661<span>Added in: v0.1.90</span> 662</div> 663<ul> 664<li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 665<li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 666<li><code>backlog</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Common parameter of <a href="#net_server_listen"><code>server.listen()</code></a> functions.</li> 667<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>.</li> 668<li>Returns: <a href="net.html#net_class_net_server" class="type"><net.Server></a></li> 669</ul> 670<p>Start a TCP server listening for connections on the given <code>port</code> and <code>host</code>.</p> 671<p>If <code>port</code> is omitted or is 0, the operating system will assign an arbitrary 672unused port, which can be retrieved by using <code>server.address().port</code> 673after the <a href="#net_event_listening"><code>'listening'</code></a> event has been emitted.</p> 674<p>If <code>host</code> is omitted, the server will accept connections on the 675<a href="https://en.wikipedia.org/wiki/IPv6_address#Unspecified_address">unspecified IPv6 address</a> (<code>::</code>) when IPv6 is available, or the 676<a href="https://en.wikipedia.org/wiki/0.0.0.0">unspecified IPv4 address</a> (<code>0.0.0.0</code>) otherwise.</p> 677<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>) 678may 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> 679(<code>0.0.0.0</code>).</p> 680<h4><code>server.listening</code><span><a class="mark" href="#net_server_listening" id="net_server_listening">#</a></span></h4> 681<div class="api_metadata"> 682<span>Added in: v5.7.0</span> 683</div> 684<ul> 685<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Indicates whether or not the server is listening for connections.</li> 686</ul> 687<h4><code>server.maxConnections</code><span><a class="mark" href="#net_server_maxconnections" id="net_server_maxconnections">#</a></span></h4> 688<div class="api_metadata"> 689<span>Added in: v0.2.0</span> 690</div> 691<ul> 692<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 693</ul> 694<p>Set this property to reject connections when the server's connection count gets 695high.</p> 696<p>It is not recommended to use this option once a socket has been sent to a child 697with <a href="child_process.html#child_process_child_process_fork_modulepath_args_options"><code>child_process.fork()</code></a>.</p> 698<h4><code>server.ref()</code><span><a class="mark" href="#net_server_ref" id="net_server_ref">#</a></span></h4> 699<div class="api_metadata"> 700<span>Added in: v0.9.1</span> 701</div> 702<ul> 703<li>Returns: <a href="net.html#net_class_net_server" class="type"><net.Server></a></li> 704</ul> 705<p>Opposite of <code>unref()</code>, calling <code>ref()</code> on a previously <code>unref</code>ed server will 706<em>not</em> let the program exit if it's the only server left (the default behavior). 707If the server is <code>ref</code>ed calling <code>ref()</code> again will have no effect.</p> 708<h4><code>server.unref()</code><span><a class="mark" href="#net_server_unref" id="net_server_unref">#</a></span></h4> 709<div class="api_metadata"> 710<span>Added in: v0.9.1</span> 711</div> 712<ul> 713<li>Returns: <a href="net.html#net_class_net_server" class="type"><net.Server></a></li> 714</ul> 715<p>Calling <code>unref()</code> on a server will allow the program to exit if this is the only 716active server in the event system. If the server is already <code>unref</code>ed calling 717<code>unref()</code> again will have no effect.</p> 718</section><section><h3>Class: <code>net.Socket</code><span><a class="mark" href="#net_class_net_socket" id="net_class_net_socket">#</a></span></h3> 719<div class="api_metadata"> 720<span>Added in: v0.3.4</span> 721</div> 722<ul> 723<li>Extends: <a href="stream.html#stream_class_stream_duplex" class="type"><stream.Duplex></a></li> 724</ul> 725<p>This class is an abstraction of a TCP socket or a streaming <a href="#net_ipc_support">IPC</a> endpoint 726(uses named pipes on Windows, and Unix domain sockets otherwise). It is also 727an <a href="events.html#events_class_eventemitter"><code>EventEmitter</code></a>.</p> 728<p>A <code>net.Socket</code> can be created by the user and used directly to interact with 729a server. For example, it is returned by <a href="#net_net_createconnection"><code>net.createConnection()</code></a>, 730so the user can use it to talk to the server.</p> 731<p>It can also be created by Node.js and passed to the user when a connection 732is received. For example, it is passed to the listeners of a 733<a href="#net_event_connection"><code>'connection'</code></a> event emitted on a <a href="#net_class_net_server"><code>net.Server</code></a>, so the user can use 734it to interact with the client.</p> 735<h4><code>new net.Socket([options])</code><span><a class="mark" href="#net_new_net_socket_options" id="net_new_net_socket_options">#</a></span></h4> 736<div class="api_metadata"> 737<span>Added in: v0.3.4</span> 738</div> 739<ul> 740<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Available options are: 741<ul> 742<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> If specified, wrap around an existing socket with 743the given file descriptor, otherwise a new socket will be created.</li> 744<li><code>allowHalfOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If set to <code>false</code>, then the socket will 745automatically end the writable side when the readable side ends. See 746<a href="#net_net_createserver_options_connectionlistener"><code>net.createServer()</code></a> and the <a href="#net_event_end"><code>'end'</code></a> event for details. <strong>Default:</strong> 747<code>false</code>.</li> 748<li><code>readable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Allow reads on the socket when an <code>fd</code> is passed, 749otherwise ignored. <strong>Default:</strong> <code>false</code>.</li> 750<li><code>writable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Allow writes on the socket when an <code>fd</code> is passed, 751otherwise ignored. <strong>Default:</strong> <code>false</code>.</li> 752</ul> 753</li> 754<li>Returns: <a href="net.html#net_class_net_socket" class="type"><net.Socket></a></li> 755</ul> 756<p>Creates a new socket object.</p> 757<p>The newly created socket can be either a TCP socket or a streaming <a href="#net_ipc_support">IPC</a> 758endpoint, depending on what it <a href="#net_socket_connect"><code>connect()</code></a> to.</p> 759<h4>Event: <code>'close'</code><span><a class="mark" href="#net_event_close_1" id="net_event_close_1">#</a></span></h4> 760<div class="api_metadata"> 761<span>Added in: v0.1.90</span> 762</div> 763<ul> 764<li><code>hadError</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if the socket had a transmission error.</li> 765</ul> 766<p>Emitted once the socket is fully closed. The argument <code>hadError</code> is a boolean 767which says if the socket was closed due to a transmission error.</p> 768<h4>Event: <code>'connect'</code><span><a class="mark" href="#net_event_connect" id="net_event_connect">#</a></span></h4> 769<div class="api_metadata"> 770<span>Added in: v0.1.90</span> 771</div> 772<p>Emitted when a socket connection is successfully established. 773See <a href="#net_net_createconnection"><code>net.createConnection()</code></a>.</p> 774<h4>Event: <code>'data'</code><span><a class="mark" href="#net_event_data" id="net_event_data">#</a></span></h4> 775<div class="api_metadata"> 776<span>Added in: v0.1.90</span> 777</div> 778<ul> 779<li><a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 780</ul> 781<p>Emitted when data is received. The argument <code>data</code> will be a <code>Buffer</code> or 782<code>String</code>. Encoding of data is set by <a href="#net_socket_setencoding_encoding"><code>socket.setEncoding()</code></a>.</p> 783<p>The data will be lost if there is no listener when a <code>Socket</code> 784emits a <code>'data'</code> event.</p> 785<h4>Event: <code>'drain'</code><span><a class="mark" href="#net_event_drain" id="net_event_drain">#</a></span></h4> 786<div class="api_metadata"> 787<span>Added in: v0.1.90</span> 788</div> 789<p>Emitted when the write buffer becomes empty. Can be used to throttle uploads.</p> 790<p>See also: the return values of <code>socket.write()</code>.</p> 791<h4>Event: <code>'end'</code><span><a class="mark" href="#net_event_end" id="net_event_end">#</a></span></h4> 792<div class="api_metadata"> 793<span>Added in: v0.1.90</span> 794</div> 795<p>Emitted when the other end of the socket signals the end of transmission, thus 796ending the readable side of the socket.</p> 797<p>By default (<code>allowHalfOpen</code> is <code>false</code>) the socket will send an end of 798transmission packet back and destroy its file descriptor once it has written out 799its pending write queue. However, if <code>allowHalfOpen</code> is set to <code>true</code>, the 800socket will not automatically <a href="#net_socket_end_data_encoding_callback"><code>end()</code></a> its writable side, 801allowing the user to write arbitrary amounts of data. The user must call 802<a href="#net_socket_end_data_encoding_callback"><code>end()</code></a> explicitly to close the connection (i.e. sending a 803FIN packet back).</p> 804<h4>Event: <code>'error'</code><span><a class="mark" href="#net_event_error_1" id="net_event_error_1">#</a></span></h4> 805<div class="api_metadata"> 806<span>Added in: v0.1.90</span> 807</div> 808<ul> 809<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 810</ul> 811<p>Emitted when an error occurs. The <code>'close'</code> event will be called directly 812following this event.</p> 813<h4>Event: <code>'lookup'</code><span><a class="mark" href="#net_event_lookup" id="net_event_lookup">#</a></span></h4> 814<div class="api_metadata"> 815<details class="changelog"><summary>History</summary> 816<table> 817<tbody><tr><th>Version</th><th>Changes</th></tr> 818<tr><td>v5.10.0</td> 819<td><p>The <code>host</code> parameter is supported now.</p></td></tr> 820<tr><td>v0.11.3</td> 821<td><p><span>Added in: v0.11.3</span></p></td></tr> 822</tbody></table> 823</details> 824</div> 825<p>Emitted after resolving the host name but before connecting. 826Not applicable to Unix sockets.</p> 827<ul> 828<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#dns_dns_lookup_hostname_options_callback"><code>dns.lookup()</code></a>.</li> 829<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> 830<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#dns_dns_lookup_hostname_options_callback"><code>dns.lookup()</code></a>.</li> 831<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> 832</ul> 833<h4>Event: <code>'ready'</code><span><a class="mark" href="#net_event_ready" id="net_event_ready">#</a></span></h4> 834<div class="api_metadata"> 835<span>Added in: v9.11.0</span> 836</div> 837<p>Emitted when a socket is ready to be used.</p> 838<p>Triggered immediately after <code>'connect'</code>.</p> 839<h4>Event: <code>'timeout'</code><span><a class="mark" href="#net_event_timeout" id="net_event_timeout">#</a></span></h4> 840<div class="api_metadata"> 841<span>Added in: v0.1.90</span> 842</div> 843<p>Emitted if the socket times out from inactivity. This is only to notify that 844the socket has been idle. The user must manually close the connection.</p> 845<p>See also: <a href="#net_socket_settimeout_timeout_callback"><code>socket.setTimeout()</code></a>.</p> 846<h4><code>socket.address()</code><span><a class="mark" href="#net_socket_address" id="net_socket_address">#</a></span></h4> 847<div class="api_metadata"> 848<span>Added in: v0.1.90</span> 849</div> 850<ul> 851<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 852</ul> 853<p>Returns the bound <code>address</code>, the address <code>family</code> name and <code>port</code> of the 854socket as reported by the operating system: 855<code>{ port: 12346, family: 'IPv4', address: '127.0.0.1' }</code></p> 856<h4><code>socket.bufferSize</code><span><a class="mark" href="#net_socket_buffersize" id="net_socket_buffersize">#</a></span></h4> 857<div class="api_metadata"> 858<span>Added in: v0.3.8</span><span>Deprecated since: v14.6.0</span> 859</div> 860<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="stream.html#stream_writable_writablelength"><code>writable.writableLength</code></a> instead.</div><p></p> 861<ul> 862<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 863</ul> 864<p>This property shows the number of characters buffered for writing. The buffer 865may contain strings whose length after encoding is not yet known. So this number 866is only an approximation of the number of bytes in the buffer.</p> 867<p><code>net.Socket</code> has the property that <code>socket.write()</code> always works. This is to 868help users get up and running quickly. The computer cannot always keep up 869with the amount of data that is written to a socket. The network connection 870simply might be too slow. Node.js will internally queue up the data written to a 871socket and send it out over the wire when it is possible.</p> 872<p>The consequence of this internal buffering is that memory may grow. 873Users who experience large or growing <code>bufferSize</code> should attempt to 874"throttle" the data flows in their program with 875<a href="#net_socket_pause"><code>socket.pause()</code></a> and <a href="#net_socket_resume"><code>socket.resume()</code></a>.</p> 876<h4><code>socket.bytesRead</code><span><a class="mark" href="#net_socket_bytesread" id="net_socket_bytesread">#</a></span></h4> 877<div class="api_metadata"> 878<span>Added in: v0.5.3</span> 879</div> 880<ul> 881<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 882</ul> 883<p>The amount of received bytes.</p> 884<h4><code>socket.bytesWritten</code><span><a class="mark" href="#net_socket_byteswritten" id="net_socket_byteswritten">#</a></span></h4> 885<div class="api_metadata"> 886<span>Added in: v0.5.3</span> 887</div> 888<ul> 889<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 890</ul> 891<p>The amount of bytes sent.</p> 892<h4><code>socket.connect()</code><span><a class="mark" href="#net_socket_connect" id="net_socket_connect">#</a></span></h4> 893<p>Initiate a connection on a given socket.</p> 894<p>Possible signatures:</p> 895<ul> 896<li><a href="#net_socket_connect_options_connectlistener"><code>socket.connect(options[, connectListener])</code></a></li> 897<li><a href="#net_socket_connect_path_connectlistener"><code>socket.connect(path[, connectListener])</code></a> 898for <a href="#net_ipc_support">IPC</a> connections.</li> 899<li><a href="#net_socket_connect_port_host_connectlistener"><code>socket.connect(port[, host][, connectListener])</code></a> 900for TCP connections.</li> 901<li>Returns: <a href="net.html#net_class_net_socket" class="type"><net.Socket></a> The socket itself.</li> 902</ul> 903<p>This function is asynchronous. When the connection is established, the 904<a href="#net_event_connect"><code>'connect'</code></a> event will be emitted. If there is a problem connecting, 905instead of a <a href="#net_event_connect"><code>'connect'</code></a> event, an <a href="#net_event_error_1"><code>'error'</code></a> event will be emitted with 906the error passed to the <a href="#net_event_error_1"><code>'error'</code></a> listener. 907The last parameter <code>connectListener</code>, if supplied, will be added as a listener 908for the <a href="#net_event_connect"><code>'connect'</code></a> event <strong>once</strong>.</p> 909<p>This function should only be used for reconnecting a socket after 910<code>'close'</code> has been emitted or otherwise it may lead to undefined 911behavior.</p> 912<h5><code>socket.connect(options[, connectListener])</code><span><a class="mark" href="#net_socket_connect_options_connectlistener" id="net_socket_connect_options_connectlistener">#</a></span></h5> 913<div class="api_metadata"> 914<details class="changelog"><summary>History</summary> 915<table> 916<tbody><tr><th>Version</th><th>Changes</th></tr> 917<tr><td>v12.10.0</td> 918<td><p>Added <code>onread</code> option.</p></td></tr> 919<tr><td>v6.0.0</td> 920<td><p>The <code>hints</code> option defaults to <code>0</code> in all cases now. Previously, in the absence of the <code>family</code> option it would default to <code>dns.ADDRCONFIG | dns.V4MAPPED</code>.</p></td></tr> 921<tr><td>v5.11.0</td> 922<td><p>The <code>hints</code> option is supported now.</p></td></tr> 923<tr><td>v0.1.90</td> 924<td><p><span>Added in: v0.1.90</span></p></td></tr> 925</tbody></table> 926</details> 927</div> 928<ul> 929<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 930<li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Common parameter of <a href="#net_socket_connect"><code>socket.connect()</code></a> 931methods. Will be added as a listener for the <a href="#net_event_connect"><code>'connect'</code></a> event once.</li> 932<li>Returns: <a href="net.html#net_class_net_socket" class="type"><net.Socket></a> The socket itself.</li> 933</ul> 934<p>Initiate a connection on a given socket. Normally this method is not needed, 935the socket should be created and opened with <a href="#net_net_createconnection"><code>net.createConnection()</code></a>. Use 936this only when implementing a custom Socket.</p> 937<p>For TCP connections, available <code>options</code> are:</p> 938<ul> 939<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> 940<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> 941<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> 942<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> 943<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 944<code>0</code> indicates that both IPv4 and IPv6 addresses are allowed. <strong>Default:</strong> <code>0</code>.</li> 945<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#dns_supported_getaddrinfo_flags"><code>dns.lookup()</code> hints</a>.</li> 946<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#dns_dns_lookup_hostname_options_callback"><code>dns.lookup()</code></a>.</li> 947</ul> 948<p>For <a href="#net_ipc_support">IPC</a> connections, available <code>options</code> are:</p> 949<ul> 950<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. 951See <a href="#net_identifying_paths_for_ipc_connections">Identifying paths for IPC connections</a>. If provided, the TCP-specific 952options above are ignored.</li> 953</ul> 954<p>For both types, available <code>options</code> include:</p> 955<ul> 956<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> 957and passed to the supplied <code>callback</code> when data arrives on the socket. 958This will cause the streaming functionality to not provide any data. 959The socket will emit events like <code>'error'</code>, <code>'end'</code>, and <code>'close'</code> 960as usual. Methods like <code>pause()</code> and <code>resume()</code> will also behave as 961expected. 962<ul> 963<li><code>buffer</code> <a href="buffer.html#buffer_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 964use for storing incoming data or a function that returns such.</li> 965<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 966data. Two arguments are passed to it: the number of bytes written to 967<code>buffer</code> and a reference to <code>buffer</code>. Return <code>false</code> from this function to 968implicitly <code>pause()</code> the socket. This function will be executed in the 969global context.</li> 970</ul> 971</li> 972</ul> 973<p>Following is an example of a client using the <code>onread</code> option:</p> 974<pre><code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">'net'</span>); 975net.<span class="hljs-title function_">connect</span>({ 976 <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>, 977 <span class="hljs-attr">onread</span>: { 978 <span class="hljs-comment">// Reuses a 4KiB Buffer for every read from the socket.</span> 979 <span class="hljs-attr">buffer</span>: <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">4</span> * <span class="hljs-number">1024</span>), 980 <span class="hljs-attr">callback</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">nread, buf</span>) { 981 <span class="hljs-comment">// Received data is available in `buf` from 0 to `nread`.</span> 982 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'utf8'</span>, <span class="hljs-number">0</span>, nread)); 983 } 984 } 985});</code></pre> 986<h5><code>socket.connect(path[, connectListener])</code><span><a class="mark" href="#net_socket_connect_path_connectlistener" id="net_socket_connect_path_connectlistener">#</a></span></h5> 987<ul> 988<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Path the client should connect to. See 989<a href="#net_identifying_paths_for_ipc_connections">Identifying paths for IPC connections</a>.</li> 990<li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Common parameter of <a href="#net_socket_connect"><code>socket.connect()</code></a> 991methods. Will be added as a listener for the <a href="#net_event_connect"><code>'connect'</code></a> event once.</li> 992<li>Returns: <a href="net.html#net_class_net_socket" class="type"><net.Socket></a> The socket itself.</li> 993</ul> 994<p>Initiate an <a href="#net_ipc_support">IPC</a> connection on the given socket.</p> 995<p>Alias to 996<a href="#net_socket_connect_options_connectlistener"><code>socket.connect(options[, connectListener])</code></a> 997called with <code>{ path: path }</code> as <code>options</code>.</p> 998<h5><code>socket.connect(port[, host][, connectListener])</code><span><a class="mark" href="#net_socket_connect_port_host_connectlistener" id="net_socket_connect_port_host_connectlistener">#</a></span></h5> 999<div class="api_metadata"> 1000<span>Added in: v0.1.90</span> 1001</div> 1002<ul> 1003<li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Port the client should connect to.</li> 1004<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 client should connect to.</li> 1005<li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Common parameter of <a href="#net_socket_connect"><code>socket.connect()</code></a> 1006methods. Will be added as a listener for the <a href="#net_event_connect"><code>'connect'</code></a> event once.</li> 1007<li>Returns: <a href="net.html#net_class_net_socket" class="type"><net.Socket></a> The socket itself.</li> 1008</ul> 1009<p>Initiate a TCP connection on the given socket.</p> 1010<p>Alias to 1011<a href="#net_socket_connect_options_connectlistener"><code>socket.connect(options[, connectListener])</code></a> 1012called with <code>{port: port, host: host}</code> as <code>options</code>.</p> 1013<h4><code>socket.connecting</code><span><a class="mark" href="#net_socket_connecting" id="net_socket_connecting">#</a></span></h4> 1014<div class="api_metadata"> 1015<span>Added in: v6.1.0</span> 1016</div> 1017<ul> 1018<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1019</ul> 1020<p>If <code>true</code>, 1021<a href="#net_socket_connect_options_connectlistener"><code>socket.connect(options[, connectListener])</code></a> was 1022called and has not yet finished. It will stay <code>true</code> until the socket becomes 1023connected, then it is set to <code>false</code> and the <code>'connect'</code> event is emitted. Note 1024that the 1025<a href="#net_socket_connect_options_connectlistener"><code>socket.connect(options[, connectListener])</code></a> 1026callback is a listener for the <code>'connect'</code> event.</p> 1027<h4><code>socket.destroy([error])</code><span><a class="mark" href="#net_socket_destroy_error" id="net_socket_destroy_error">#</a></span></h4> 1028<div class="api_metadata"> 1029<span>Added in: v0.1.90</span> 1030</div> 1031<ul> 1032<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 1033<li>Returns: <a href="net.html#net_class_net_socket" class="type"><net.Socket></a></li> 1034</ul> 1035<p>Ensures that no more I/O activity happens on this socket. 1036Destroys the stream and closes the connection.</p> 1037<p>See <a href="stream.html#stream_writable_destroy_error"><code>writable.destroy()</code></a> for further details.</p> 1038<h4><code>socket.destroyed</code><span><a class="mark" href="#net_socket_destroyed" id="net_socket_destroyed">#</a></span></h4> 1039<ul> 1040<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Indicates if the connection is destroyed or not. Once a 1041connection is destroyed no further data can be transferred using it.</li> 1042</ul> 1043<p>See <a href="stream.html#stream_writable_destroyed"><code>writable.destroyed</code></a> for further details.</p> 1044<h4><code>socket.end([data[, encoding]][, callback])</code><span><a class="mark" href="#net_socket_end_data_encoding_callback" id="net_socket_end_data_encoding_callback">#</a></span></h4> 1045<div class="api_metadata"> 1046<span>Added in: v0.1.90</span> 1047</div> 1048<ul> 1049<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_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></li> 1050<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Only used when data is <code>string</code>. <strong>Default:</strong> <code>'utf8'</code>.</li> 1051<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Optional callback for when the socket is finished.</li> 1052<li>Returns: <a href="net.html#net_class_net_socket" class="type"><net.Socket></a> The socket itself.</li> 1053</ul> 1054<p>Half-closes the socket. i.e., it sends a FIN packet. It is possible the 1055server will still send some data.</p> 1056<p>See <a href="stream.html#stream_writable_end_chunk_encoding_callback"><code>writable.end()</code></a> for further details.</p> 1057<h4><code>socket.localAddress</code><span><a class="mark" href="#net_socket_localaddress" id="net_socket_localaddress">#</a></span></h4> 1058<div class="api_metadata"> 1059<span>Added in: v0.9.6</span> 1060</div> 1061<ul> 1062<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1063</ul> 1064<p>The string representation of the local IP address the remote client is 1065connecting on. For example, in a server listening on <code>'0.0.0.0'</code>, if a client 1066connects on <code>'192.168.1.1'</code>, the value of <code>socket.localAddress</code> would be 1067<code>'192.168.1.1'</code>.</p> 1068<h4><code>socket.localPort</code><span><a class="mark" href="#net_socket_localport" id="net_socket_localport">#</a></span></h4> 1069<div class="api_metadata"> 1070<span>Added in: v0.9.6</span> 1071</div> 1072<ul> 1073<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1074</ul> 1075<p>The numeric representation of the local port. For example, <code>80</code> or <code>21</code>.</p> 1076<h4><code>socket.pause()</code><span><a class="mark" href="#net_socket_pause" id="net_socket_pause">#</a></span></h4> 1077<ul> 1078<li>Returns: <a href="net.html#net_class_net_socket" class="type"><net.Socket></a> The socket itself.</li> 1079</ul> 1080<p>Pauses the reading of data. That is, <a href="#net_event_data"><code>'data'</code></a> events will not be emitted. 1081Useful to throttle back an upload.</p> 1082<h4><code>socket.pending</code><span><a class="mark" href="#net_socket_pending" id="net_socket_pending">#</a></span></h4> 1083<div class="api_metadata"> 1084<span>Added in: v11.2.0, v10.16.0</span> 1085</div> 1086<ul> 1087<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1088</ul> 1089<p>This is <code>true</code> if the socket is not connected yet, either because <code>.connect()</code> 1090has not yet been called or because it is still in the process of connecting 1091(see <a href="#net_socket_connecting"><code>socket.connecting</code></a>).</p> 1092<h4><code>socket.ref()</code><span><a class="mark" href="#net_socket_ref" id="net_socket_ref">#</a></span></h4> 1093<div class="api_metadata"> 1094<span>Added in: v0.9.1</span> 1095</div> 1096<ul> 1097<li>Returns: <a href="net.html#net_class_net_socket" class="type"><net.Socket></a> The socket itself.</li> 1098</ul> 1099<p>Opposite of <code>unref()</code>, calling <code>ref()</code> on a previously <code>unref</code>ed socket will 1100<em>not</em> let the program exit if it's the only socket left (the default behavior). 1101If the socket is <code>ref</code>ed calling <code>ref</code> again will have no effect.</p> 1102<h4><code>socket.remoteAddress</code><span><a class="mark" href="#net_socket_remoteaddress" id="net_socket_remoteaddress">#</a></span></h4> 1103<div class="api_metadata"> 1104<span>Added in: v0.5.10</span> 1105</div> 1106<ul> 1107<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1108</ul> 1109<p>The string representation of the remote IP address. For example, 1110<code>'74.125.127.100'</code> or <code>'2001:4860:a005::68'</code>. Value may be <code>undefined</code> if 1111the socket is destroyed (for example, if the client disconnected).</p> 1112<h4><code>socket.remoteFamily</code><span><a class="mark" href="#net_socket_remotefamily" id="net_socket_remotefamily">#</a></span></h4> 1113<div class="api_metadata"> 1114<span>Added in: v0.11.14</span> 1115</div> 1116<ul> 1117<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1118</ul> 1119<p>The string representation of the remote IP family. <code>'IPv4'</code> or <code>'IPv6'</code>.</p> 1120<h4><code>socket.remotePort</code><span><a class="mark" href="#net_socket_remoteport" id="net_socket_remoteport">#</a></span></h4> 1121<div class="api_metadata"> 1122<span>Added in: v0.5.10</span> 1123</div> 1124<ul> 1125<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1126</ul> 1127<p>The numeric representation of the remote port. For example, <code>80</code> or <code>21</code>.</p> 1128<h4><code>socket.resume()</code><span><a class="mark" href="#net_socket_resume" id="net_socket_resume">#</a></span></h4> 1129<ul> 1130<li>Returns: <a href="net.html#net_class_net_socket" class="type"><net.Socket></a> The socket itself.</li> 1131</ul> 1132<p>Resumes reading after a call to <a href="#net_socket_pause"><code>socket.pause()</code></a>.</p> 1133<h4><code>socket.setEncoding([encoding])</code><span><a class="mark" href="#net_socket_setencoding_encoding" id="net_socket_setencoding_encoding">#</a></span></h4> 1134<div class="api_metadata"> 1135<span>Added in: v0.1.90</span> 1136</div> 1137<ul> 1138<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1139<li>Returns: <a href="net.html#net_class_net_socket" class="type"><net.Socket></a> The socket itself.</li> 1140</ul> 1141<p>Set the encoding for the socket as a <a href="stream.html#stream_class_stream_readable">Readable Stream</a>. See 1142<a href="stream.html#stream_readable_setencoding_encoding"><code>readable.setEncoding()</code></a> for more information.</p> 1143<h4><code>socket.setKeepAlive([enable][, initialDelay])</code><span><a class="mark" href="#net_socket_setkeepalive_enable_initialdelay" id="net_socket_setkeepalive_enable_initialdelay">#</a></span></h4> 1144<div class="api_metadata"> 1145<details class="changelog"><summary>History</summary> 1146<table> 1147<tbody><tr><th>Version</th><th>Changes</th></tr> 1148<tr><td>v13.12.0, v12.17.0</td> 1149<td><p>New defaults for <code>TCP_KEEPCNT</code> and <code>TCP_KEEPINTVL</code> socket options were added.</p></td></tr> 1150<tr><td>v0.1.92</td> 1151<td><p><span>Added in: v0.1.92</span></p></td></tr> 1152</tbody></table> 1153</details> 1154</div> 1155<ul> 1156<li><code>enable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li> 1157<li><code>initialDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> <strong>Default:</strong> <code>0</code></li> 1158<li>Returns: <a href="net.html#net_class_net_socket" class="type"><net.Socket></a> The socket itself.</li> 1159</ul> 1160<p>Enable/disable keep-alive functionality, and optionally set the initial 1161delay before the first keepalive probe is sent on an idle socket.</p> 1162<p>Set <code>initialDelay</code> (in milliseconds) to set the delay between the last 1163data packet received and the first keepalive probe. Setting <code>0</code> for 1164<code>initialDelay</code> will leave the value unchanged from the default 1165(or previous) setting.</p> 1166<p>Enabling the keep-alive functionality will set the following socket options:</p> 1167<ul> 1168<li><code>SO_KEEPALIVE=1</code></li> 1169<li><code>TCP_KEEPIDLE=initialDelay</code></li> 1170<li><code>TCP_KEEPCNT=10</code></li> 1171<li><code>TCP_KEEPINTVL=1</code></li> 1172</ul> 1173<h4><code>socket.setNoDelay([noDelay])</code><span><a class="mark" href="#net_socket_setnodelay_nodelay" id="net_socket_setnodelay_nodelay">#</a></span></h4> 1174<div class="api_metadata"> 1175<span>Added in: v0.1.90</span> 1176</div> 1177<ul> 1178<li><code>noDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>true</code></li> 1179<li>Returns: <a href="net.html#net_class_net_socket" class="type"><net.Socket></a> The socket itself.</li> 1180</ul> 1181<p>Enable/disable the use of Nagle's algorithm.</p> 1182<p>When a TCP connection is created, it will have Nagle's algorithm enabled.</p> 1183<p>Nagle's algorithm delays data before it is sent via the network. It attempts 1184to optimize throughput at the expense of latency.</p> 1185<p>Passing <code>true</code> for <code>noDelay</code> or not passing an argument will disable Nagle's 1186algorithm for the socket. Passing <code>false</code> for <code>noDelay</code> will enable Nagle's 1187algorithm.</p> 1188<h4><code>socket.setTimeout(timeout[, callback])</code><span><a class="mark" href="#net_socket_settimeout_timeout_callback" id="net_socket_settimeout_timeout_callback">#</a></span></h4> 1189<div class="api_metadata"> 1190<span>Added in: v0.1.90</span> 1191</div> 1192<ul> 1193<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1194<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 1195<li>Returns: <a href="net.html#net_class_net_socket" class="type"><net.Socket></a> The socket itself.</li> 1196</ul> 1197<p>Sets the socket to timeout after <code>timeout</code> milliseconds of inactivity on 1198the socket. By default <code>net.Socket</code> do not have a timeout.</p> 1199<p>When an idle timeout is triggered the socket will receive a <a href="#net_event_timeout"><code>'timeout'</code></a> 1200event but the connection will not be severed. The user must manually call 1201<a href="#net_socket_end_data_encoding_callback"><code>socket.end()</code></a> or <a href="#net_socket_destroy_error"><code>socket.destroy()</code></a> to end the connection.</p> 1202<pre><code class="language-js">socket.<span class="hljs-built_in">setTimeout</span>(<span class="hljs-number">3000</span>); 1203socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'timeout'</span>, <span class="hljs-function">() =></span> { 1204 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'socket timeout'</span>); 1205 socket.<span class="hljs-title function_">end</span>(); 1206});</code></pre> 1207<p>If <code>timeout</code> is 0, then the existing idle timeout is disabled.</p> 1208<p>The optional <code>callback</code> parameter will be added as a one-time listener for the 1209<a href="#net_event_timeout"><code>'timeout'</code></a> event.</p> 1210<h4><code>socket.timeout</code><span><a class="mark" href="#net_socket_timeout" id="net_socket_timeout">#</a></span></h4> 1211<div class="api_metadata"> 1212<span>Added in: v10.7.0</span> 1213</div> 1214<ul> 1215<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a></li> 1216</ul> 1217<p>The socket timeout in milliseconds as set by <a href="#net_socket_settimeout_timeout_callback"><code>socket.setTimeout()</code></a>. 1218It is <code>undefined</code> if a timeout has not been set.</p> 1219<h4><code>socket.unref()</code><span><a class="mark" href="#net_socket_unref" id="net_socket_unref">#</a></span></h4> 1220<div class="api_metadata"> 1221<span>Added in: v0.9.1</span> 1222</div> 1223<ul> 1224<li>Returns: <a href="net.html#net_class_net_socket" class="type"><net.Socket></a> The socket itself.</li> 1225</ul> 1226<p>Calling <code>unref()</code> on a socket will allow the program to exit if this is the only 1227active socket in the event system. If the socket is already <code>unref</code>ed calling 1228<code>unref()</code> again will have no effect.</p> 1229<h4><code>socket.write(data[, encoding][, callback])</code><span><a class="mark" href="#net_socket_write_data_encoding_callback" id="net_socket_write_data_encoding_callback">#</a></span></h4> 1230<div class="api_metadata"> 1231<span>Added in: v0.1.90</span> 1232</div> 1233<ul> 1234<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_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></li> 1235<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Only used when data is <code>string</code>. <strong>Default:</strong> <code>utf8</code>.</li> 1236<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 1237<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1238</ul> 1239<p>Sends data on the socket. The second parameter specifies the encoding in the 1240case of a string. It defaults to UTF8 encoding.</p> 1241<p>Returns <code>true</code> if the entire data was flushed successfully to the kernel 1242buffer. Returns <code>false</code> if all or part of the data was queued in user memory. 1243<a href="#net_event_drain"><code>'drain'</code></a> will be emitted when the buffer is again free.</p> 1244<p>The optional <code>callback</code> parameter will be executed when the data is finally 1245written out, which may not be immediately.</p> 1246<p>See <code>Writable</code> stream <a href="stream.html#stream_writable_write_chunk_encoding_callback"><code>write()</code></a> method for more 1247information.</p> 1248<h4><code>socket.readyState</code><span><a class="mark" href="#net_socket_readystate" id="net_socket_readystate">#</a></span></h4> 1249<div class="api_metadata"> 1250<span>Added in: v0.5.0</span> 1251</div> 1252<ul> 1253<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1254</ul> 1255<p>This property represents the state of the connection as a string.</p> 1256<ul> 1257<li>If the stream is connecting <code>socket.readyState</code> is <code>opening</code>.</li> 1258<li>If the stream is readable and writable, it is <code>open</code>.</li> 1259<li>If the stream is readable and not writable, it is <code>readOnly</code>.</li> 1260<li>If the stream is not readable and writable, it is <code>writeOnly</code>.</li> 1261</ul> 1262</section><section><h3><code>net.connect()</code><span><a class="mark" href="#net_net_connect" id="net_net_connect">#</a></span></h3> 1263<p>Aliases to 1264<a href="#net_net_createconnection"><code>net.createConnection()</code></a>.</p> 1265<p>Possible signatures:</p> 1266<ul> 1267<li><a href="#net_net_connect_options_connectlistener"><code>net.connect(options[, connectListener])</code></a></li> 1268<li><a href="#net_net_connect_path_connectlistener"><code>net.connect(path[, connectListener])</code></a> for <a href="#net_ipc_support">IPC</a> 1269connections.</li> 1270<li><a href="#net_net_connect_port_host_connectlistener"><code>net.connect(port[, host][, connectListener])</code></a> 1271for TCP connections.</li> 1272</ul> 1273<h4><code>net.connect(options[, connectListener])</code><span><a class="mark" href="#net_net_connect_options_connectlistener" id="net_net_connect_options_connectlistener">#</a></span></h4> 1274<div class="api_metadata"> 1275<span>Added in: v0.7.0</span> 1276</div> 1277<ul> 1278<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 1279<li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 1280<li>Returns: <a href="net.html#net_class_net_socket" class="type"><net.Socket></a></li> 1281</ul> 1282<p>Alias to 1283<a href="#net_net_createconnection_options_connectlistener"><code>net.createConnection(options[, connectListener])</code></a>.</p> 1284<h4><code>net.connect(path[, connectListener])</code><span><a class="mark" href="#net_net_connect_path_connectlistener" id="net_net_connect_path_connectlistener">#</a></span></h4> 1285<div class="api_metadata"> 1286<span>Added in: v0.1.90</span> 1287</div> 1288<ul> 1289<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1290<li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 1291<li>Returns: <a href="net.html#net_class_net_socket" class="type"><net.Socket></a></li> 1292</ul> 1293<p>Alias to 1294<a href="#net_net_createconnection_path_connectlistener"><code>net.createConnection(path[, connectListener])</code></a>.</p> 1295<h4><code>net.connect(port[, host][, connectListener])</code><span><a class="mark" href="#net_net_connect_port_host_connectlistener" id="net_net_connect_port_host_connectlistener">#</a></span></h4> 1296<div class="api_metadata"> 1297<span>Added in: v0.1.90</span> 1298</div> 1299<ul> 1300<li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1301<li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1302<li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 1303<li>Returns: <a href="net.html#net_class_net_socket" class="type"><net.Socket></a></li> 1304</ul> 1305<p>Alias to 1306<a href="#net_net_createconnection_port_host_connectlistener"><code>net.createConnection(port[, host][, connectListener])</code></a>.</p> 1307</section><section><h3><code>net.createConnection()</code><span><a class="mark" href="#net_net_createconnection" id="net_net_createconnection">#</a></span></h3> 1308<p>A factory function, which creates a new <a href="#net_class_net_socket"><code>net.Socket</code></a>, 1309immediately initiates connection with <a href="#net_socket_connect"><code>socket.connect()</code></a>, 1310then returns the <code>net.Socket</code> that starts the connection.</p> 1311<p>When the connection is established, a <a href="#net_event_connect"><code>'connect'</code></a> event will be emitted 1312on the returned socket. The last parameter <code>connectListener</code>, if supplied, 1313will be added as a listener for the <a href="#net_event_connect"><code>'connect'</code></a> event <strong>once</strong>.</p> 1314<p>Possible signatures:</p> 1315<ul> 1316<li><a href="#net_net_createconnection_options_connectlistener"><code>net.createConnection(options[, connectListener])</code></a></li> 1317<li><a href="#net_net_createconnection_path_connectlistener"><code>net.createConnection(path[, connectListener])</code></a> 1318for <a href="#net_ipc_support">IPC</a> connections.</li> 1319<li><a href="#net_net_createconnection_port_host_connectlistener"><code>net.createConnection(port[, host][, connectListener])</code></a> 1320for TCP connections.</li> 1321</ul> 1322<p>The <a href="#net_net_connect"><code>net.connect()</code></a> function is an alias to this function.</p> 1323<h4><code>net.createConnection(options[, connectListener])</code><span><a class="mark" href="#net_net_createconnection_options_connectlistener" id="net_net_createconnection_options_connectlistener">#</a></span></h4> 1324<div class="api_metadata"> 1325<span>Added in: v0.1.90</span> 1326</div> 1327<ul> 1328<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Required. Will be passed to both the 1329<a href="#net_new_net_socket_options"><code>new net.Socket([options])</code></a> call and the 1330<a href="#net_socket_connect_options_connectlistener"><code>socket.connect(options[, connectListener])</code></a> 1331method.</li> 1332<li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Common parameter of the 1333<a href="#net_net_createconnection"><code>net.createConnection()</code></a> functions. If supplied, will be added as 1334a listener for the <a href="#net_event_connect"><code>'connect'</code></a> event on the returned socket once.</li> 1335<li>Returns: <a href="net.html#net_class_net_socket" class="type"><net.Socket></a> The newly created socket used to start the connection.</li> 1336</ul> 1337<p>For available options, see 1338<a href="#net_new_net_socket_options"><code>new net.Socket([options])</code></a> 1339and <a href="#net_socket_connect_options_connectlistener"><code>socket.connect(options[, connectListener])</code></a>.</p> 1340<p>Additional options:</p> 1341<ul> 1342<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 1343<a href="#net_socket_settimeout_timeout_callback"><code>socket.setTimeout(timeout)</code></a> after the socket is created, but before 1344it starts the connection.</li> 1345</ul> 1346<p>Following is an example of a client of the echo server described 1347in the <a href="#net_net_createserver_options_connectionlistener"><code>net.createServer()</code></a> section:</p> 1348<pre><code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">'net'</span>); 1349<span class="hljs-keyword">const</span> client = net.<span class="hljs-title function_">createConnection</span>({ <span class="hljs-attr">port</span>: <span class="hljs-number">8124</span> }, <span class="hljs-function">() =></span> { 1350 <span class="hljs-comment">// 'connect' listener.</span> 1351 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'connected to server!'</span>); 1352 client.<span class="hljs-title function_">write</span>(<span class="hljs-string">'world!\r\n'</span>); 1353}); 1354client.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> { 1355 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(data.<span class="hljs-title function_">toString</span>()); 1356 client.<span class="hljs-title function_">end</span>(); 1357}); 1358client.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> { 1359 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'disconnected from server'</span>); 1360});</code></pre> 1361<p>To connect on the socket <code>/tmp/echo.sock</code>:</p> 1362<pre><code class="language-js"><span class="hljs-keyword">const</span> client = net.<span class="hljs-title function_">createConnection</span>({ <span class="hljs-attr">path</span>: <span class="hljs-string">'/tmp/echo.sock'</span> });</code></pre> 1363<h4><code>net.createConnection(path[, connectListener])</code><span><a class="mark" href="#net_net_createconnection_path_connectlistener" id="net_net_createconnection_path_connectlistener">#</a></span></h4> 1364<div class="api_metadata"> 1365<span>Added in: v0.1.90</span> 1366</div> 1367<ul> 1368<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Path the socket should connect to. Will be passed to 1369<a href="#net_socket_connect_path_connectlistener"><code>socket.connect(path[, connectListener])</code></a>. 1370See <a href="#net_identifying_paths_for_ipc_connections">Identifying paths for IPC connections</a>.</li> 1371<li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Common parameter of the 1372<a href="#net_net_createconnection"><code>net.createConnection()</code></a> functions, an "once" listener for the 1373<code>'connect'</code> event on the initiating socket. Will be passed to 1374<a href="#net_socket_connect_path_connectlistener"><code>socket.connect(path[, connectListener])</code></a>.</li> 1375<li>Returns: <a href="net.html#net_class_net_socket" class="type"><net.Socket></a> The newly created socket used to start the connection.</li> 1376</ul> 1377<p>Initiates an <a href="#net_ipc_support">IPC</a> connection.</p> 1378<p>This function creates a new <a href="#net_class_net_socket"><code>net.Socket</code></a> with all options set to default, 1379immediately initiates connection with 1380<a href="#net_socket_connect_path_connectlistener"><code>socket.connect(path[, connectListener])</code></a>, 1381then returns the <code>net.Socket</code> that starts the connection.</p> 1382<h4><code>net.createConnection(port[, host][, connectListener])</code><span><a class="mark" href="#net_net_createconnection_port_host_connectlistener" id="net_net_createconnection_port_host_connectlistener">#</a></span></h4> 1383<div class="api_metadata"> 1384<span>Added in: v0.1.90</span> 1385</div> 1386<ul> 1387<li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Port the socket should connect to. Will be passed to 1388<a href="#net_socket_connect_port_host_connectlistener"><code>socket.connect(port[, host][, connectListener])</code></a>.</li> 1389<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. Will be passed to 1390<a href="#net_socket_connect_port_host_connectlistener"><code>socket.connect(port[, host][, connectListener])</code></a>. 1391<strong>Default:</strong> <code>'localhost'</code>.</li> 1392<li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Common parameter of the 1393<a href="#net_net_createconnection"><code>net.createConnection()</code></a> functions, an "once" listener for the 1394<code>'connect'</code> event on the initiating socket. Will be passed to 1395<a href="#net_socket_connect_port_host_connectlistener"><code>socket.connect(port[, host][, connectListener])</code></a>.</li> 1396<li>Returns: <a href="net.html#net_class_net_socket" class="type"><net.Socket></a> The newly created socket used to start the connection.</li> 1397</ul> 1398<p>Initiates a TCP connection.</p> 1399<p>This function creates a new <a href="#net_class_net_socket"><code>net.Socket</code></a> with all options set to default, 1400immediately initiates connection with 1401<a href="#net_socket_connect_port_host_connectlistener"><code>socket.connect(port[, host][, connectListener])</code></a>, 1402then returns the <code>net.Socket</code> that starts the connection.</p> 1403</section><section><h3><code>net.createServer([options][, connectionListener])</code><span><a class="mark" href="#net_net_createserver_options_connectionlistener" id="net_net_createserver_options_connectionlistener">#</a></span></h3> 1404<div class="api_metadata"> 1405<span>Added in: v0.5.0</span> 1406</div> 1407<ul> 1408<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1409<ul> 1410<li><code>allowHalfOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If set to <code>false</code>, then the socket will 1411automatically end the writable side when the readable side ends. 1412<strong>Default:</strong> <code>false</code>.</li> 1413<li><code>pauseOnConnect</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Indicates whether the socket should be 1414paused on incoming connections. <strong>Default:</strong> <code>false</code>.</li> 1415</ul> 1416</li> 1417<li><code>connectionListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Automatically set as a listener for the 1418<a href="#net_event_connection"><code>'connection'</code></a> event.</li> 1419<li>Returns: <a href="net.html#net_class_net_server" class="type"><net.Server></a></li> 1420</ul> 1421<p>Creates a new TCP or <a href="#net_ipc_support">IPC</a> server.</p> 1422<p>If <code>allowHalfOpen</code> is set to <code>true</code>, when the other end of the socket 1423signals the end of transmission, the server will only send back the end of 1424transmission when <a href="#net_socket_end_data_encoding_callback"><code>socket.end()</code></a> is explicitly called. For example, in the 1425context of TCP, when a FIN packed is received, a FIN packed is sent 1426back only when <a href="#net_socket_end_data_encoding_callback"><code>socket.end()</code></a> is explicitly called. Until then the 1427connection is half-closed (non-readable but still writable). See <a href="#net_event_end"><code>'end'</code></a> 1428event and <a href="https://tools.ietf.org/html/rfc1122">RFC 1122</a> (section 4.2.2.13) for more information.</p> 1429<p>If <code>pauseOnConnect</code> is set to <code>true</code>, then the socket associated with each 1430incoming connection will be paused, and no data will be read from its handle. 1431This allows connections to be passed between processes without any data being 1432read by the original process. To begin reading data from a paused socket, call 1433<a href="#net_socket_resume"><code>socket.resume()</code></a>.</p> 1434<p>The server can be a TCP server or an <a href="#net_ipc_support">IPC</a> server, depending on what it 1435<a href="#net_server_listen"><code>listen()</code></a> to.</p> 1436<p>Here is an example of an TCP echo server which listens for connections 1437on port 8124:</p> 1438<pre><code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">'net'</span>); 1439<span class="hljs-keyword">const</span> server = net.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">c</span>) =></span> { 1440 <span class="hljs-comment">// 'connection' listener.</span> 1441 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'client connected'</span>); 1442 c.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> { 1443 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'client disconnected'</span>); 1444 }); 1445 c.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello\r\n'</span>); 1446 c.<span class="hljs-title function_">pipe</span>(c); 1447}); 1448server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 1449 <span class="hljs-keyword">throw</span> err; 1450}); 1451server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8124</span>, <span class="hljs-function">() =></span> { 1452 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'server bound'</span>); 1453});</code></pre> 1454<p>Test this by using <code>telnet</code>:</p> 1455<pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">telnet localhost 8124</span></code></pre> 1456<p>To listen on the socket <code>/tmp/echo.sock</code>:</p> 1457<pre><code class="language-js">server.<span class="hljs-title function_">listen</span>(<span class="hljs-string">'/tmp/echo.sock'</span>, <span class="hljs-function">() =></span> { 1458 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'server bound'</span>); 1459});</code></pre> 1460<p>Use <code>nc</code> to connect to a Unix domain socket server:</p> 1461<pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">nc -U /tmp/echo.sock</span></code></pre> 1462</section><section><h3><code>net.isIP(input)</code><span><a class="mark" href="#net_net_isip_input" id="net_net_isip_input">#</a></span></h3> 1463<div class="api_metadata"> 1464<span>Added in: v0.3.0</span> 1465</div> 1466<ul> 1467<li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1468<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1469</ul> 1470<p>Tests if input is an IP address. Returns <code>0</code> for invalid strings, 1471returns <code>4</code> for IP version 4 addresses, and returns <code>6</code> for IP version 6 1472addresses.</p> 1473</section><section><h3><code>net.isIPv4(input)</code><span><a class="mark" href="#net_net_isipv4_input" id="net_net_isipv4_input">#</a></span></h3> 1474<div class="api_metadata"> 1475<span>Added in: v0.3.0</span> 1476</div> 1477<ul> 1478<li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1479<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1480</ul> 1481<p>Returns <code>true</code> if input is a version 4 IP address, otherwise returns <code>false</code>.</p> 1482</section><section><h3><code>net.isIPv6(input)</code><span><a class="mark" href="#net_net_isipv6_input" id="net_net_isipv6_input">#</a></span></h3> 1483<div class="api_metadata"> 1484<span>Added in: v0.3.0</span> 1485</div> 1486<ul> 1487<li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1488<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1489</ul> 1490<p>Returns <code>true</code> if input is a version 6 IP address, otherwise returns <code>false</code>.</p></section> 1491 <!-- API END --> 1492 </div> 1493 </div> 1494 </div> 1495 <script> 1496 'use strict'; 1497 { 1498 const kCustomPreference = 'customDarkTheme'; 1499 const userSettings = sessionStorage.getItem(kCustomPreference); 1500 const themeToggleButton = document.getElementById('theme-toggle-btn'); 1501 if (userSettings === null && window.matchMedia) { 1502 const mq = window.matchMedia('(prefers-color-scheme: dark)'); 1503 if ('onchange' in mq) { 1504 function mqChangeListener(e) { 1505 document.body.classList.toggle('dark-mode', e.matches); 1506 } 1507 mq.addEventListener('change', mqChangeListener); 1508 if (themeToggleButton) { 1509 themeToggleButton.addEventListener('click', function() { 1510 mq.removeEventListener('change', mqChangeListener); 1511 }, { once: true }); 1512 } 1513 } 1514 if (mq.matches) { 1515 document.body.classList.add('dark-mode'); 1516 } 1517 } else if (userSettings === 'true') { 1518 document.body.classList.add('dark-mode'); 1519 } 1520 if (themeToggleButton) { 1521 themeToggleButton.hidden = false; 1522 themeToggleButton.addEventListener('click', function() { 1523 sessionStorage.setItem( 1524 kCustomPreference, 1525 document.body.classList.toggle('dark-mode') 1526 ); 1527 }); 1528 } 1529 } 1530 </script> 1531</body> 1532</html> 1533