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