11cb0ef41Sopenharmony_ci<!DOCTYPE html> 21cb0ef41Sopenharmony_ci<html lang="en"> 31cb0ef41Sopenharmony_ci<head> 41cb0ef41Sopenharmony_ci <meta charset="utf-8"> 51cb0ef41Sopenharmony_ci <meta name="viewport" content="width=device-width"> 61cb0ef41Sopenharmony_ci <meta name="nodejs.org:node-version" content="v18.20.1"> 71cb0ef41Sopenharmony_ci <title>Net | Node.js v18.20.1 Documentation</title> 81cb0ef41Sopenharmony_ci <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 91cb0ef41Sopenharmony_ci <link rel="stylesheet" href="assets/style.css"> 101cb0ef41Sopenharmony_ci <link rel="stylesheet" href="assets/hljs.css"> 111cb0ef41Sopenharmony_ci <link rel="canonical" href="https://nodejs.org/api/net.html"> 121cb0ef41Sopenharmony_ci <script async defer src="assets/api.js" type="text/javascript"></script> 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ci</head> 151cb0ef41Sopenharmony_ci<body class="alt apidoc" id="api-section-net"> 161cb0ef41Sopenharmony_ci <div id="content" class="clearfix"> 171cb0ef41Sopenharmony_ci <div id="column2" class="interior"> 181cb0ef41Sopenharmony_ci <div id="intro" class="interior"> 191cb0ef41Sopenharmony_ci <a href="/" title="Go back to the home page"> 201cb0ef41Sopenharmony_ci Node.js 211cb0ef41Sopenharmony_ci </a> 221cb0ef41Sopenharmony_ci </div> 231cb0ef41Sopenharmony_ci <ul> 241cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 251cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 261cb0ef41Sopenharmony_ci</ul> 271cb0ef41Sopenharmony_ci<hr class="line"> 281cb0ef41Sopenharmony_ci<ul> 291cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 301cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 311cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 321cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 331cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 341cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 351cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 361cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 371cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 381cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 391cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li> 401cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 411cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 421cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 431cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 441cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 451cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li> 461cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li> 471cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li> 481cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li> 491cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li> 501cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li> 511cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li> 521cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 531cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li> 541cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 551cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 561cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 571cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 581cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 591cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 601cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net active">Net</a></li> 611cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li> 621cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li> 631cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 641cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 651cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li> 661cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 671cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 681cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li> 691cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li> 701cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li> 711cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 721cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li> 731cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 741cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li> 751cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li> 761cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 771cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 781cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li> 791cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 801cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li> 811cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li> 821cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li> 831cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li> 841cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 851cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 861cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 871cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 881cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 891cb0ef41Sopenharmony_ci</ul> 901cb0ef41Sopenharmony_ci<hr class="line"> 911cb0ef41Sopenharmony_ci<ul> 921cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 931cb0ef41Sopenharmony_ci</ul> 941cb0ef41Sopenharmony_ci </div> 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci <div id="column1" data-id="net" class="interior"> 971cb0ef41Sopenharmony_ci <header class="header"> 981cb0ef41Sopenharmony_ci <div class="header-container"> 991cb0ef41Sopenharmony_ci <h1>Node.js v18.20.1 documentation</h1> 1001cb0ef41Sopenharmony_ci <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 1011cb0ef41Sopenharmony_ci <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 1021cb0ef41Sopenharmony_ci <path fill="none" d="M0 0h24v24H0z" /> 1031cb0ef41Sopenharmony_ci <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"/> 1041cb0ef41Sopenharmony_ci <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"/> 1051cb0ef41Sopenharmony_ci </svg> 1061cb0ef41Sopenharmony_ci <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 1071cb0ef41Sopenharmony_ci <path d="M0 0h24v24H0z" fill="none" /> 1081cb0ef41Sopenharmony_ci <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"/> 1091cb0ef41Sopenharmony_ci </svg> 1101cb0ef41Sopenharmony_ci </button> 1111cb0ef41Sopenharmony_ci </div> 1121cb0ef41Sopenharmony_ci <div id="gtoc"> 1131cb0ef41Sopenharmony_ci <ul> 1141cb0ef41Sopenharmony_ci <li class="pinned-header">Node.js v18.20.1</li> 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci <li class="picker-header"> 1171cb0ef41Sopenharmony_ci <a href="#"> 1181cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 1191cb0ef41Sopenharmony_ci Table of contents 1201cb0ef41Sopenharmony_ci </a> 1211cb0ef41Sopenharmony_ci 1221cb0ef41Sopenharmony_ci <div class="picker"><div class="toc"><ul> 1231cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#net">Net</a></span> 1241cb0ef41Sopenharmony_ci<ul> 1251cb0ef41Sopenharmony_ci<li><a href="#ipc-support">IPC support</a> 1261cb0ef41Sopenharmony_ci<ul> 1271cb0ef41Sopenharmony_ci<li><a href="#identifying-paths-for-ipc-connections">Identifying paths for IPC connections</a></li> 1281cb0ef41Sopenharmony_ci</ul> 1291cb0ef41Sopenharmony_ci</li> 1301cb0ef41Sopenharmony_ci<li><a href="#class-netblocklist">Class: <code>net.BlockList</code></a> 1311cb0ef41Sopenharmony_ci<ul> 1321cb0ef41Sopenharmony_ci<li><a href="#blocklistaddaddressaddress-type"><code>blockList.addAddress(address[, type])</code></a></li> 1331cb0ef41Sopenharmony_ci<li><a href="#blocklistaddrangestart-end-type"><code>blockList.addRange(start, end[, type])</code></a></li> 1341cb0ef41Sopenharmony_ci<li><a href="#blocklistaddsubnetnet-prefix-type"><code>blockList.addSubnet(net, prefix[, type])</code></a></li> 1351cb0ef41Sopenharmony_ci<li><a href="#blocklistcheckaddress-type"><code>blockList.check(address[, type])</code></a></li> 1361cb0ef41Sopenharmony_ci<li><a href="#blocklistrules"><code>blockList.rules</code></a></li> 1371cb0ef41Sopenharmony_ci</ul> 1381cb0ef41Sopenharmony_ci</li> 1391cb0ef41Sopenharmony_ci<li><a href="#class-netsocketaddress">Class: <code>net.SocketAddress</code></a> 1401cb0ef41Sopenharmony_ci<ul> 1411cb0ef41Sopenharmony_ci<li><a href="#new-netsocketaddressoptions"><code>new net.SocketAddress([options])</code></a></li> 1421cb0ef41Sopenharmony_ci<li><a href="#socketaddressaddress"><code>socketaddress.address</code></a></li> 1431cb0ef41Sopenharmony_ci<li><a href="#socketaddressfamily"><code>socketaddress.family</code></a></li> 1441cb0ef41Sopenharmony_ci<li><a href="#socketaddressflowlabel"><code>socketaddress.flowlabel</code></a></li> 1451cb0ef41Sopenharmony_ci<li><a href="#socketaddressport"><code>socketaddress.port</code></a></li> 1461cb0ef41Sopenharmony_ci</ul> 1471cb0ef41Sopenharmony_ci</li> 1481cb0ef41Sopenharmony_ci<li><a href="#class-netserver">Class: <code>net.Server</code></a> 1491cb0ef41Sopenharmony_ci<ul> 1501cb0ef41Sopenharmony_ci<li><a href="#new-netserveroptions-connectionlistener"><code>new net.Server([options][, connectionListener])</code></a></li> 1511cb0ef41Sopenharmony_ci<li><a href="#event-close">Event: <code>'close'</code></a></li> 1521cb0ef41Sopenharmony_ci<li><a href="#event-connection">Event: <code>'connection'</code></a></li> 1531cb0ef41Sopenharmony_ci<li><a href="#event-error">Event: <code>'error'</code></a></li> 1541cb0ef41Sopenharmony_ci<li><a href="#event-listening">Event: <code>'listening'</code></a></li> 1551cb0ef41Sopenharmony_ci<li><a href="#event-drop">Event: <code>'drop'</code></a></li> 1561cb0ef41Sopenharmony_ci<li><a href="#serveraddress"><code>server.address()</code></a></li> 1571cb0ef41Sopenharmony_ci<li><a href="#serverclosecallback"><code>server.close([callback])</code></a></li> 1581cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#serversymbolasyncdispose"><code>server[Symbol.asyncDispose]()</code></a></span></li> 1591cb0ef41Sopenharmony_ci<li><a href="#servergetconnectionscallback"><code>server.getConnections(callback)</code></a></li> 1601cb0ef41Sopenharmony_ci<li><a href="#serverlisten"><code>server.listen()</code></a> 1611cb0ef41Sopenharmony_ci<ul> 1621cb0ef41Sopenharmony_ci<li><a href="#serverlistenhandle-backlog-callback"><code>server.listen(handle[, backlog][, callback])</code></a></li> 1631cb0ef41Sopenharmony_ci<li><a href="#serverlistenoptions-callback"><code>server.listen(options[, callback])</code></a></li> 1641cb0ef41Sopenharmony_ci<li><a href="#serverlistenpath-backlog-callback"><code>server.listen(path[, backlog][, callback])</code></a></li> 1651cb0ef41Sopenharmony_ci<li><a href="#serverlistenport-host-backlog-callback"><code>server.listen([port[, host[, backlog]]][, callback])</code></a></li> 1661cb0ef41Sopenharmony_ci</ul> 1671cb0ef41Sopenharmony_ci</li> 1681cb0ef41Sopenharmony_ci<li><a href="#serverlistening"><code>server.listening</code></a></li> 1691cb0ef41Sopenharmony_ci<li><a href="#servermaxconnections"><code>server.maxConnections</code></a></li> 1701cb0ef41Sopenharmony_ci<li><a href="#serverref"><code>server.ref()</code></a></li> 1711cb0ef41Sopenharmony_ci<li><a href="#serverunref"><code>server.unref()</code></a></li> 1721cb0ef41Sopenharmony_ci</ul> 1731cb0ef41Sopenharmony_ci</li> 1741cb0ef41Sopenharmony_ci<li><a href="#class-netsocket">Class: <code>net.Socket</code></a> 1751cb0ef41Sopenharmony_ci<ul> 1761cb0ef41Sopenharmony_ci<li><a href="#new-netsocketoptions"><code>new net.Socket([options])</code></a></li> 1771cb0ef41Sopenharmony_ci<li><a href="#event-close_1">Event: <code>'close'</code></a></li> 1781cb0ef41Sopenharmony_ci<li><a href="#event-connect">Event: <code>'connect'</code></a></li> 1791cb0ef41Sopenharmony_ci<li><a href="#event-data">Event: <code>'data'</code></a></li> 1801cb0ef41Sopenharmony_ci<li><a href="#event-drain">Event: <code>'drain'</code></a></li> 1811cb0ef41Sopenharmony_ci<li><a href="#event-end">Event: <code>'end'</code></a></li> 1821cb0ef41Sopenharmony_ci<li><a href="#event-error_1">Event: <code>'error'</code></a></li> 1831cb0ef41Sopenharmony_ci<li><a href="#event-lookup">Event: <code>'lookup'</code></a></li> 1841cb0ef41Sopenharmony_ci<li><a href="#event-ready">Event: <code>'ready'</code></a></li> 1851cb0ef41Sopenharmony_ci<li><a href="#event-timeout">Event: <code>'timeout'</code></a></li> 1861cb0ef41Sopenharmony_ci<li><a href="#socketaddress"><code>socket.address()</code></a></li> 1871cb0ef41Sopenharmony_ci<li><a href="#socketautoselectfamilyattemptedaddresses"><code>socket.autoSelectFamilyAttemptedAddresses</code></a></li> 1881cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#socketbuffersize"><code>socket.bufferSize</code></a></span></li> 1891cb0ef41Sopenharmony_ci<li><a href="#socketbytesread"><code>socket.bytesRead</code></a></li> 1901cb0ef41Sopenharmony_ci<li><a href="#socketbyteswritten"><code>socket.bytesWritten</code></a></li> 1911cb0ef41Sopenharmony_ci<li><a href="#socketconnect"><code>socket.connect()</code></a> 1921cb0ef41Sopenharmony_ci<ul> 1931cb0ef41Sopenharmony_ci<li><a href="#socketconnectoptions-connectlistener"><code>socket.connect(options[, connectListener])</code></a></li> 1941cb0ef41Sopenharmony_ci<li><a href="#socketconnectpath-connectlistener"><code>socket.connect(path[, connectListener])</code></a></li> 1951cb0ef41Sopenharmony_ci<li><a href="#socketconnectport-host-connectlistener"><code>socket.connect(port[, host][, connectListener])</code></a></li> 1961cb0ef41Sopenharmony_ci</ul> 1971cb0ef41Sopenharmony_ci</li> 1981cb0ef41Sopenharmony_ci<li><a href="#socketconnecting"><code>socket.connecting</code></a></li> 1991cb0ef41Sopenharmony_ci<li><a href="#socketdestroyerror"><code>socket.destroy([error])</code></a></li> 2001cb0ef41Sopenharmony_ci<li><a href="#socketdestroyed"><code>socket.destroyed</code></a></li> 2011cb0ef41Sopenharmony_ci<li><a href="#socketdestroysoon"><code>socket.destroySoon()</code></a></li> 2021cb0ef41Sopenharmony_ci<li><a href="#socketenddata-encoding-callback"><code>socket.end([data[, encoding]][, callback])</code></a></li> 2031cb0ef41Sopenharmony_ci<li><a href="#socketlocaladdress"><code>socket.localAddress</code></a></li> 2041cb0ef41Sopenharmony_ci<li><a href="#socketlocalport"><code>socket.localPort</code></a></li> 2051cb0ef41Sopenharmony_ci<li><a href="#socketlocalfamily"><code>socket.localFamily</code></a></li> 2061cb0ef41Sopenharmony_ci<li><a href="#socketpause"><code>socket.pause()</code></a></li> 2071cb0ef41Sopenharmony_ci<li><a href="#socketpending"><code>socket.pending</code></a></li> 2081cb0ef41Sopenharmony_ci<li><a href="#socketref"><code>socket.ref()</code></a></li> 2091cb0ef41Sopenharmony_ci<li><a href="#socketremoteaddress"><code>socket.remoteAddress</code></a></li> 2101cb0ef41Sopenharmony_ci<li><a href="#socketremotefamily"><code>socket.remoteFamily</code></a></li> 2111cb0ef41Sopenharmony_ci<li><a href="#socketremoteport"><code>socket.remotePort</code></a></li> 2121cb0ef41Sopenharmony_ci<li><a href="#socketresetanddestroy"><code>socket.resetAndDestroy()</code></a></li> 2131cb0ef41Sopenharmony_ci<li><a href="#socketresume"><code>socket.resume()</code></a></li> 2141cb0ef41Sopenharmony_ci<li><a href="#socketsetencodingencoding"><code>socket.setEncoding([encoding])</code></a></li> 2151cb0ef41Sopenharmony_ci<li><a href="#socketsetkeepaliveenable-initialdelay"><code>socket.setKeepAlive([enable][, initialDelay])</code></a></li> 2161cb0ef41Sopenharmony_ci<li><a href="#socketsetnodelaynodelay"><code>socket.setNoDelay([noDelay])</code></a></li> 2171cb0ef41Sopenharmony_ci<li><a href="#socketsettimeouttimeout-callback"><code>socket.setTimeout(timeout[, callback])</code></a></li> 2181cb0ef41Sopenharmony_ci<li><a href="#sockettimeout"><code>socket.timeout</code></a></li> 2191cb0ef41Sopenharmony_ci<li><a href="#socketunref"><code>socket.unref()</code></a></li> 2201cb0ef41Sopenharmony_ci<li><a href="#socketwritedata-encoding-callback"><code>socket.write(data[, encoding][, callback])</code></a></li> 2211cb0ef41Sopenharmony_ci<li><a href="#socketreadystate"><code>socket.readyState</code></a></li> 2221cb0ef41Sopenharmony_ci</ul> 2231cb0ef41Sopenharmony_ci</li> 2241cb0ef41Sopenharmony_ci<li><a href="#netconnect"><code>net.connect()</code></a> 2251cb0ef41Sopenharmony_ci<ul> 2261cb0ef41Sopenharmony_ci<li><a href="#netconnectoptions-connectlistener"><code>net.connect(options[, connectListener])</code></a></li> 2271cb0ef41Sopenharmony_ci<li><a href="#netconnectpath-connectlistener"><code>net.connect(path[, connectListener])</code></a></li> 2281cb0ef41Sopenharmony_ci<li><a href="#netconnectport-host-connectlistener"><code>net.connect(port[, host][, connectListener])</code></a></li> 2291cb0ef41Sopenharmony_ci</ul> 2301cb0ef41Sopenharmony_ci</li> 2311cb0ef41Sopenharmony_ci<li><a href="#netcreateconnection"><code>net.createConnection()</code></a> 2321cb0ef41Sopenharmony_ci<ul> 2331cb0ef41Sopenharmony_ci<li><a href="#netcreateconnectionoptions-connectlistener"><code>net.createConnection(options[, connectListener])</code></a></li> 2341cb0ef41Sopenharmony_ci<li><a href="#netcreateconnectionpath-connectlistener"><code>net.createConnection(path[, connectListener])</code></a></li> 2351cb0ef41Sopenharmony_ci<li><a href="#netcreateconnectionport-host-connectlistener"><code>net.createConnection(port[, host][, connectListener])</code></a></li> 2361cb0ef41Sopenharmony_ci</ul> 2371cb0ef41Sopenharmony_ci</li> 2381cb0ef41Sopenharmony_ci<li><a href="#netcreateserveroptions-connectionlistener"><code>net.createServer([options][, connectionListener])</code></a></li> 2391cb0ef41Sopenharmony_ci<li><a href="#netgetdefaultautoselectfamily"><code>net.getDefaultAutoSelectFamily()</code></a></li> 2401cb0ef41Sopenharmony_ci<li><a href="#netsetdefaultautoselectfamilyvalue"><code>net.setDefaultAutoSelectFamily(value)</code></a></li> 2411cb0ef41Sopenharmony_ci<li><a href="#netgetdefaultautoselectfamilyattempttimeout"><code>net.getDefaultAutoSelectFamilyAttemptTimeout()</code></a></li> 2421cb0ef41Sopenharmony_ci<li><a href="#netsetdefaultautoselectfamilyattempttimeoutvalue"><code>net.setDefaultAutoSelectFamilyAttemptTimeout(value)</code></a></li> 2431cb0ef41Sopenharmony_ci<li><a href="#netisipinput"><code>net.isIP(input)</code></a></li> 2441cb0ef41Sopenharmony_ci<li><a href="#netisipv4input"><code>net.isIPv4(input)</code></a></li> 2451cb0ef41Sopenharmony_ci<li><a href="#netisipv6input"><code>net.isIPv6(input)</code></a></li> 2461cb0ef41Sopenharmony_ci</ul> 2471cb0ef41Sopenharmony_ci</li> 2481cb0ef41Sopenharmony_ci</ul></div></div> 2491cb0ef41Sopenharmony_ci </li> 2501cb0ef41Sopenharmony_ci 2511cb0ef41Sopenharmony_ci 2521cb0ef41Sopenharmony_ci <li class="picker-header"> 2531cb0ef41Sopenharmony_ci <a href="#"> 2541cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 2551cb0ef41Sopenharmony_ci Index 2561cb0ef41Sopenharmony_ci </a> 2571cb0ef41Sopenharmony_ci 2581cb0ef41Sopenharmony_ci <div class="picker"><ul> 2591cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 2601cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 2611cb0ef41Sopenharmony_ci 2621cb0ef41Sopenharmony_ci <li> 2631cb0ef41Sopenharmony_ci <a href="index.html">Index</a> 2641cb0ef41Sopenharmony_ci </li> 2651cb0ef41Sopenharmony_ci </ul> 2661cb0ef41Sopenharmony_ci 2671cb0ef41Sopenharmony_ci<hr class="line"> 2681cb0ef41Sopenharmony_ci<ul> 2691cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 2701cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 2711cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 2721cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 2731cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 2741cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 2751cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 2761cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 2771cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 2781cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 2791cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li> 2801cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 2811cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 2821cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 2831cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 2841cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 2851cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li> 2861cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li> 2871cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li> 2881cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li> 2891cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li> 2901cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li> 2911cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li> 2921cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 2931cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li> 2941cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 2951cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 2961cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 2971cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 2981cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 2991cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 3001cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net active">Net</a></li> 3011cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li> 3021cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li> 3031cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 3041cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 3051cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li> 3061cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 3071cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 3081cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li> 3091cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li> 3101cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li> 3111cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 3121cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li> 3131cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 3141cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li> 3151cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li> 3161cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 3171cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 3181cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li> 3191cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 3201cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li> 3211cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li> 3221cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li> 3231cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li> 3241cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 3251cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 3261cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 3271cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 3281cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 3291cb0ef41Sopenharmony_ci</ul> 3301cb0ef41Sopenharmony_ci<hr class="line"> 3311cb0ef41Sopenharmony_ci<ul> 3321cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 3331cb0ef41Sopenharmony_ci</ul></div> 3341cb0ef41Sopenharmony_ci </li> 3351cb0ef41Sopenharmony_ci 3361cb0ef41Sopenharmony_ci 3371cb0ef41Sopenharmony_ci <li class="picker-header"> 3381cb0ef41Sopenharmony_ci <a href="#"> 3391cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 3401cb0ef41Sopenharmony_ci Other versions 3411cb0ef41Sopenharmony_ci </a> 3421cb0ef41Sopenharmony_ci <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/net.html">21.x</a></li> 3431cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v20.x/api/net.html">20.x <b>LTS</b></a></li> 3441cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v19.x/api/net.html">19.x</a></li> 3451cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v18.x/api/net.html">18.x <b>LTS</b></a></li> 3461cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v17.x/api/net.html">17.x</a></li> 3471cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v16.x/api/net.html">16.x</a></li> 3481cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v15.x/api/net.html">15.x</a></li> 3491cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v14.x/api/net.html">14.x</a></li> 3501cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v13.x/api/net.html">13.x</a></li> 3511cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v12.x/api/net.html">12.x</a></li> 3521cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v11.x/api/net.html">11.x</a></li> 3531cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v10.x/api/net.html">10.x</a></li> 3541cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v9.x/api/net.html">9.x</a></li> 3551cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v8.x/api/net.html">8.x</a></li> 3561cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v7.x/api/net.html">7.x</a></li> 3571cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v6.x/api/net.html">6.x</a></li> 3581cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v5.x/api/net.html">5.x</a></li> 3591cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v4.x/api/net.html">4.x</a></li> 3601cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/net.html">0.12.x</a></li> 3611cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/net.html">0.10.x</a></li></ol></div> 3621cb0ef41Sopenharmony_ci </li> 3631cb0ef41Sopenharmony_ci 3641cb0ef41Sopenharmony_ci <li class="picker-header"> 3651cb0ef41Sopenharmony_ci <a href="#"> 3661cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 3671cb0ef41Sopenharmony_ci Options 3681cb0ef41Sopenharmony_ci </a> 3691cb0ef41Sopenharmony_ci 3701cb0ef41Sopenharmony_ci <div class="picker"> 3711cb0ef41Sopenharmony_ci <ul> 3721cb0ef41Sopenharmony_ci <li> 3731cb0ef41Sopenharmony_ci <a href="all.html">View on single page</a> 3741cb0ef41Sopenharmony_ci </li> 3751cb0ef41Sopenharmony_ci <li> 3761cb0ef41Sopenharmony_ci <a href="net.json">View as JSON</a> 3771cb0ef41Sopenharmony_ci </li> 3781cb0ef41Sopenharmony_ci <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/net.md">Edit on GitHub</a></li> 3791cb0ef41Sopenharmony_ci </ul> 3801cb0ef41Sopenharmony_ci </div> 3811cb0ef41Sopenharmony_ci </li> 3821cb0ef41Sopenharmony_ci </ul> 3831cb0ef41Sopenharmony_ci </div> 3841cb0ef41Sopenharmony_ci <hr> 3851cb0ef41Sopenharmony_ci </header> 3861cb0ef41Sopenharmony_ci 3871cb0ef41Sopenharmony_ci <details id="toc" open><summary>Table of contents</summary><ul> 3881cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#net">Net</a></span> 3891cb0ef41Sopenharmony_ci<ul> 3901cb0ef41Sopenharmony_ci<li><a href="#ipc-support">IPC support</a> 3911cb0ef41Sopenharmony_ci<ul> 3921cb0ef41Sopenharmony_ci<li><a href="#identifying-paths-for-ipc-connections">Identifying paths for IPC connections</a></li> 3931cb0ef41Sopenharmony_ci</ul> 3941cb0ef41Sopenharmony_ci</li> 3951cb0ef41Sopenharmony_ci<li><a href="#class-netblocklist">Class: <code>net.BlockList</code></a> 3961cb0ef41Sopenharmony_ci<ul> 3971cb0ef41Sopenharmony_ci<li><a href="#blocklistaddaddressaddress-type"><code>blockList.addAddress(address[, type])</code></a></li> 3981cb0ef41Sopenharmony_ci<li><a href="#blocklistaddrangestart-end-type"><code>blockList.addRange(start, end[, type])</code></a></li> 3991cb0ef41Sopenharmony_ci<li><a href="#blocklistaddsubnetnet-prefix-type"><code>blockList.addSubnet(net, prefix[, type])</code></a></li> 4001cb0ef41Sopenharmony_ci<li><a href="#blocklistcheckaddress-type"><code>blockList.check(address[, type])</code></a></li> 4011cb0ef41Sopenharmony_ci<li><a href="#blocklistrules"><code>blockList.rules</code></a></li> 4021cb0ef41Sopenharmony_ci</ul> 4031cb0ef41Sopenharmony_ci</li> 4041cb0ef41Sopenharmony_ci<li><a href="#class-netsocketaddress">Class: <code>net.SocketAddress</code></a> 4051cb0ef41Sopenharmony_ci<ul> 4061cb0ef41Sopenharmony_ci<li><a href="#new-netsocketaddressoptions"><code>new net.SocketAddress([options])</code></a></li> 4071cb0ef41Sopenharmony_ci<li><a href="#socketaddressaddress"><code>socketaddress.address</code></a></li> 4081cb0ef41Sopenharmony_ci<li><a href="#socketaddressfamily"><code>socketaddress.family</code></a></li> 4091cb0ef41Sopenharmony_ci<li><a href="#socketaddressflowlabel"><code>socketaddress.flowlabel</code></a></li> 4101cb0ef41Sopenharmony_ci<li><a href="#socketaddressport"><code>socketaddress.port</code></a></li> 4111cb0ef41Sopenharmony_ci</ul> 4121cb0ef41Sopenharmony_ci</li> 4131cb0ef41Sopenharmony_ci<li><a href="#class-netserver">Class: <code>net.Server</code></a> 4141cb0ef41Sopenharmony_ci<ul> 4151cb0ef41Sopenharmony_ci<li><a href="#new-netserveroptions-connectionlistener"><code>new net.Server([options][, connectionListener])</code></a></li> 4161cb0ef41Sopenharmony_ci<li><a href="#event-close">Event: <code>'close'</code></a></li> 4171cb0ef41Sopenharmony_ci<li><a href="#event-connection">Event: <code>'connection'</code></a></li> 4181cb0ef41Sopenharmony_ci<li><a href="#event-error">Event: <code>'error'</code></a></li> 4191cb0ef41Sopenharmony_ci<li><a href="#event-listening">Event: <code>'listening'</code></a></li> 4201cb0ef41Sopenharmony_ci<li><a href="#event-drop">Event: <code>'drop'</code></a></li> 4211cb0ef41Sopenharmony_ci<li><a href="#serveraddress"><code>server.address()</code></a></li> 4221cb0ef41Sopenharmony_ci<li><a href="#serverclosecallback"><code>server.close([callback])</code></a></li> 4231cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#serversymbolasyncdispose"><code>server[Symbol.asyncDispose]()</code></a></span></li> 4241cb0ef41Sopenharmony_ci<li><a href="#servergetconnectionscallback"><code>server.getConnections(callback)</code></a></li> 4251cb0ef41Sopenharmony_ci<li><a href="#serverlisten"><code>server.listen()</code></a> 4261cb0ef41Sopenharmony_ci<ul> 4271cb0ef41Sopenharmony_ci<li><a href="#serverlistenhandle-backlog-callback"><code>server.listen(handle[, backlog][, callback])</code></a></li> 4281cb0ef41Sopenharmony_ci<li><a href="#serverlistenoptions-callback"><code>server.listen(options[, callback])</code></a></li> 4291cb0ef41Sopenharmony_ci<li><a href="#serverlistenpath-backlog-callback"><code>server.listen(path[, backlog][, callback])</code></a></li> 4301cb0ef41Sopenharmony_ci<li><a href="#serverlistenport-host-backlog-callback"><code>server.listen([port[, host[, backlog]]][, callback])</code></a></li> 4311cb0ef41Sopenharmony_ci</ul> 4321cb0ef41Sopenharmony_ci</li> 4331cb0ef41Sopenharmony_ci<li><a href="#serverlistening"><code>server.listening</code></a></li> 4341cb0ef41Sopenharmony_ci<li><a href="#servermaxconnections"><code>server.maxConnections</code></a></li> 4351cb0ef41Sopenharmony_ci<li><a href="#serverref"><code>server.ref()</code></a></li> 4361cb0ef41Sopenharmony_ci<li><a href="#serverunref"><code>server.unref()</code></a></li> 4371cb0ef41Sopenharmony_ci</ul> 4381cb0ef41Sopenharmony_ci</li> 4391cb0ef41Sopenharmony_ci<li><a href="#class-netsocket">Class: <code>net.Socket</code></a> 4401cb0ef41Sopenharmony_ci<ul> 4411cb0ef41Sopenharmony_ci<li><a href="#new-netsocketoptions"><code>new net.Socket([options])</code></a></li> 4421cb0ef41Sopenharmony_ci<li><a href="#event-close_1">Event: <code>'close'</code></a></li> 4431cb0ef41Sopenharmony_ci<li><a href="#event-connect">Event: <code>'connect'</code></a></li> 4441cb0ef41Sopenharmony_ci<li><a href="#event-data">Event: <code>'data'</code></a></li> 4451cb0ef41Sopenharmony_ci<li><a href="#event-drain">Event: <code>'drain'</code></a></li> 4461cb0ef41Sopenharmony_ci<li><a href="#event-end">Event: <code>'end'</code></a></li> 4471cb0ef41Sopenharmony_ci<li><a href="#event-error_1">Event: <code>'error'</code></a></li> 4481cb0ef41Sopenharmony_ci<li><a href="#event-lookup">Event: <code>'lookup'</code></a></li> 4491cb0ef41Sopenharmony_ci<li><a href="#event-ready">Event: <code>'ready'</code></a></li> 4501cb0ef41Sopenharmony_ci<li><a href="#event-timeout">Event: <code>'timeout'</code></a></li> 4511cb0ef41Sopenharmony_ci<li><a href="#socketaddress"><code>socket.address()</code></a></li> 4521cb0ef41Sopenharmony_ci<li><a href="#socketautoselectfamilyattemptedaddresses"><code>socket.autoSelectFamilyAttemptedAddresses</code></a></li> 4531cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#socketbuffersize"><code>socket.bufferSize</code></a></span></li> 4541cb0ef41Sopenharmony_ci<li><a href="#socketbytesread"><code>socket.bytesRead</code></a></li> 4551cb0ef41Sopenharmony_ci<li><a href="#socketbyteswritten"><code>socket.bytesWritten</code></a></li> 4561cb0ef41Sopenharmony_ci<li><a href="#socketconnect"><code>socket.connect()</code></a> 4571cb0ef41Sopenharmony_ci<ul> 4581cb0ef41Sopenharmony_ci<li><a href="#socketconnectoptions-connectlistener"><code>socket.connect(options[, connectListener])</code></a></li> 4591cb0ef41Sopenharmony_ci<li><a href="#socketconnectpath-connectlistener"><code>socket.connect(path[, connectListener])</code></a></li> 4601cb0ef41Sopenharmony_ci<li><a href="#socketconnectport-host-connectlistener"><code>socket.connect(port[, host][, connectListener])</code></a></li> 4611cb0ef41Sopenharmony_ci</ul> 4621cb0ef41Sopenharmony_ci</li> 4631cb0ef41Sopenharmony_ci<li><a href="#socketconnecting"><code>socket.connecting</code></a></li> 4641cb0ef41Sopenharmony_ci<li><a href="#socketdestroyerror"><code>socket.destroy([error])</code></a></li> 4651cb0ef41Sopenharmony_ci<li><a href="#socketdestroyed"><code>socket.destroyed</code></a></li> 4661cb0ef41Sopenharmony_ci<li><a href="#socketdestroysoon"><code>socket.destroySoon()</code></a></li> 4671cb0ef41Sopenharmony_ci<li><a href="#socketenddata-encoding-callback"><code>socket.end([data[, encoding]][, callback])</code></a></li> 4681cb0ef41Sopenharmony_ci<li><a href="#socketlocaladdress"><code>socket.localAddress</code></a></li> 4691cb0ef41Sopenharmony_ci<li><a href="#socketlocalport"><code>socket.localPort</code></a></li> 4701cb0ef41Sopenharmony_ci<li><a href="#socketlocalfamily"><code>socket.localFamily</code></a></li> 4711cb0ef41Sopenharmony_ci<li><a href="#socketpause"><code>socket.pause()</code></a></li> 4721cb0ef41Sopenharmony_ci<li><a href="#socketpending"><code>socket.pending</code></a></li> 4731cb0ef41Sopenharmony_ci<li><a href="#socketref"><code>socket.ref()</code></a></li> 4741cb0ef41Sopenharmony_ci<li><a href="#socketremoteaddress"><code>socket.remoteAddress</code></a></li> 4751cb0ef41Sopenharmony_ci<li><a href="#socketremotefamily"><code>socket.remoteFamily</code></a></li> 4761cb0ef41Sopenharmony_ci<li><a href="#socketremoteport"><code>socket.remotePort</code></a></li> 4771cb0ef41Sopenharmony_ci<li><a href="#socketresetanddestroy"><code>socket.resetAndDestroy()</code></a></li> 4781cb0ef41Sopenharmony_ci<li><a href="#socketresume"><code>socket.resume()</code></a></li> 4791cb0ef41Sopenharmony_ci<li><a href="#socketsetencodingencoding"><code>socket.setEncoding([encoding])</code></a></li> 4801cb0ef41Sopenharmony_ci<li><a href="#socketsetkeepaliveenable-initialdelay"><code>socket.setKeepAlive([enable][, initialDelay])</code></a></li> 4811cb0ef41Sopenharmony_ci<li><a href="#socketsetnodelaynodelay"><code>socket.setNoDelay([noDelay])</code></a></li> 4821cb0ef41Sopenharmony_ci<li><a href="#socketsettimeouttimeout-callback"><code>socket.setTimeout(timeout[, callback])</code></a></li> 4831cb0ef41Sopenharmony_ci<li><a href="#sockettimeout"><code>socket.timeout</code></a></li> 4841cb0ef41Sopenharmony_ci<li><a href="#socketunref"><code>socket.unref()</code></a></li> 4851cb0ef41Sopenharmony_ci<li><a href="#socketwritedata-encoding-callback"><code>socket.write(data[, encoding][, callback])</code></a></li> 4861cb0ef41Sopenharmony_ci<li><a href="#socketreadystate"><code>socket.readyState</code></a></li> 4871cb0ef41Sopenharmony_ci</ul> 4881cb0ef41Sopenharmony_ci</li> 4891cb0ef41Sopenharmony_ci<li><a href="#netconnect"><code>net.connect()</code></a> 4901cb0ef41Sopenharmony_ci<ul> 4911cb0ef41Sopenharmony_ci<li><a href="#netconnectoptions-connectlistener"><code>net.connect(options[, connectListener])</code></a></li> 4921cb0ef41Sopenharmony_ci<li><a href="#netconnectpath-connectlistener"><code>net.connect(path[, connectListener])</code></a></li> 4931cb0ef41Sopenharmony_ci<li><a href="#netconnectport-host-connectlistener"><code>net.connect(port[, host][, connectListener])</code></a></li> 4941cb0ef41Sopenharmony_ci</ul> 4951cb0ef41Sopenharmony_ci</li> 4961cb0ef41Sopenharmony_ci<li><a href="#netcreateconnection"><code>net.createConnection()</code></a> 4971cb0ef41Sopenharmony_ci<ul> 4981cb0ef41Sopenharmony_ci<li><a href="#netcreateconnectionoptions-connectlistener"><code>net.createConnection(options[, connectListener])</code></a></li> 4991cb0ef41Sopenharmony_ci<li><a href="#netcreateconnectionpath-connectlistener"><code>net.createConnection(path[, connectListener])</code></a></li> 5001cb0ef41Sopenharmony_ci<li><a href="#netcreateconnectionport-host-connectlistener"><code>net.createConnection(port[, host][, connectListener])</code></a></li> 5011cb0ef41Sopenharmony_ci</ul> 5021cb0ef41Sopenharmony_ci</li> 5031cb0ef41Sopenharmony_ci<li><a href="#netcreateserveroptions-connectionlistener"><code>net.createServer([options][, connectionListener])</code></a></li> 5041cb0ef41Sopenharmony_ci<li><a href="#netgetdefaultautoselectfamily"><code>net.getDefaultAutoSelectFamily()</code></a></li> 5051cb0ef41Sopenharmony_ci<li><a href="#netsetdefaultautoselectfamilyvalue"><code>net.setDefaultAutoSelectFamily(value)</code></a></li> 5061cb0ef41Sopenharmony_ci<li><a href="#netgetdefaultautoselectfamilyattempttimeout"><code>net.getDefaultAutoSelectFamilyAttemptTimeout()</code></a></li> 5071cb0ef41Sopenharmony_ci<li><a href="#netsetdefaultautoselectfamilyattempttimeoutvalue"><code>net.setDefaultAutoSelectFamilyAttemptTimeout(value)</code></a></li> 5081cb0ef41Sopenharmony_ci<li><a href="#netisipinput"><code>net.isIP(input)</code></a></li> 5091cb0ef41Sopenharmony_ci<li><a href="#netisipv4input"><code>net.isIPv4(input)</code></a></li> 5101cb0ef41Sopenharmony_ci<li><a href="#netisipv6input"><code>net.isIPv6(input)</code></a></li> 5111cb0ef41Sopenharmony_ci</ul> 5121cb0ef41Sopenharmony_ci</li> 5131cb0ef41Sopenharmony_ci</ul></details> 5141cb0ef41Sopenharmony_ci 5151cb0ef41Sopenharmony_ci <div id="apicontent"> 5161cb0ef41Sopenharmony_ci <h2>Net<span><a class="mark" href="#net" id="net">#</a></span><a aria-hidden="true" class="legacy" id="net_net"></a></h2> 5171cb0ef41Sopenharmony_ci 5181cb0ef41Sopenharmony_ci<!--lint disable maximum-line-length--> 5191cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> 5201cb0ef41Sopenharmony_ci<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/net.js">lib/net.js</a></p> 5211cb0ef41Sopenharmony_ci<p>The <code>node:net</code> module provides an asynchronous network API for creating stream-based 5221cb0ef41Sopenharmony_ciTCP or <a href="#ipc-support">IPC</a> servers (<a href="#netcreateserveroptions-connectionlistener"><code>net.createServer()</code></a>) and clients 5231cb0ef41Sopenharmony_ci(<a href="#netcreateconnection"><code>net.createConnection()</code></a>).</p> 5241cb0ef41Sopenharmony_ci<p>It can be accessed using:</p> 5251cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:net'</span>);</code> <button class="copy-button">copy</button></pre> 5261cb0ef41Sopenharmony_ci<section><h3>IPC support<span><a class="mark" href="#ipc-support" id="ipc-support">#</a></span><a aria-hidden="true" class="legacy" id="net_ipc_support"></a></h3> 5271cb0ef41Sopenharmony_ci<p>The <code>node:net</code> module supports IPC with named pipes on Windows, and Unix domain 5281cb0ef41Sopenharmony_cisockets on other operating systems.</p> 5291cb0ef41Sopenharmony_ci<h4>Identifying paths for IPC connections<span><a class="mark" href="#identifying-paths-for-ipc-connections" id="identifying-paths-for-ipc-connections">#</a></span><a aria-hidden="true" class="legacy" id="net_identifying_paths_for_ipc_connections"></a></h4> 5301cb0ef41Sopenharmony_ci<p><a href="#netconnect"><code>net.connect()</code></a>, <a href="#netcreateconnection"><code>net.createConnection()</code></a>, <a href="#serverlisten"><code>server.listen()</code></a>, and 5311cb0ef41Sopenharmony_ci<a href="#socketconnect"><code>socket.connect()</code></a> take a <code>path</code> parameter to identify IPC endpoints.</p> 5321cb0ef41Sopenharmony_ci<p>On Unix, the local domain is also known as the Unix domain. The path is a 5331cb0ef41Sopenharmony_cifile system pathname. It gets truncated to an OS-dependent length of 5341cb0ef41Sopenharmony_ci<code>sizeof(sockaddr_un.sun_path) - 1</code>. Typical values are 107 bytes on Linux and 5351cb0ef41Sopenharmony_ci103 bytes on macOS. If a Node.js API abstraction creates the Unix domain socket, 5361cb0ef41Sopenharmony_ciit will unlink the Unix domain socket as well. For example, 5371cb0ef41Sopenharmony_ci<a href="#netcreateserveroptions-connectionlistener"><code>net.createServer()</code></a> may create a Unix domain socket and 5381cb0ef41Sopenharmony_ci<a href="#serverclosecallback"><code>server.close()</code></a> will unlink it. But if a user creates the Unix domain 5391cb0ef41Sopenharmony_cisocket outside of these abstractions, the user will need to remove it. The same 5401cb0ef41Sopenharmony_ciapplies when a Node.js API creates a Unix domain socket but the program then 5411cb0ef41Sopenharmony_cicrashes. In short, a Unix domain socket will be visible in the file system and 5421cb0ef41Sopenharmony_ciwill persist until unlinked.</p> 5431cb0ef41Sopenharmony_ci<p>On Windows, the local domain is implemented using a named pipe. The path <em>must</em> 5441cb0ef41Sopenharmony_cirefer to an entry in <code>\\?\pipe\</code> or <code>\\.\pipe\</code>. Any characters are permitted, 5451cb0ef41Sopenharmony_cibut the latter may do some processing of pipe names, such as resolving <code>..</code> 5461cb0ef41Sopenharmony_cisequences. Despite how it might look, the pipe namespace is flat. Pipes will 5471cb0ef41Sopenharmony_ci<em>not persist</em>. They are removed when the last reference to them is closed. 5481cb0ef41Sopenharmony_ciUnlike Unix domain sockets, Windows will close and remove the pipe when the 5491cb0ef41Sopenharmony_ciowning process exits.</p> 5501cb0ef41Sopenharmony_ci<p>JavaScript string escaping requires paths to be specified with extra backslash 5511cb0ef41Sopenharmony_ciescaping such as:</p> 5521cb0ef41Sopenharmony_ci<pre><code class="language-js">net.<span class="hljs-title function_">createServer</span>().<span class="hljs-title function_">listen</span>( 5531cb0ef41Sopenharmony_ci 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> <button class="copy-button">copy</button></pre> 5541cb0ef41Sopenharmony_ci</section><section><h3>Class: <code>net.BlockList</code><span><a class="mark" href="#class-netblocklist" id="class-netblocklist">#</a></span><a aria-hidden="true" class="legacy" id="net_class_net_blocklist"></a></h3> 5551cb0ef41Sopenharmony_ci<div class="api_metadata"> 5561cb0ef41Sopenharmony_ci<span>Added in: v15.0.0, v14.18.0</span> 5571cb0ef41Sopenharmony_ci</div> 5581cb0ef41Sopenharmony_ci<p>The <code>BlockList</code> object can be used with some network APIs to specify rules for 5591cb0ef41Sopenharmony_cidisabling inbound or outbound access to specific IP addresses, IP ranges, or 5601cb0ef41Sopenharmony_ciIP subnets.</p> 5611cb0ef41Sopenharmony_ci<h4><code>blockList.addAddress(address[, type])</code><span><a class="mark" href="#blocklistaddaddressaddress-type" id="blocklistaddaddressaddress-type">#</a></span><a aria-hidden="true" class="legacy" id="net_blocklist_addaddress_address_type"></a></h4> 5621cb0ef41Sopenharmony_ci<div class="api_metadata"> 5631cb0ef41Sopenharmony_ci<span>Added in: v15.0.0, v14.18.0</span> 5641cb0ef41Sopenharmony_ci</div> 5651cb0ef41Sopenharmony_ci<ul> 5661cb0ef41Sopenharmony_ci<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#class-netsocketaddress" class="type"><net.SocketAddress></a> An IPv4 or IPv6 address.</li> 5671cb0ef41Sopenharmony_ci<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> 5681cb0ef41Sopenharmony_ci</ul> 5691cb0ef41Sopenharmony_ci<p>Adds a rule to block the given IP address.</p> 5701cb0ef41Sopenharmony_ci<h4><code>blockList.addRange(start, end[, type])</code><span><a class="mark" href="#blocklistaddrangestart-end-type" id="blocklistaddrangestart-end-type">#</a></span><a aria-hidden="true" class="legacy" id="net_blocklist_addrange_start_end_type"></a></h4> 5711cb0ef41Sopenharmony_ci<div class="api_metadata"> 5721cb0ef41Sopenharmony_ci<span>Added in: v15.0.0, v14.18.0</span> 5731cb0ef41Sopenharmony_ci</div> 5741cb0ef41Sopenharmony_ci<ul> 5751cb0ef41Sopenharmony_ci<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#class-netsocketaddress" class="type"><net.SocketAddress></a> The starting IPv4 or IPv6 address in the 5761cb0ef41Sopenharmony_cirange.</li> 5771cb0ef41Sopenharmony_ci<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#class-netsocketaddress" class="type"><net.SocketAddress></a> The ending IPv4 or IPv6 address in the range.</li> 5781cb0ef41Sopenharmony_ci<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> 5791cb0ef41Sopenharmony_ci</ul> 5801cb0ef41Sopenharmony_ci<p>Adds a rule to block a range of IP addresses from <code>start</code> (inclusive) to 5811cb0ef41Sopenharmony_ci<code>end</code> (inclusive).</p> 5821cb0ef41Sopenharmony_ci<h4><code>blockList.addSubnet(net, prefix[, type])</code><span><a class="mark" href="#blocklistaddsubnetnet-prefix-type" id="blocklistaddsubnetnet-prefix-type">#</a></span><a aria-hidden="true" class="legacy" id="net_blocklist_addsubnet_net_prefix_type"></a></h4> 5831cb0ef41Sopenharmony_ci<div class="api_metadata"> 5841cb0ef41Sopenharmony_ci<span>Added in: v15.0.0, v14.18.0</span> 5851cb0ef41Sopenharmony_ci</div> 5861cb0ef41Sopenharmony_ci<ul> 5871cb0ef41Sopenharmony_ci<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#class-netsocketaddress" class="type"><net.SocketAddress></a> The network IPv4 or IPv6 address.</li> 5881cb0ef41Sopenharmony_ci<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 5891cb0ef41Sopenharmony_cimust be a value between <code>0</code> and <code>32</code>. For IPv6, this must be between 5901cb0ef41Sopenharmony_ci<code>0</code> and <code>128</code>.</li> 5911cb0ef41Sopenharmony_ci<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> 5921cb0ef41Sopenharmony_ci</ul> 5931cb0ef41Sopenharmony_ci<p>Adds a rule to block a range of IP addresses specified as a subnet mask.</p> 5941cb0ef41Sopenharmony_ci<h4><code>blockList.check(address[, type])</code><span><a class="mark" href="#blocklistcheckaddress-type" id="blocklistcheckaddress-type">#</a></span><a aria-hidden="true" class="legacy" id="net_blocklist_check_address_type"></a></h4> 5951cb0ef41Sopenharmony_ci<div class="api_metadata"> 5961cb0ef41Sopenharmony_ci<span>Added in: v15.0.0, v14.18.0</span> 5971cb0ef41Sopenharmony_ci</div> 5981cb0ef41Sopenharmony_ci<ul> 5991cb0ef41Sopenharmony_ci<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#class-netsocketaddress" class="type"><net.SocketAddress></a> The IP address to check</li> 6001cb0ef41Sopenharmony_ci<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> 6011cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 6021cb0ef41Sopenharmony_ci</ul> 6031cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if the given IP address matches any of the rules added to the 6041cb0ef41Sopenharmony_ci<code>BlockList</code>.</p> 6051cb0ef41Sopenharmony_ci<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>(); 6061cb0ef41Sopenharmony_ciblockList.<span class="hljs-title function_">addAddress</span>(<span class="hljs-string">'123.123.123.123'</span>); 6071cb0ef41Sopenharmony_ciblockList.<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>); 6081cb0ef41Sopenharmony_ciblockList.<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>); 6091cb0ef41Sopenharmony_ci 6101cb0ef41Sopenharmony_ci<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> 6111cb0ef41Sopenharmony_ci<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> 6121cb0ef41Sopenharmony_ci<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> 6131cb0ef41Sopenharmony_ci 6141cb0ef41Sopenharmony_ci<span class="hljs-comment">// IPv6 notation for IPv4 addresses works:</span> 6151cb0ef41Sopenharmony_ci<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> 6161cb0ef41Sopenharmony_ci<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> <button class="copy-button">copy</button></pre> 6171cb0ef41Sopenharmony_ci<h4><code>blockList.rules</code><span><a class="mark" href="#blocklistrules" id="blocklistrules">#</a></span><a aria-hidden="true" class="legacy" id="net_blocklist_rules"></a></h4> 6181cb0ef41Sopenharmony_ci<div class="api_metadata"> 6191cb0ef41Sopenharmony_ci<span>Added in: v15.0.0, v14.18.0</span> 6201cb0ef41Sopenharmony_ci</div> 6211cb0ef41Sopenharmony_ci<ul> 6221cb0ef41Sopenharmony_ci<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 6231cb0ef41Sopenharmony_ci</ul> 6241cb0ef41Sopenharmony_ci<p>The list of rules added to the blocklist.</p> 6251cb0ef41Sopenharmony_ci</section><section><h3>Class: <code>net.SocketAddress</code><span><a class="mark" href="#class-netsocketaddress" id="class-netsocketaddress">#</a></span><a aria-hidden="true" class="legacy" id="net_class_net_socketaddress"></a></h3> 6261cb0ef41Sopenharmony_ci<div class="api_metadata"> 6271cb0ef41Sopenharmony_ci<span>Added in: v15.14.0, v14.18.0</span> 6281cb0ef41Sopenharmony_ci</div> 6291cb0ef41Sopenharmony_ci<h4><code>new net.SocketAddress([options])</code><span><a class="mark" href="#new-netsocketaddressoptions" id="new-netsocketaddressoptions">#</a></span><a aria-hidden="true" class="legacy" id="net_new_net_socketaddress_options"></a></h4> 6301cb0ef41Sopenharmony_ci<div class="api_metadata"> 6311cb0ef41Sopenharmony_ci<span>Added in: v15.14.0, v14.18.0</span> 6321cb0ef41Sopenharmony_ci</div> 6331cb0ef41Sopenharmony_ci<ul> 6341cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 6351cb0ef41Sopenharmony_ci<ul> 6361cb0ef41Sopenharmony_ci<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. 6371cb0ef41Sopenharmony_ci<strong>Default</strong>: <code>'127.0.0.1'</code> if <code>family</code> is <code>'ipv4'</code>; <code>'::'</code> if <code>family</code> is 6381cb0ef41Sopenharmony_ci<code>'ipv6'</code>.</li> 6391cb0ef41Sopenharmony_ci<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 <code>'ipv6'</code>. 6401cb0ef41Sopenharmony_ci<strong>Default</strong>: <code>'ipv4'</code>.</li> 6411cb0ef41Sopenharmony_ci<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> 6421cb0ef41Sopenharmony_ci<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> 6431cb0ef41Sopenharmony_ci</ul> 6441cb0ef41Sopenharmony_ci</li> 6451cb0ef41Sopenharmony_ci</ul> 6461cb0ef41Sopenharmony_ci<h4><code>socketaddress.address</code><span><a class="mark" href="#socketaddressaddress" id="socketaddressaddress">#</a></span><a aria-hidden="true" class="legacy" id="net_socketaddress_address"></a></h4> 6471cb0ef41Sopenharmony_ci<div class="api_metadata"> 6481cb0ef41Sopenharmony_ci<span>Added in: v15.14.0, v14.18.0</span> 6491cb0ef41Sopenharmony_ci</div> 6501cb0ef41Sopenharmony_ci<ul> 6511cb0ef41Sopenharmony_ci<li>Type <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 6521cb0ef41Sopenharmony_ci</ul> 6531cb0ef41Sopenharmony_ci<h4><code>socketaddress.family</code><span><a class="mark" href="#socketaddressfamily" id="socketaddressfamily">#</a></span><a aria-hidden="true" class="legacy" id="net_socketaddress_family"></a></h4> 6541cb0ef41Sopenharmony_ci<div class="api_metadata"> 6551cb0ef41Sopenharmony_ci<span>Added in: v15.14.0, v14.18.0</span> 6561cb0ef41Sopenharmony_ci</div> 6571cb0ef41Sopenharmony_ci<ul> 6581cb0ef41Sopenharmony_ci<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> 6591cb0ef41Sopenharmony_ci</ul> 6601cb0ef41Sopenharmony_ci<h4><code>socketaddress.flowlabel</code><span><a class="mark" href="#socketaddressflowlabel" id="socketaddressflowlabel">#</a></span><a aria-hidden="true" class="legacy" id="net_socketaddress_flowlabel"></a></h4> 6611cb0ef41Sopenharmony_ci<div class="api_metadata"> 6621cb0ef41Sopenharmony_ci<span>Added in: v15.14.0, v14.18.0</span> 6631cb0ef41Sopenharmony_ci</div> 6641cb0ef41Sopenharmony_ci<ul> 6651cb0ef41Sopenharmony_ci<li>Type <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 6661cb0ef41Sopenharmony_ci</ul> 6671cb0ef41Sopenharmony_ci<h4><code>socketaddress.port</code><span><a class="mark" href="#socketaddressport" id="socketaddressport">#</a></span><a aria-hidden="true" class="legacy" id="net_socketaddress_port"></a></h4> 6681cb0ef41Sopenharmony_ci<div class="api_metadata"> 6691cb0ef41Sopenharmony_ci<span>Added in: v15.14.0, v14.18.0</span> 6701cb0ef41Sopenharmony_ci</div> 6711cb0ef41Sopenharmony_ci<ul> 6721cb0ef41Sopenharmony_ci<li>Type <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 6731cb0ef41Sopenharmony_ci</ul> 6741cb0ef41Sopenharmony_ci</section><section><h3>Class: <code>net.Server</code><span><a class="mark" href="#class-netserver" id="class-netserver">#</a></span><a aria-hidden="true" class="legacy" id="net_class_net_server"></a></h3> 6751cb0ef41Sopenharmony_ci<div class="api_metadata"> 6761cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 6771cb0ef41Sopenharmony_ci</div> 6781cb0ef41Sopenharmony_ci<ul> 6791cb0ef41Sopenharmony_ci<li>Extends: <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 6801cb0ef41Sopenharmony_ci</ul> 6811cb0ef41Sopenharmony_ci<p>This class is used to create a TCP or <a href="#ipc-support">IPC</a> server.</p> 6821cb0ef41Sopenharmony_ci<h4><code>new net.Server([options][, connectionListener])</code><span><a class="mark" href="#new-netserveroptions-connectionlistener" id="new-netserveroptions-connectionlistener">#</a></span><a aria-hidden="true" class="legacy" id="net_new_net_server_options_connectionlistener"></a></h4> 6831cb0ef41Sopenharmony_ci<ul> 6841cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> See 6851cb0ef41Sopenharmony_ci<a href="#netcreateserveroptions-connectionlistener"><code>net.createServer([options][, connectionListener])</code></a>.</li> 6861cb0ef41Sopenharmony_ci<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 6871cb0ef41Sopenharmony_ci<a href="#event-connection"><code>'connection'</code></a> event.</li> 6881cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netserver" class="type"><net.Server></a></li> 6891cb0ef41Sopenharmony_ci</ul> 6901cb0ef41Sopenharmony_ci<p><code>net.Server</code> is an <a href="events.html#class-eventemitter"><code>EventEmitter</code></a> with the following events:</p> 6911cb0ef41Sopenharmony_ci<h4>Event: <code>'close'</code><span><a class="mark" href="#event-close" id="event-close">#</a></span><a aria-hidden="true" class="legacy" id="net_event_close"></a></h4> 6921cb0ef41Sopenharmony_ci<div class="api_metadata"> 6931cb0ef41Sopenharmony_ci<span>Added in: v0.5.0</span> 6941cb0ef41Sopenharmony_ci</div> 6951cb0ef41Sopenharmony_ci<p>Emitted when the server closes. If connections exist, this 6961cb0ef41Sopenharmony_cievent is not emitted until all connections are ended.</p> 6971cb0ef41Sopenharmony_ci<h4>Event: <code>'connection'</code><span><a class="mark" href="#event-connection" id="event-connection">#</a></span><a aria-hidden="true" class="legacy" id="net_event_connection"></a></h4> 6981cb0ef41Sopenharmony_ci<div class="api_metadata"> 6991cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 7001cb0ef41Sopenharmony_ci</div> 7011cb0ef41Sopenharmony_ci<ul> 7021cb0ef41Sopenharmony_ci<li><a href="net.html#class-netsocket" class="type"><net.Socket></a> The connection object</li> 7031cb0ef41Sopenharmony_ci</ul> 7041cb0ef41Sopenharmony_ci<p>Emitted when a new connection is made. <code>socket</code> is an instance of 7051cb0ef41Sopenharmony_ci<code>net.Socket</code>.</p> 7061cb0ef41Sopenharmony_ci<h4>Event: <code>'error'</code><span><a class="mark" href="#event-error" id="event-error">#</a></span><a aria-hidden="true" class="legacy" id="net_event_error"></a></h4> 7071cb0ef41Sopenharmony_ci<div class="api_metadata"> 7081cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 7091cb0ef41Sopenharmony_ci</div> 7101cb0ef41Sopenharmony_ci<ul> 7111cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 7121cb0ef41Sopenharmony_ci</ul> 7131cb0ef41Sopenharmony_ci<p>Emitted when an error occurs. Unlike <a href="#class-netsocket"><code>net.Socket</code></a>, the <a href="#event-close"><code>'close'</code></a> 7141cb0ef41Sopenharmony_cievent will <strong>not</strong> be emitted directly following this event unless 7151cb0ef41Sopenharmony_ci<a href="#serverclosecallback"><code>server.close()</code></a> is manually called. See the example in discussion of 7161cb0ef41Sopenharmony_ci<a href="#serverlisten"><code>server.listen()</code></a>.</p> 7171cb0ef41Sopenharmony_ci<h4>Event: <code>'listening'</code><span><a class="mark" href="#event-listening" id="event-listening">#</a></span><a aria-hidden="true" class="legacy" id="net_event_listening"></a></h4> 7181cb0ef41Sopenharmony_ci<div class="api_metadata"> 7191cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 7201cb0ef41Sopenharmony_ci</div> 7211cb0ef41Sopenharmony_ci<p>Emitted when the server has been bound after calling <a href="#serverlisten"><code>server.listen()</code></a>.</p> 7221cb0ef41Sopenharmony_ci<h4>Event: <code>'drop'</code><span><a class="mark" href="#event-drop" id="event-drop">#</a></span><a aria-hidden="true" class="legacy" id="net_event_drop"></a></h4> 7231cb0ef41Sopenharmony_ci<div class="api_metadata"> 7241cb0ef41Sopenharmony_ci<span>Added in: v18.6.0</span> 7251cb0ef41Sopenharmony_ci</div> 7261cb0ef41Sopenharmony_ci<p>When the number of connections reaches the threshold of <code>server.maxConnections</code>, 7271cb0ef41Sopenharmony_cithe server will drop new connections and emit <code>'drop'</code> event instead. If it is a 7281cb0ef41Sopenharmony_ciTCP server, the argument is as follows, otherwise the argument is <code>undefined</code>.</p> 7291cb0ef41Sopenharmony_ci<ul> 7301cb0ef41Sopenharmony_ci<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> The argument passed to event listener. 7311cb0ef41Sopenharmony_ci<ul> 7321cb0ef41Sopenharmony_ci<li><code>localAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Local address.</li> 7331cb0ef41Sopenharmony_ci<li><code>localPort</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Local port.</li> 7341cb0ef41Sopenharmony_ci<li><code>localFamily</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Local family.</li> 7351cb0ef41Sopenharmony_ci<li><code>remoteAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Remote address.</li> 7361cb0ef41Sopenharmony_ci<li><code>remotePort</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Remote port.</li> 7371cb0ef41Sopenharmony_ci<li><code>remoteFamily</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Remote IP family. <code>'IPv4'</code> or <code>'IPv6'</code>.</li> 7381cb0ef41Sopenharmony_ci</ul> 7391cb0ef41Sopenharmony_ci</li> 7401cb0ef41Sopenharmony_ci</ul> 7411cb0ef41Sopenharmony_ci<h4><code>server.address()</code><span><a class="mark" href="#serveraddress" id="serveraddress">#</a></span><a aria-hidden="true" class="legacy" id="net_server_address"></a></h4> 7421cb0ef41Sopenharmony_ci<div class="api_metadata"> 7431cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 7441cb0ef41Sopenharmony_ci<table> 7451cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 7461cb0ef41Sopenharmony_ci<tr><td>v18.4.0</td> 7471cb0ef41Sopenharmony_ci<td><p>The <code>family</code> property now returns a string instead of a number.</p></td></tr> 7481cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 7491cb0ef41Sopenharmony_ci<td><p>The <code>family</code> property now returns a number instead of a string.</p></td></tr> 7501cb0ef41Sopenharmony_ci<tr><td>v0.1.90</td> 7511cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.90</span></p></td></tr> 7521cb0ef41Sopenharmony_ci</tbody></table> 7531cb0ef41Sopenharmony_ci</details> 7541cb0ef41Sopenharmony_ci</div> 7551cb0ef41Sopenharmony_ci<ul> 7561cb0ef41Sopenharmony_ci<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> 7571cb0ef41Sopenharmony_ci</ul> 7581cb0ef41Sopenharmony_ci<p>Returns the bound <code>address</code>, the address <code>family</code> name, and <code>port</code> of the server 7591cb0ef41Sopenharmony_cias reported by the operating system if listening on an IP socket 7601cb0ef41Sopenharmony_ci(useful to find which port was assigned when getting an OS-assigned address): 7611cb0ef41Sopenharmony_ci<code>{ port: 12346, family: 'IPv4', address: '127.0.0.1' }</code>.</p> 7621cb0ef41Sopenharmony_ci<p>For a server listening on a pipe or Unix domain socket, the name is returned 7631cb0ef41Sopenharmony_cias a string.</p> 7641cb0ef41Sopenharmony_ci<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> { 7651cb0ef41Sopenharmony_ci socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'goodbye\n'</span>); 7661cb0ef41Sopenharmony_ci}).<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 7671cb0ef41Sopenharmony_ci <span class="hljs-comment">// Handle errors here.</span> 7681cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> err; 7691cb0ef41Sopenharmony_ci}); 7701cb0ef41Sopenharmony_ci 7711cb0ef41Sopenharmony_ci<span class="hljs-comment">// Grab an arbitrary unused port.</span> 7721cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">listen</span>(<span class="hljs-function">() =></span> { 7731cb0ef41Sopenharmony_ci <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>()); 7741cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 7751cb0ef41Sopenharmony_ci<p><code>server.address()</code> returns <code>null</code> before the <code>'listening'</code> event has been 7761cb0ef41Sopenharmony_ciemitted or after calling <code>server.close()</code>.</p> 7771cb0ef41Sopenharmony_ci<h4><code>server.close([callback])</code><span><a class="mark" href="#serverclosecallback" id="serverclosecallback">#</a></span><a aria-hidden="true" class="legacy" id="net_server_close_callback"></a></h4> 7781cb0ef41Sopenharmony_ci<div class="api_metadata"> 7791cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 7801cb0ef41Sopenharmony_ci</div> 7811cb0ef41Sopenharmony_ci<ul> 7821cb0ef41Sopenharmony_ci<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> 7831cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netserver" class="type"><net.Server></a></li> 7841cb0ef41Sopenharmony_ci</ul> 7851cb0ef41Sopenharmony_ci<p>Stops the server from accepting new connections and keeps existing 7861cb0ef41Sopenharmony_ciconnections. This function is asynchronous, the server is finally closed 7871cb0ef41Sopenharmony_ciwhen all connections are ended and the server emits a <a href="#event-close"><code>'close'</code></a> event. 7881cb0ef41Sopenharmony_ciThe optional <code>callback</code> will be called once the <code>'close'</code> event occurs. Unlike 7891cb0ef41Sopenharmony_cithat event, it will be called with an <code>Error</code> as its only argument if the server 7901cb0ef41Sopenharmony_ciwas not open when it was closed.</p> 7911cb0ef41Sopenharmony_ci<h4><code>server[Symbol.asyncDispose]()</code><span><a class="mark" href="#serversymbolasyncdispose" id="serversymbolasyncdispose">#</a></span><a aria-hidden="true" class="legacy" id="net_server_symbol_asyncdispose"></a></h4> 7921cb0ef41Sopenharmony_ci<div class="api_metadata"> 7931cb0ef41Sopenharmony_ci<span>Added in: v18.18.0</span> 7941cb0ef41Sopenharmony_ci</div> 7951cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 7961cb0ef41Sopenharmony_ci<p>Calls <a href="#serverclosecallback"><code>server.close()</code></a> and returns a promise that fulfills when the 7971cb0ef41Sopenharmony_ciserver has closed.</p> 7981cb0ef41Sopenharmony_ci<h4><code>server.getConnections(callback)</code><span><a class="mark" href="#servergetconnectionscallback" id="servergetconnectionscallback">#</a></span><a aria-hidden="true" class="legacy" id="net_server_getconnections_callback"></a></h4> 7991cb0ef41Sopenharmony_ci<div class="api_metadata"> 8001cb0ef41Sopenharmony_ci<span>Added in: v0.9.7</span> 8011cb0ef41Sopenharmony_ci</div> 8021cb0ef41Sopenharmony_ci<ul> 8031cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 8041cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netserver" class="type"><net.Server></a></li> 8051cb0ef41Sopenharmony_ci</ul> 8061cb0ef41Sopenharmony_ci<p>Asynchronously get the number of concurrent connections on the server. Works 8071cb0ef41Sopenharmony_ciwhen sockets were sent to forks.</p> 8081cb0ef41Sopenharmony_ci<p>Callback should take two arguments <code>err</code> and <code>count</code>.</p> 8091cb0ef41Sopenharmony_ci<h4><code>server.listen()</code><span><a class="mark" href="#serverlisten" id="serverlisten">#</a></span><a aria-hidden="true" class="legacy" id="net_server_listen"></a></h4> 8101cb0ef41Sopenharmony_ci<p>Start a server listening for connections. A <code>net.Server</code> can be a TCP or 8111cb0ef41Sopenharmony_cian <a href="#ipc-support">IPC</a> server depending on what it listens to.</p> 8121cb0ef41Sopenharmony_ci<p>Possible signatures:</p> 8131cb0ef41Sopenharmony_ci<ul> 8141cb0ef41Sopenharmony_ci<li><a href="#serverlistenhandle-backlog-callback"><code>server.listen(handle[, backlog][, callback])</code></a></li> 8151cb0ef41Sopenharmony_ci<li><a href="#serverlistenoptions-callback"><code>server.listen(options[, callback])</code></a></li> 8161cb0ef41Sopenharmony_ci<li><a href="#serverlistenpath-backlog-callback"><code>server.listen(path[, backlog][, callback])</code></a> 8171cb0ef41Sopenharmony_cifor <a href="#ipc-support">IPC</a> servers</li> 8181cb0ef41Sopenharmony_ci<li><a href="#serverlistenport-host-backlog-callback"><code>server.listen([port[, host[, backlog]]][, callback])</code></a> 8191cb0ef41Sopenharmony_cifor TCP servers</li> 8201cb0ef41Sopenharmony_ci</ul> 8211cb0ef41Sopenharmony_ci<p>This function is asynchronous. When the server starts listening, the 8221cb0ef41Sopenharmony_ci<a href="#event-listening"><code>'listening'</code></a> event will be emitted. The last parameter <code>callback</code> 8231cb0ef41Sopenharmony_ciwill be added as a listener for the <a href="#event-listening"><code>'listening'</code></a> event.</p> 8241cb0ef41Sopenharmony_ci<p>All <code>listen()</code> methods can take a <code>backlog</code> parameter to specify the maximum 8251cb0ef41Sopenharmony_cilength of the queue of pending connections. The actual length will be determined 8261cb0ef41Sopenharmony_ciby the OS through sysctl settings such as <code>tcp_max_syn_backlog</code> and <code>somaxconn</code> 8271cb0ef41Sopenharmony_cion Linux. The default value of this parameter is 511 (not 512).</p> 8281cb0ef41Sopenharmony_ci<p>All <a href="#class-netsocket"><code>net.Socket</code></a> are set to <code>SO_REUSEADDR</code> (see <a href="https://man7.org/linux/man-pages/man7/socket.7.html"><code>socket(7)</code></a> for 8291cb0ef41Sopenharmony_cidetails).</p> 8301cb0ef41Sopenharmony_ci<p>The <code>server.listen()</code> method can be called again if and only if there was an 8311cb0ef41Sopenharmony_cierror during the first <code>server.listen()</code> call or <code>server.close()</code> has been 8321cb0ef41Sopenharmony_cicalled. Otherwise, an <code>ERR_SERVER_ALREADY_LISTEN</code> error will be thrown.</p> 8331cb0ef41Sopenharmony_ci<p>One of the most common errors raised when listening is <code>EADDRINUSE</code>. 8341cb0ef41Sopenharmony_ciThis happens when another server is already listening on the requested 8351cb0ef41Sopenharmony_ci<code>port</code>/<code>path</code>/<code>handle</code>. One way to handle this would be to retry 8361cb0ef41Sopenharmony_ciafter a certain amount of time:</p> 8371cb0ef41Sopenharmony_ci<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> { 8381cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (e.<span class="hljs-property">code</span> === <span class="hljs-string">'EADDRINUSE'</span>) { 8391cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'Address in use, retrying...'</span>); 8401cb0ef41Sopenharmony_ci <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> { 8411cb0ef41Sopenharmony_ci server.<span class="hljs-title function_">close</span>(); 8421cb0ef41Sopenharmony_ci server.<span class="hljs-title function_">listen</span>(<span class="hljs-variable constant_">PORT</span>, <span class="hljs-variable constant_">HOST</span>); 8431cb0ef41Sopenharmony_ci }, <span class="hljs-number">1000</span>); 8441cb0ef41Sopenharmony_ci } 8451cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 8461cb0ef41Sopenharmony_ci<h5><code>server.listen(handle[, backlog][, callback])</code><span><a class="mark" href="#serverlistenhandle-backlog-callback" id="serverlistenhandle-backlog-callback">#</a></span><a aria-hidden="true" class="legacy" id="net_server_listen_handle_backlog_callback"></a></h5> 8471cb0ef41Sopenharmony_ci<div class="api_metadata"> 8481cb0ef41Sopenharmony_ci<span>Added in: v0.5.10</span> 8491cb0ef41Sopenharmony_ci</div> 8501cb0ef41Sopenharmony_ci<ul> 8511cb0ef41Sopenharmony_ci<li><code>handle</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 8521cb0ef41Sopenharmony_ci<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="#serverlisten"><code>server.listen()</code></a> functions</li> 8531cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 8541cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netserver" class="type"><net.Server></a></li> 8551cb0ef41Sopenharmony_ci</ul> 8561cb0ef41Sopenharmony_ci<p>Start a server listening for connections on a given <code>handle</code> that has 8571cb0ef41Sopenharmony_cialready been bound to a port, a Unix domain socket, or a Windows named pipe.</p> 8581cb0ef41Sopenharmony_ci<p>The <code>handle</code> object can be either a server, a socket (anything with an 8591cb0ef41Sopenharmony_ciunderlying <code>_handle</code> member), or an object with an <code>fd</code> member that is a 8601cb0ef41Sopenharmony_civalid file descriptor.</p> 8611cb0ef41Sopenharmony_ci<p>Listening on a file descriptor is not supported on Windows.</p> 8621cb0ef41Sopenharmony_ci<h5><code>server.listen(options[, callback])</code><span><a class="mark" href="#serverlistenoptions-callback" id="serverlistenoptions-callback">#</a></span><a aria-hidden="true" class="legacy" id="net_server_listen_options_callback"></a></h5> 8631cb0ef41Sopenharmony_ci<div class="api_metadata"> 8641cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 8651cb0ef41Sopenharmony_ci<table> 8661cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 8671cb0ef41Sopenharmony_ci<tr><td>v15.6.0</td> 8681cb0ef41Sopenharmony_ci<td><p>AbortSignal support was added.</p></td></tr> 8691cb0ef41Sopenharmony_ci<tr><td>v11.4.0</td> 8701cb0ef41Sopenharmony_ci<td><p>The <code>ipv6Only</code> option is supported.</p></td></tr> 8711cb0ef41Sopenharmony_ci<tr><td>v0.11.14</td> 8721cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.14</span></p></td></tr> 8731cb0ef41Sopenharmony_ci</tbody></table> 8741cb0ef41Sopenharmony_ci</details> 8751cb0ef41Sopenharmony_ci</div> 8761cb0ef41Sopenharmony_ci<ul> 8771cb0ef41Sopenharmony_ci<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: 8781cb0ef41Sopenharmony_ci<ul> 8791cb0ef41Sopenharmony_ci<li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 8801cb0ef41Sopenharmony_ci<li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 8811cb0ef41Sopenharmony_ci<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 8821cb0ef41Sopenharmony_ci<a href="#identifying-paths-for-ipc-connections">Identifying paths for IPC connections</a>.</li> 8831cb0ef41Sopenharmony_ci<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="#serverlisten"><code>server.listen()</code></a> 8841cb0ef41Sopenharmony_cifunctions.</li> 8851cb0ef41Sopenharmony_ci<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> 8861cb0ef41Sopenharmony_ci<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 8871cb0ef41Sopenharmony_cifor all users. <strong>Default:</strong> <code>false</code>.</li> 8881cb0ef41Sopenharmony_ci<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 8891cb0ef41Sopenharmony_cifor all users. <strong>Default:</strong> <code>false</code>.</li> 8901cb0ef41Sopenharmony_ci<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 8911cb0ef41Sopenharmony_cidisable dual-stack support, i.e., binding to host <code>::</code> won't make 8921cb0ef41Sopenharmony_ci<code>0.0.0.0</code> be bound. <strong>Default:</strong> <code>false</code>.</li> 8931cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> An AbortSignal that may be used to close a listening server.</li> 8941cb0ef41Sopenharmony_ci</ul> 8951cb0ef41Sopenharmony_ci</li> 8961cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 8971cb0ef41Sopenharmony_cifunctions.</li> 8981cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netserver" class="type"><net.Server></a></li> 8991cb0ef41Sopenharmony_ci</ul> 9001cb0ef41Sopenharmony_ci<p>If <code>port</code> is specified, it behaves the same as 9011cb0ef41Sopenharmony_ci<a href="#serverlistenport-host-backlog-callback"><code>server.listen([port[, host[, backlog]]][, callback])</code></a>. 9021cb0ef41Sopenharmony_ciOtherwise, if <code>path</code> is specified, it behaves the same as 9031cb0ef41Sopenharmony_ci<a href="#serverlistenpath-backlog-callback"><code>server.listen(path[, backlog][, callback])</code></a>. 9041cb0ef41Sopenharmony_ciIf none of them is specified, an error will be thrown.</p> 9051cb0ef41Sopenharmony_ci<p>If <code>exclusive</code> is <code>false</code> (default), then cluster workers will use the same 9061cb0ef41Sopenharmony_ciunderlying handle, allowing connection handling duties to be shared. When 9071cb0ef41Sopenharmony_ci<code>exclusive</code> is <code>true</code>, the handle is not shared, and attempted port sharing 9081cb0ef41Sopenharmony_ciresults in an error. An example which listens on an exclusive port is 9091cb0ef41Sopenharmony_cishown below.</p> 9101cb0ef41Sopenharmony_ci<pre><code class="language-js">server.<span class="hljs-title function_">listen</span>({ 9111cb0ef41Sopenharmony_ci <span class="hljs-attr">host</span>: <span class="hljs-string">'localhost'</span>, 9121cb0ef41Sopenharmony_ci <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>, 9131cb0ef41Sopenharmony_ci <span class="hljs-attr">exclusive</span>: <span class="hljs-literal">true</span>, 9141cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 9151cb0ef41Sopenharmony_ci<p>When <code>exclusive</code> is <code>true</code> and the underlying handle is shared, it is 9161cb0ef41Sopenharmony_cipossible that several workers query a handle with different backlogs. 9171cb0ef41Sopenharmony_ciIn this case, the first <code>backlog</code> passed to the master process will be used.</p> 9181cb0ef41Sopenharmony_ci<p>Starting an IPC server as root may cause the server path to be inaccessible for 9191cb0ef41Sopenharmony_ciunprivileged users. Using <code>readableAll</code> and <code>writableAll</code> will make the server 9201cb0ef41Sopenharmony_ciaccessible for all users.</p> 9211cb0ef41Sopenharmony_ci<p>If the <code>signal</code> option is enabled, calling <code>.abort()</code> on the corresponding 9221cb0ef41Sopenharmony_ci<code>AbortController</code> is similar to calling <code>.close()</code> on the server:</p> 9231cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); 9241cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">listen</span>({ 9251cb0ef41Sopenharmony_ci <span class="hljs-attr">host</span>: <span class="hljs-string">'localhost'</span>, 9261cb0ef41Sopenharmony_ci <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>, 9271cb0ef41Sopenharmony_ci <span class="hljs-attr">signal</span>: controller.<span class="hljs-property">signal</span>, 9281cb0ef41Sopenharmony_ci}); 9291cb0ef41Sopenharmony_ci<span class="hljs-comment">// Later, when you want to close the server.</span> 9301cb0ef41Sopenharmony_cicontroller.<span class="hljs-title function_">abort</span>();</code> <button class="copy-button">copy</button></pre> 9311cb0ef41Sopenharmony_ci<h5><code>server.listen(path[, backlog][, callback])</code><span><a class="mark" href="#serverlistenpath-backlog-callback" id="serverlistenpath-backlog-callback">#</a></span><a aria-hidden="true" class="legacy" id="net_server_listen_path_backlog_callback"></a></h5> 9321cb0ef41Sopenharmony_ci<div class="api_metadata"> 9331cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 9341cb0ef41Sopenharmony_ci</div> 9351cb0ef41Sopenharmony_ci<ul> 9361cb0ef41Sopenharmony_ci<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 9371cb0ef41Sopenharmony_ci<a href="#identifying-paths-for-ipc-connections">Identifying paths for IPC connections</a>.</li> 9381cb0ef41Sopenharmony_ci<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="#serverlisten"><code>server.listen()</code></a> functions.</li> 9391cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>.</li> 9401cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netserver" class="type"><net.Server></a></li> 9411cb0ef41Sopenharmony_ci</ul> 9421cb0ef41Sopenharmony_ci<p>Start an <a href="#ipc-support">IPC</a> server listening for connections on the given <code>path</code>.</p> 9431cb0ef41Sopenharmony_ci<h5><code>server.listen([port[, host[, backlog]]][, callback])</code><span><a class="mark" href="#serverlistenport-host-backlog-callback" id="serverlistenport-host-backlog-callback">#</a></span><a aria-hidden="true" class="legacy" id="net_server_listen_port_host_backlog_callback"></a></h5> 9441cb0ef41Sopenharmony_ci<div class="api_metadata"> 9451cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 9461cb0ef41Sopenharmony_ci</div> 9471cb0ef41Sopenharmony_ci<ul> 9481cb0ef41Sopenharmony_ci<li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 9491cb0ef41Sopenharmony_ci<li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 9501cb0ef41Sopenharmony_ci<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="#serverlisten"><code>server.listen()</code></a> functions.</li> 9511cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>.</li> 9521cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netserver" class="type"><net.Server></a></li> 9531cb0ef41Sopenharmony_ci</ul> 9541cb0ef41Sopenharmony_ci<p>Start a TCP server listening for connections on the given <code>port</code> and <code>host</code>.</p> 9551cb0ef41Sopenharmony_ci<p>If <code>port</code> is omitted or is 0, the operating system will assign an arbitrary 9561cb0ef41Sopenharmony_ciunused port, which can be retrieved by using <code>server.address().port</code> 9571cb0ef41Sopenharmony_ciafter the <a href="#event-listening"><code>'listening'</code></a> event has been emitted.</p> 9581cb0ef41Sopenharmony_ci<p>If <code>host</code> is omitted, the server will accept connections on the 9591cb0ef41Sopenharmony_ci<a href="https://en.wikipedia.org/wiki/IPv6_address#Unspecified_address">unspecified IPv6 address</a> (<code>::</code>) when IPv6 is available, or the 9601cb0ef41Sopenharmony_ci<a href="https://en.wikipedia.org/wiki/0.0.0.0">unspecified IPv4 address</a> (<code>0.0.0.0</code>) otherwise.</p> 9611cb0ef41Sopenharmony_ci<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>) 9621cb0ef41Sopenharmony_cimay 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> 9631cb0ef41Sopenharmony_ci(<code>0.0.0.0</code>).</p> 9641cb0ef41Sopenharmony_ci<h4><code>server.listening</code><span><a class="mark" href="#serverlistening" id="serverlistening">#</a></span><a aria-hidden="true" class="legacy" id="net_server_listening"></a></h4> 9651cb0ef41Sopenharmony_ci<div class="api_metadata"> 9661cb0ef41Sopenharmony_ci<span>Added in: v5.7.0</span> 9671cb0ef41Sopenharmony_ci</div> 9681cb0ef41Sopenharmony_ci<ul> 9691cb0ef41Sopenharmony_ci<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> 9701cb0ef41Sopenharmony_ci</ul> 9711cb0ef41Sopenharmony_ci<h4><code>server.maxConnections</code><span><a class="mark" href="#servermaxconnections" id="servermaxconnections">#</a></span><a aria-hidden="true" class="legacy" id="net_server_maxconnections"></a></h4> 9721cb0ef41Sopenharmony_ci<div class="api_metadata"> 9731cb0ef41Sopenharmony_ci<span>Added in: v0.2.0</span> 9741cb0ef41Sopenharmony_ci</div> 9751cb0ef41Sopenharmony_ci<ul> 9761cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 9771cb0ef41Sopenharmony_ci</ul> 9781cb0ef41Sopenharmony_ci<p>Set this property to reject connections when the server's connection count gets 9791cb0ef41Sopenharmony_cihigh.</p> 9801cb0ef41Sopenharmony_ci<p>It is not recommended to use this option once a socket has been sent to a child 9811cb0ef41Sopenharmony_ciwith <a href="child_process.html#child_processforkmodulepath-args-options"><code>child_process.fork()</code></a>.</p> 9821cb0ef41Sopenharmony_ci<h4><code>server.ref()</code><span><a class="mark" href="#serverref" id="serverref">#</a></span><a aria-hidden="true" class="legacy" id="net_server_ref"></a></h4> 9831cb0ef41Sopenharmony_ci<div class="api_metadata"> 9841cb0ef41Sopenharmony_ci<span>Added in: v0.9.1</span> 9851cb0ef41Sopenharmony_ci</div> 9861cb0ef41Sopenharmony_ci<ul> 9871cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netserver" class="type"><net.Server></a></li> 9881cb0ef41Sopenharmony_ci</ul> 9891cb0ef41Sopenharmony_ci<p>Opposite of <code>unref()</code>, calling <code>ref()</code> on a previously <code>unref</code>ed server will 9901cb0ef41Sopenharmony_ci<em>not</em> let the program exit if it's the only server left (the default behavior). 9911cb0ef41Sopenharmony_ciIf the server is <code>ref</code>ed calling <code>ref()</code> again will have no effect.</p> 9921cb0ef41Sopenharmony_ci<h4><code>server.unref()</code><span><a class="mark" href="#serverunref" id="serverunref">#</a></span><a aria-hidden="true" class="legacy" id="net_server_unref"></a></h4> 9931cb0ef41Sopenharmony_ci<div class="api_metadata"> 9941cb0ef41Sopenharmony_ci<span>Added in: v0.9.1</span> 9951cb0ef41Sopenharmony_ci</div> 9961cb0ef41Sopenharmony_ci<ul> 9971cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netserver" class="type"><net.Server></a></li> 9981cb0ef41Sopenharmony_ci</ul> 9991cb0ef41Sopenharmony_ci<p>Calling <code>unref()</code> on a server will allow the program to exit if this is the only 10001cb0ef41Sopenharmony_ciactive server in the event system. If the server is already <code>unref</code>ed calling 10011cb0ef41Sopenharmony_ci<code>unref()</code> again will have no effect.</p> 10021cb0ef41Sopenharmony_ci</section><section><h3>Class: <code>net.Socket</code><span><a class="mark" href="#class-netsocket" id="class-netsocket">#</a></span><a aria-hidden="true" class="legacy" id="net_class_net_socket"></a></h3> 10031cb0ef41Sopenharmony_ci<div class="api_metadata"> 10041cb0ef41Sopenharmony_ci<span>Added in: v0.3.4</span> 10051cb0ef41Sopenharmony_ci</div> 10061cb0ef41Sopenharmony_ci<ul> 10071cb0ef41Sopenharmony_ci<li>Extends: <a href="stream.html#class-streamduplex" class="type"><stream.Duplex></a></li> 10081cb0ef41Sopenharmony_ci</ul> 10091cb0ef41Sopenharmony_ci<p>This class is an abstraction of a TCP socket or a streaming <a href="#ipc-support">IPC</a> endpoint 10101cb0ef41Sopenharmony_ci(uses named pipes on Windows, and Unix domain sockets otherwise). It is also 10111cb0ef41Sopenharmony_cian <a href="events.html#class-eventemitter"><code>EventEmitter</code></a>.</p> 10121cb0ef41Sopenharmony_ci<p>A <code>net.Socket</code> can be created by the user and used directly to interact with 10131cb0ef41Sopenharmony_cia server. For example, it is returned by <a href="#netcreateconnection"><code>net.createConnection()</code></a>, 10141cb0ef41Sopenharmony_ciso the user can use it to talk to the server.</p> 10151cb0ef41Sopenharmony_ci<p>It can also be created by Node.js and passed to the user when a connection 10161cb0ef41Sopenharmony_ciis received. For example, it is passed to the listeners of a 10171cb0ef41Sopenharmony_ci<a href="#event-connection"><code>'connection'</code></a> event emitted on a <a href="#class-netserver"><code>net.Server</code></a>, so the user can use 10181cb0ef41Sopenharmony_ciit to interact with the client.</p> 10191cb0ef41Sopenharmony_ci<h4><code>new net.Socket([options])</code><span><a class="mark" href="#new-netsocketoptions" id="new-netsocketoptions">#</a></span><a aria-hidden="true" class="legacy" id="net_new_net_socket_options"></a></h4> 10201cb0ef41Sopenharmony_ci<div class="api_metadata"> 10211cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 10221cb0ef41Sopenharmony_ci<table> 10231cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 10241cb0ef41Sopenharmony_ci<tr><td>v15.14.0</td> 10251cb0ef41Sopenharmony_ci<td><p>AbortSignal support was added.</p></td></tr> 10261cb0ef41Sopenharmony_ci<tr><td>v0.3.4</td> 10271cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.3.4</span></p></td></tr> 10281cb0ef41Sopenharmony_ci</tbody></table> 10291cb0ef41Sopenharmony_ci</details> 10301cb0ef41Sopenharmony_ci</div> 10311cb0ef41Sopenharmony_ci<ul> 10321cb0ef41Sopenharmony_ci<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: 10331cb0ef41Sopenharmony_ci<ul> 10341cb0ef41Sopenharmony_ci<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 10351cb0ef41Sopenharmony_cithe given file descriptor, otherwise a new socket will be created.</li> 10361cb0ef41Sopenharmony_ci<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 10371cb0ef41Sopenharmony_ciautomatically end the writable side when the readable side ends. See 10381cb0ef41Sopenharmony_ci<a href="#netcreateserveroptions-connectionlistener"><code>net.createServer()</code></a> and the <a href="#event-end"><code>'end'</code></a> event for details. <strong>Default:</strong> 10391cb0ef41Sopenharmony_ci<code>false</code>.</li> 10401cb0ef41Sopenharmony_ci<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, 10411cb0ef41Sopenharmony_ciotherwise ignored. <strong>Default:</strong> <code>false</code>.</li> 10421cb0ef41Sopenharmony_ci<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, 10431cb0ef41Sopenharmony_ciotherwise ignored. <strong>Default:</strong> <code>false</code>.</li> 10441cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> An Abort signal that may be used to destroy the 10451cb0ef41Sopenharmony_cisocket.</li> 10461cb0ef41Sopenharmony_ci</ul> 10471cb0ef41Sopenharmony_ci</li> 10481cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netsocket" class="type"><net.Socket></a></li> 10491cb0ef41Sopenharmony_ci</ul> 10501cb0ef41Sopenharmony_ci<p>Creates a new socket object.</p> 10511cb0ef41Sopenharmony_ci<p>The newly created socket can be either a TCP socket or a streaming <a href="#ipc-support">IPC</a> 10521cb0ef41Sopenharmony_ciendpoint, depending on what it <a href="#socketconnect"><code>connect()</code></a> to.</p> 10531cb0ef41Sopenharmony_ci<h4>Event: <code>'close'</code><span><a class="mark" href="#event-close_1" id="event-close_1">#</a></span><a aria-hidden="true" class="legacy" id="net_event_close_1"></a></h4> 10541cb0ef41Sopenharmony_ci<div class="api_metadata"> 10551cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 10561cb0ef41Sopenharmony_ci</div> 10571cb0ef41Sopenharmony_ci<ul> 10581cb0ef41Sopenharmony_ci<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> 10591cb0ef41Sopenharmony_ci</ul> 10601cb0ef41Sopenharmony_ci<p>Emitted once the socket is fully closed. The argument <code>hadError</code> is a boolean 10611cb0ef41Sopenharmony_ciwhich says if the socket was closed due to a transmission error.</p> 10621cb0ef41Sopenharmony_ci<h4>Event: <code>'connect'</code><span><a class="mark" href="#event-connect" id="event-connect">#</a></span><a aria-hidden="true" class="legacy" id="net_event_connect"></a></h4> 10631cb0ef41Sopenharmony_ci<div class="api_metadata"> 10641cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 10651cb0ef41Sopenharmony_ci</div> 10661cb0ef41Sopenharmony_ci<p>Emitted when a socket connection is successfully established. 10671cb0ef41Sopenharmony_ciSee <a href="#netcreateconnection"><code>net.createConnection()</code></a>.</p> 10681cb0ef41Sopenharmony_ci<h4>Event: <code>'data'</code><span><a class="mark" href="#event-data" id="event-data">#</a></span><a aria-hidden="true" class="legacy" id="net_event_data"></a></h4> 10691cb0ef41Sopenharmony_ci<div class="api_metadata"> 10701cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 10711cb0ef41Sopenharmony_ci</div> 10721cb0ef41Sopenharmony_ci<ul> 10731cb0ef41Sopenharmony_ci<li><a href="buffer.html#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> 10741cb0ef41Sopenharmony_ci</ul> 10751cb0ef41Sopenharmony_ci<p>Emitted when data is received. The argument <code>data</code> will be a <code>Buffer</code> or 10761cb0ef41Sopenharmony_ci<code>String</code>. Encoding of data is set by <a href="#socketsetencodingencoding"><code>socket.setEncoding()</code></a>.</p> 10771cb0ef41Sopenharmony_ci<p>The data will be lost if there is no listener when a <code>Socket</code> 10781cb0ef41Sopenharmony_ciemits a <code>'data'</code> event.</p> 10791cb0ef41Sopenharmony_ci<h4>Event: <code>'drain'</code><span><a class="mark" href="#event-drain" id="event-drain">#</a></span><a aria-hidden="true" class="legacy" id="net_event_drain"></a></h4> 10801cb0ef41Sopenharmony_ci<div class="api_metadata"> 10811cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 10821cb0ef41Sopenharmony_ci</div> 10831cb0ef41Sopenharmony_ci<p>Emitted when the write buffer becomes empty. Can be used to throttle uploads.</p> 10841cb0ef41Sopenharmony_ci<p>See also: the return values of <code>socket.write()</code>.</p> 10851cb0ef41Sopenharmony_ci<h4>Event: <code>'end'</code><span><a class="mark" href="#event-end" id="event-end">#</a></span><a aria-hidden="true" class="legacy" id="net_event_end"></a></h4> 10861cb0ef41Sopenharmony_ci<div class="api_metadata"> 10871cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 10881cb0ef41Sopenharmony_ci</div> 10891cb0ef41Sopenharmony_ci<p>Emitted when the other end of the socket signals the end of transmission, thus 10901cb0ef41Sopenharmony_ciending the readable side of the socket.</p> 10911cb0ef41Sopenharmony_ci<p>By default (<code>allowHalfOpen</code> is <code>false</code>) the socket will send an end of 10921cb0ef41Sopenharmony_citransmission packet back and destroy its file descriptor once it has written out 10931cb0ef41Sopenharmony_ciits pending write queue. However, if <code>allowHalfOpen</code> is set to <code>true</code>, the 10941cb0ef41Sopenharmony_cisocket will not automatically <a href="#socketenddata-encoding-callback"><code>end()</code></a> its writable side, 10951cb0ef41Sopenharmony_ciallowing the user to write arbitrary amounts of data. The user must call 10961cb0ef41Sopenharmony_ci<a href="#socketenddata-encoding-callback"><code>end()</code></a> explicitly to close the connection (i.e. sending a 10971cb0ef41Sopenharmony_ciFIN packet back).</p> 10981cb0ef41Sopenharmony_ci<h4>Event: <code>'error'</code><span><a class="mark" href="#event-error_1" id="event-error_1">#</a></span><a aria-hidden="true" class="legacy" id="net_event_error_1"></a></h4> 10991cb0ef41Sopenharmony_ci<div class="api_metadata"> 11001cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 11011cb0ef41Sopenharmony_ci</div> 11021cb0ef41Sopenharmony_ci<ul> 11031cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 11041cb0ef41Sopenharmony_ci</ul> 11051cb0ef41Sopenharmony_ci<p>Emitted when an error occurs. The <code>'close'</code> event will be called directly 11061cb0ef41Sopenharmony_cifollowing this event.</p> 11071cb0ef41Sopenharmony_ci<h4>Event: <code>'lookup'</code><span><a class="mark" href="#event-lookup" id="event-lookup">#</a></span><a aria-hidden="true" class="legacy" id="net_event_lookup"></a></h4> 11081cb0ef41Sopenharmony_ci<div class="api_metadata"> 11091cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 11101cb0ef41Sopenharmony_ci<table> 11111cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 11121cb0ef41Sopenharmony_ci<tr><td>v5.10.0</td> 11131cb0ef41Sopenharmony_ci<td><p>The <code>host</code> parameter is supported now.</p></td></tr> 11141cb0ef41Sopenharmony_ci<tr><td>v0.11.3</td> 11151cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.3</span></p></td></tr> 11161cb0ef41Sopenharmony_ci</tbody></table> 11171cb0ef41Sopenharmony_ci</details> 11181cb0ef41Sopenharmony_ci</div> 11191cb0ef41Sopenharmony_ci<p>Emitted after resolving the host name but before connecting. 11201cb0ef41Sopenharmony_ciNot applicable to Unix sockets.</p> 11211cb0ef41Sopenharmony_ci<ul> 11221cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> The error object. See <a href="dns.html#dnslookuphostname-options-callback"><code>dns.lookup()</code></a>.</li> 11231cb0ef41Sopenharmony_ci<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> 11241cb0ef41Sopenharmony_ci<li><code>family</code> <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#Null_type" class="type"><null></a> The address type. See <a href="dns.html#dnslookuphostname-options-callback"><code>dns.lookup()</code></a>.</li> 11251cb0ef41Sopenharmony_ci<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> 11261cb0ef41Sopenharmony_ci</ul> 11271cb0ef41Sopenharmony_ci<h4>Event: <code>'ready'</code><span><a class="mark" href="#event-ready" id="event-ready">#</a></span><a aria-hidden="true" class="legacy" id="net_event_ready"></a></h4> 11281cb0ef41Sopenharmony_ci<div class="api_metadata"> 11291cb0ef41Sopenharmony_ci<span>Added in: v9.11.0</span> 11301cb0ef41Sopenharmony_ci</div> 11311cb0ef41Sopenharmony_ci<p>Emitted when a socket is ready to be used.</p> 11321cb0ef41Sopenharmony_ci<p>Triggered immediately after <code>'connect'</code>.</p> 11331cb0ef41Sopenharmony_ci<h4>Event: <code>'timeout'</code><span><a class="mark" href="#event-timeout" id="event-timeout">#</a></span><a aria-hidden="true" class="legacy" id="net_event_timeout"></a></h4> 11341cb0ef41Sopenharmony_ci<div class="api_metadata"> 11351cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 11361cb0ef41Sopenharmony_ci</div> 11371cb0ef41Sopenharmony_ci<p>Emitted if the socket times out from inactivity. This is only to notify that 11381cb0ef41Sopenharmony_cithe socket has been idle. The user must manually close the connection.</p> 11391cb0ef41Sopenharmony_ci<p>See also: <a href="#socketsettimeouttimeout-callback"><code>socket.setTimeout()</code></a>.</p> 11401cb0ef41Sopenharmony_ci<h4><code>socket.address()</code><span><a class="mark" href="#socketaddress" id="socketaddress">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_address"></a></h4> 11411cb0ef41Sopenharmony_ci<div class="api_metadata"> 11421cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 11431cb0ef41Sopenharmony_ci<table> 11441cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 11451cb0ef41Sopenharmony_ci<tr><td>v18.4.0</td> 11461cb0ef41Sopenharmony_ci<td><p>The <code>family</code> property now returns a string instead of a number.</p></td></tr> 11471cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 11481cb0ef41Sopenharmony_ci<td><p>The <code>family</code> property now returns a number instead of a string.</p></td></tr> 11491cb0ef41Sopenharmony_ci<tr><td>v0.1.90</td> 11501cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.90</span></p></td></tr> 11511cb0ef41Sopenharmony_ci</tbody></table> 11521cb0ef41Sopenharmony_ci</details> 11531cb0ef41Sopenharmony_ci</div> 11541cb0ef41Sopenharmony_ci<ul> 11551cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 11561cb0ef41Sopenharmony_ci</ul> 11571cb0ef41Sopenharmony_ci<p>Returns the bound <code>address</code>, the address <code>family</code> name and <code>port</code> of the 11581cb0ef41Sopenharmony_cisocket as reported by the operating system: 11591cb0ef41Sopenharmony_ci<code>{ port: 12346, family: 'IPv4', address: '127.0.0.1' }</code></p> 11601cb0ef41Sopenharmony_ci<h4><code>socket.autoSelectFamilyAttemptedAddresses</code><span><a class="mark" href="#socketautoselectfamilyattemptedaddresses" id="socketautoselectfamilyattemptedaddresses">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_autoselectfamilyattemptedaddresses"></a></h4> 11611cb0ef41Sopenharmony_ci<div class="api_metadata"> 11621cb0ef41Sopenharmony_ci<span>Added in: v18.18.0</span> 11631cb0ef41Sopenharmony_ci</div> 11641cb0ef41Sopenharmony_ci<ul> 11651cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 11661cb0ef41Sopenharmony_ci</ul> 11671cb0ef41Sopenharmony_ci<p>This property is only present if the family autoselection algorithm is enabled in 11681cb0ef41Sopenharmony_ci<a href="#socketconnectoptions-connectlistener"><code>socket.connect(options)</code></a> and it is an array of the addresses that have been attempted.</p> 11691cb0ef41Sopenharmony_ci<p>Each address is a string in the form of <code>$IP:$PORT</code>. If the connection was successful, 11701cb0ef41Sopenharmony_cithen the last address is the one that the socket is currently connected to.</p> 11711cb0ef41Sopenharmony_ci<h4><code>socket.bufferSize</code><span><a class="mark" href="#socketbuffersize" id="socketbuffersize">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_buffersize"></a></h4> 11721cb0ef41Sopenharmony_ci<div class="api_metadata"> 11731cb0ef41Sopenharmony_ci<span>Added in: v0.3.8</span><span>Deprecated since: v14.6.0</span> 11741cb0ef41Sopenharmony_ci</div> 11751cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="stream.html#writablewritablelength"><code>writable.writableLength</code></a> instead.</div><p></p> 11761cb0ef41Sopenharmony_ci<ul> 11771cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 11781cb0ef41Sopenharmony_ci</ul> 11791cb0ef41Sopenharmony_ci<p>This property shows the number of characters buffered for writing. The buffer 11801cb0ef41Sopenharmony_cimay contain strings whose length after encoding is not yet known. So this number 11811cb0ef41Sopenharmony_ciis only an approximation of the number of bytes in the buffer.</p> 11821cb0ef41Sopenharmony_ci<p><code>net.Socket</code> has the property that <code>socket.write()</code> always works. This is to 11831cb0ef41Sopenharmony_cihelp users get up and running quickly. The computer cannot always keep up 11841cb0ef41Sopenharmony_ciwith the amount of data that is written to a socket. The network connection 11851cb0ef41Sopenharmony_cisimply might be too slow. Node.js will internally queue up the data written to a 11861cb0ef41Sopenharmony_cisocket and send it out over the wire when it is possible.</p> 11871cb0ef41Sopenharmony_ci<p>The consequence of this internal buffering is that memory may grow. 11881cb0ef41Sopenharmony_ciUsers who experience large or growing <code>bufferSize</code> should attempt to 11891cb0ef41Sopenharmony_ci"throttle" the data flows in their program with 11901cb0ef41Sopenharmony_ci<a href="#socketpause"><code>socket.pause()</code></a> and <a href="#socketresume"><code>socket.resume()</code></a>.</p> 11911cb0ef41Sopenharmony_ci<h4><code>socket.bytesRead</code><span><a class="mark" href="#socketbytesread" id="socketbytesread">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_bytesread"></a></h4> 11921cb0ef41Sopenharmony_ci<div class="api_metadata"> 11931cb0ef41Sopenharmony_ci<span>Added in: v0.5.3</span> 11941cb0ef41Sopenharmony_ci</div> 11951cb0ef41Sopenharmony_ci<ul> 11961cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 11971cb0ef41Sopenharmony_ci</ul> 11981cb0ef41Sopenharmony_ci<p>The amount of received bytes.</p> 11991cb0ef41Sopenharmony_ci<h4><code>socket.bytesWritten</code><span><a class="mark" href="#socketbyteswritten" id="socketbyteswritten">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_byteswritten"></a></h4> 12001cb0ef41Sopenharmony_ci<div class="api_metadata"> 12011cb0ef41Sopenharmony_ci<span>Added in: v0.5.3</span> 12021cb0ef41Sopenharmony_ci</div> 12031cb0ef41Sopenharmony_ci<ul> 12041cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 12051cb0ef41Sopenharmony_ci</ul> 12061cb0ef41Sopenharmony_ci<p>The amount of bytes sent.</p> 12071cb0ef41Sopenharmony_ci<h4><code>socket.connect()</code><span><a class="mark" href="#socketconnect" id="socketconnect">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_connect"></a></h4> 12081cb0ef41Sopenharmony_ci<p>Initiate a connection on a given socket.</p> 12091cb0ef41Sopenharmony_ci<p>Possible signatures:</p> 12101cb0ef41Sopenharmony_ci<ul> 12111cb0ef41Sopenharmony_ci<li><a href="#socketconnectoptions-connectlistener"><code>socket.connect(options[, connectListener])</code></a></li> 12121cb0ef41Sopenharmony_ci<li><a href="#socketconnectpath-connectlistener"><code>socket.connect(path[, connectListener])</code></a> 12131cb0ef41Sopenharmony_cifor <a href="#ipc-support">IPC</a> connections.</li> 12141cb0ef41Sopenharmony_ci<li><a href="#socketconnectport-host-connectlistener"><code>socket.connect(port[, host][, connectListener])</code></a> 12151cb0ef41Sopenharmony_cifor TCP connections.</li> 12161cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netsocket" class="type"><net.Socket></a> The socket itself.</li> 12171cb0ef41Sopenharmony_ci</ul> 12181cb0ef41Sopenharmony_ci<p>This function is asynchronous. When the connection is established, the 12191cb0ef41Sopenharmony_ci<a href="#event-connect"><code>'connect'</code></a> event will be emitted. If there is a problem connecting, 12201cb0ef41Sopenharmony_ciinstead of a <a href="#event-connect"><code>'connect'</code></a> event, an <a href="#event-error_1"><code>'error'</code></a> event will be emitted with 12211cb0ef41Sopenharmony_cithe error passed to the <a href="#event-error_1"><code>'error'</code></a> listener. 12221cb0ef41Sopenharmony_ciThe last parameter <code>connectListener</code>, if supplied, will be added as a listener 12231cb0ef41Sopenharmony_cifor the <a href="#event-connect"><code>'connect'</code></a> event <strong>once</strong>.</p> 12241cb0ef41Sopenharmony_ci<p>This function should only be used for reconnecting a socket after 12251cb0ef41Sopenharmony_ci<code>'close'</code> has been emitted or otherwise it may lead to undefined 12261cb0ef41Sopenharmony_cibehavior.</p> 12271cb0ef41Sopenharmony_ci<h5><code>socket.connect(options[, connectListener])</code><span><a class="mark" href="#socketconnectoptions-connectlistener" id="socketconnectoptions-connectlistener">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_connect_options_connectlistener"></a></h5> 12281cb0ef41Sopenharmony_ci<div class="api_metadata"> 12291cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 12301cb0ef41Sopenharmony_ci<table> 12311cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 12321cb0ef41Sopenharmony_ci<tr><td>v18.18.0</td> 12331cb0ef41Sopenharmony_ci<td><p>The default value for autoSelectFamily option can be changed at runtime using <code>setDefaultAutoSelectFamily</code> or via the command line option <code>--enable-network-family-autoselection</code>.</p></td></tr> 12341cb0ef41Sopenharmony_ci<tr><td>v18.13.0</td> 12351cb0ef41Sopenharmony_ci<td><p>Added the <code>autoSelectFamily</code> option.</p></td></tr> 12361cb0ef41Sopenharmony_ci<tr><td>v17.7.0</td> 12371cb0ef41Sopenharmony_ci<td><p>The <code>noDelay</code>, <code>keepAlive</code>, and <code>keepAliveInitialDelay</code> options are supported now.</p></td></tr> 12381cb0ef41Sopenharmony_ci<tr><td>v12.10.0</td> 12391cb0ef41Sopenharmony_ci<td><p>Added <code>onread</code> option.</p></td></tr> 12401cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td> 12411cb0ef41Sopenharmony_ci<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> 12421cb0ef41Sopenharmony_ci<tr><td>v5.11.0</td> 12431cb0ef41Sopenharmony_ci<td><p>The <code>hints</code> option is supported now.</p></td></tr> 12441cb0ef41Sopenharmony_ci<tr><td>v0.1.90</td> 12451cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.90</span></p></td></tr> 12461cb0ef41Sopenharmony_ci</tbody></table> 12471cb0ef41Sopenharmony_ci</details> 12481cb0ef41Sopenharmony_ci</div> 12491cb0ef41Sopenharmony_ci<ul> 12501cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 12511cb0ef41Sopenharmony_ci<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="#socketconnect"><code>socket.connect()</code></a> 12521cb0ef41Sopenharmony_cimethods. Will be added as a listener for the <a href="#event-connect"><code>'connect'</code></a> event once.</li> 12531cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netsocket" class="type"><net.Socket></a> The socket itself.</li> 12541cb0ef41Sopenharmony_ci</ul> 12551cb0ef41Sopenharmony_ci<p>Initiate a connection on a given socket. Normally this method is not needed, 12561cb0ef41Sopenharmony_cithe socket should be created and opened with <a href="#netcreateconnection"><code>net.createConnection()</code></a>. Use 12571cb0ef41Sopenharmony_cithis only when implementing a custom Socket.</p> 12581cb0ef41Sopenharmony_ci<p>For TCP connections, available <code>options</code> are:</p> 12591cb0ef41Sopenharmony_ci<ul> 12601cb0ef41Sopenharmony_ci<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> 12611cb0ef41Sopenharmony_ci<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> 12621cb0ef41Sopenharmony_ci<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> 12631cb0ef41Sopenharmony_ci<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> 12641cb0ef41Sopenharmony_ci<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 12651cb0ef41Sopenharmony_ci<code>0</code> indicates that both IPv4 and IPv6 addresses are allowed. <strong>Default:</strong> <code>0</code>.</li> 12661cb0ef41Sopenharmony_ci<li><code>hints</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Optional <a href="dns.html#supported-getaddrinfo-flags"><code>dns.lookup()</code> hints</a>.</li> 12671cb0ef41Sopenharmony_ci<li><code>lookup</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Custom lookup function. <strong>Default:</strong> <a href="dns.html#dnslookuphostname-options-callback"><code>dns.lookup()</code></a>.</li> 12681cb0ef41Sopenharmony_ci<li><code>noDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If set to <code>true</code>, it disables the use of Nagle's algorithm immediately 12691cb0ef41Sopenharmony_ciafter the socket is established. <strong>Default:</strong> <code>false</code>.</li> 12701cb0ef41Sopenharmony_ci<li><code>keepAlive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If set to <code>true</code>, it enables keep-alive functionality on the socket 12711cb0ef41Sopenharmony_ciimmediately after the connection is established, similarly on what is done in 12721cb0ef41Sopenharmony_ci<a href="#socketsetkeepaliveenable-initialdelay"><code>socket.setKeepAlive([enable][, initialDelay])</code></a>. 12731cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>false</code>.</li> 12741cb0ef41Sopenharmony_ci<li><code>keepAliveInitialDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> If set to a positive number, it sets the initial delay before 12751cb0ef41Sopenharmony_cithe first keepalive probe is sent on an idle socket.<strong>Default:</strong> <code>0</code>.</li> 12761cb0ef41Sopenharmony_ci<li><code>autoSelectFamily</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a>: If set to <code>true</code>, it enables a family autodetection algorithm 12771cb0ef41Sopenharmony_cithat loosely implements section 5 of <a href="https://www.rfc-editor.org/rfc/rfc8305.txt">RFC 8305</a>. 12781cb0ef41Sopenharmony_ciThe <code>all</code> option passed to lookup is set to <code>true</code> and the sockets attempts to connect to all 12791cb0ef41Sopenharmony_ciobtained IPv6 and IPv4 addresses, in sequence, until a connection is established. 12801cb0ef41Sopenharmony_ciThe first returned AAAA address is tried first, then the first returned A address, 12811cb0ef41Sopenharmony_cithen the second returned AAAA address and so on. 12821cb0ef41Sopenharmony_ciEach connection attempt is given the amount of time specified by the <code>autoSelectFamilyAttemptTimeout</code> 12831cb0ef41Sopenharmony_cioption before timing out and trying the next address. 12841cb0ef41Sopenharmony_ciIgnored if the <code>family</code> option is not <code>0</code> or if <code>localAddress</code> is set. 12851cb0ef41Sopenharmony_ciConnection errors are not emitted if at least one connection succeeds. 12861cb0ef41Sopenharmony_ci<strong>Default:</strong> initially <code>false</code>, but it can be changed at runtime using <a href="#netsetdefaultautoselectfamilyvalue"><code>net.setDefaultAutoSelectFamily(value)</code></a> 12871cb0ef41Sopenharmony_cior via the command line option <code>--enable-network-family-autoselection</code>.</li> 12881cb0ef41Sopenharmony_ci<li><code>autoSelectFamilyAttemptTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a>: The amount of time in milliseconds to wait 12891cb0ef41Sopenharmony_cifor a connection attempt to finish before trying the next address when using the <code>autoSelectFamily</code> option. 12901cb0ef41Sopenharmony_ciIf set to a positive integer less than <code>10</code>, then the value <code>10</code> will be used instead. 12911cb0ef41Sopenharmony_ci<strong>Default:</strong> initially <code>250</code>, but it can be changed at runtime using <a href="#netsetdefaultautoselectfamilyattempttimeoutvalue"><code>net.setDefaultAutoSelectFamilyAttemptTimeout(value)</code></a></li> 12921cb0ef41Sopenharmony_ci</ul> 12931cb0ef41Sopenharmony_ci<p>For <a href="#ipc-support">IPC</a> connections, available <code>options</code> are:</p> 12941cb0ef41Sopenharmony_ci<ul> 12951cb0ef41Sopenharmony_ci<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. 12961cb0ef41Sopenharmony_ciSee <a href="#identifying-paths-for-ipc-connections">Identifying paths for IPC connections</a>. If provided, the TCP-specific 12971cb0ef41Sopenharmony_cioptions above are ignored.</li> 12981cb0ef41Sopenharmony_ci</ul> 12991cb0ef41Sopenharmony_ci<p>For both types, available <code>options</code> include:</p> 13001cb0ef41Sopenharmony_ci<ul> 13011cb0ef41Sopenharmony_ci<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> 13021cb0ef41Sopenharmony_ciand passed to the supplied <code>callback</code> when data arrives on the socket. 13031cb0ef41Sopenharmony_ciThis will cause the streaming functionality to not provide any data. 13041cb0ef41Sopenharmony_ciThe socket will emit events like <code>'error'</code>, <code>'end'</code>, and <code>'close'</code> 13051cb0ef41Sopenharmony_cias usual. Methods like <code>pause()</code> and <code>resume()</code> will also behave as 13061cb0ef41Sopenharmony_ciexpected. 13071cb0ef41Sopenharmony_ci<ul> 13081cb0ef41Sopenharmony_ci<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type"><Uint8Array></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Either a reusable chunk of memory to 13091cb0ef41Sopenharmony_ciuse for storing incoming data or a function that returns such.</li> 13101cb0ef41Sopenharmony_ci<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 13111cb0ef41Sopenharmony_cidata. Two arguments are passed to it: the number of bytes written to 13121cb0ef41Sopenharmony_ci<code>buffer</code> and a reference to <code>buffer</code>. Return <code>false</code> from this function to 13131cb0ef41Sopenharmony_ciimplicitly <code>pause()</code> the socket. This function will be executed in the 13141cb0ef41Sopenharmony_ciglobal context.</li> 13151cb0ef41Sopenharmony_ci</ul> 13161cb0ef41Sopenharmony_ci</li> 13171cb0ef41Sopenharmony_ci</ul> 13181cb0ef41Sopenharmony_ci<p>Following is an example of a client using the <code>onread</code> option:</p> 13191cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:net'</span>); 13201cb0ef41Sopenharmony_cinet.<span class="hljs-title function_">connect</span>({ 13211cb0ef41Sopenharmony_ci <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>, 13221cb0ef41Sopenharmony_ci <span class="hljs-attr">onread</span>: { 13231cb0ef41Sopenharmony_ci <span class="hljs-comment">// Reuses a 4KiB Buffer for every read from the socket.</span> 13241cb0ef41Sopenharmony_ci <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>), 13251cb0ef41Sopenharmony_ci <span class="hljs-attr">callback</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">nread, buf</span>) { 13261cb0ef41Sopenharmony_ci <span class="hljs-comment">// Received data is available in `buf` from 0 to `nread`.</span> 13271cb0ef41Sopenharmony_ci <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)); 13281cb0ef41Sopenharmony_ci }, 13291cb0ef41Sopenharmony_ci }, 13301cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 13311cb0ef41Sopenharmony_ci<h5><code>socket.connect(path[, connectListener])</code><span><a class="mark" href="#socketconnectpath-connectlistener" id="socketconnectpath-connectlistener">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_connect_path_connectlistener"></a></h5> 13321cb0ef41Sopenharmony_ci<ul> 13331cb0ef41Sopenharmony_ci<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 13341cb0ef41Sopenharmony_ci<a href="#identifying-paths-for-ipc-connections">Identifying paths for IPC connections</a>.</li> 13351cb0ef41Sopenharmony_ci<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="#socketconnect"><code>socket.connect()</code></a> 13361cb0ef41Sopenharmony_cimethods. Will be added as a listener for the <a href="#event-connect"><code>'connect'</code></a> event once.</li> 13371cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netsocket" class="type"><net.Socket></a> The socket itself.</li> 13381cb0ef41Sopenharmony_ci</ul> 13391cb0ef41Sopenharmony_ci<p>Initiate an <a href="#ipc-support">IPC</a> connection on the given socket.</p> 13401cb0ef41Sopenharmony_ci<p>Alias to 13411cb0ef41Sopenharmony_ci<a href="#socketconnectoptions-connectlistener"><code>socket.connect(options[, connectListener])</code></a> 13421cb0ef41Sopenharmony_cicalled with <code>{ path: path }</code> as <code>options</code>.</p> 13431cb0ef41Sopenharmony_ci<h5><code>socket.connect(port[, host][, connectListener])</code><span><a class="mark" href="#socketconnectport-host-connectlistener" id="socketconnectport-host-connectlistener">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_connect_port_host_connectlistener"></a></h5> 13441cb0ef41Sopenharmony_ci<div class="api_metadata"> 13451cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 13461cb0ef41Sopenharmony_ci</div> 13471cb0ef41Sopenharmony_ci<ul> 13481cb0ef41Sopenharmony_ci<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> 13491cb0ef41Sopenharmony_ci<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> 13501cb0ef41Sopenharmony_ci<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="#socketconnect"><code>socket.connect()</code></a> 13511cb0ef41Sopenharmony_cimethods. Will be added as a listener for the <a href="#event-connect"><code>'connect'</code></a> event once.</li> 13521cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netsocket" class="type"><net.Socket></a> The socket itself.</li> 13531cb0ef41Sopenharmony_ci</ul> 13541cb0ef41Sopenharmony_ci<p>Initiate a TCP connection on the given socket.</p> 13551cb0ef41Sopenharmony_ci<p>Alias to 13561cb0ef41Sopenharmony_ci<a href="#socketconnectoptions-connectlistener"><code>socket.connect(options[, connectListener])</code></a> 13571cb0ef41Sopenharmony_cicalled with <code>{port: port, host: host}</code> as <code>options</code>.</p> 13581cb0ef41Sopenharmony_ci<h4><code>socket.connecting</code><span><a class="mark" href="#socketconnecting" id="socketconnecting">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_connecting"></a></h4> 13591cb0ef41Sopenharmony_ci<div class="api_metadata"> 13601cb0ef41Sopenharmony_ci<span>Added in: v6.1.0</span> 13611cb0ef41Sopenharmony_ci</div> 13621cb0ef41Sopenharmony_ci<ul> 13631cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 13641cb0ef41Sopenharmony_ci</ul> 13651cb0ef41Sopenharmony_ci<p>If <code>true</code>, 13661cb0ef41Sopenharmony_ci<a href="#socketconnectoptions-connectlistener"><code>socket.connect(options[, connectListener])</code></a> was 13671cb0ef41Sopenharmony_cicalled and has not yet finished. It will stay <code>true</code> until the socket becomes 13681cb0ef41Sopenharmony_ciconnected, then it is set to <code>false</code> and the <code>'connect'</code> event is emitted. Note 13691cb0ef41Sopenharmony_cithat the 13701cb0ef41Sopenharmony_ci<a href="#socketconnectoptions-connectlistener"><code>socket.connect(options[, connectListener])</code></a> 13711cb0ef41Sopenharmony_cicallback is a listener for the <code>'connect'</code> event.</p> 13721cb0ef41Sopenharmony_ci<h4><code>socket.destroy([error])</code><span><a class="mark" href="#socketdestroyerror" id="socketdestroyerror">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_destroy_error"></a></h4> 13731cb0ef41Sopenharmony_ci<div class="api_metadata"> 13741cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 13751cb0ef41Sopenharmony_ci</div> 13761cb0ef41Sopenharmony_ci<ul> 13771cb0ef41Sopenharmony_ci<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 13781cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netsocket" class="type"><net.Socket></a></li> 13791cb0ef41Sopenharmony_ci</ul> 13801cb0ef41Sopenharmony_ci<p>Ensures that no more I/O activity happens on this socket. 13811cb0ef41Sopenharmony_ciDestroys the stream and closes the connection.</p> 13821cb0ef41Sopenharmony_ci<p>See <a href="stream.html#writabledestroyerror"><code>writable.destroy()</code></a> for further details.</p> 13831cb0ef41Sopenharmony_ci<h4><code>socket.destroyed</code><span><a class="mark" href="#socketdestroyed" id="socketdestroyed">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_destroyed"></a></h4> 13841cb0ef41Sopenharmony_ci<ul> 13851cb0ef41Sopenharmony_ci<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 13861cb0ef41Sopenharmony_ciconnection is destroyed no further data can be transferred using it.</li> 13871cb0ef41Sopenharmony_ci</ul> 13881cb0ef41Sopenharmony_ci<p>See <a href="stream.html#writabledestroyed"><code>writable.destroyed</code></a> for further details.</p> 13891cb0ef41Sopenharmony_ci<h4><code>socket.destroySoon()</code><span><a class="mark" href="#socketdestroysoon" id="socketdestroysoon">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_destroysoon"></a></h4> 13901cb0ef41Sopenharmony_ci<div class="api_metadata"> 13911cb0ef41Sopenharmony_ci<span>Added in: v0.3.4</span> 13921cb0ef41Sopenharmony_ci</div> 13931cb0ef41Sopenharmony_ci<p>Destroys the socket after all data is written. If the <code>'finish'</code> event was 13941cb0ef41Sopenharmony_cialready emitted the socket is destroyed immediately. If the socket is still 13951cb0ef41Sopenharmony_ciwritable it implicitly calls <code>socket.end()</code>.</p> 13961cb0ef41Sopenharmony_ci<h4><code>socket.end([data[, encoding]][, callback])</code><span><a class="mark" href="#socketenddata-encoding-callback" id="socketenddata-encoding-callback">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_end_data_encoding_callback"></a></h4> 13971cb0ef41Sopenharmony_ci<div class="api_metadata"> 13981cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 13991cb0ef41Sopenharmony_ci</div> 14001cb0ef41Sopenharmony_ci<ul> 14011cb0ef41Sopenharmony_ci<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#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> 14021cb0ef41Sopenharmony_ci<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> 14031cb0ef41Sopenharmony_ci<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> 14041cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netsocket" class="type"><net.Socket></a> The socket itself.</li> 14051cb0ef41Sopenharmony_ci</ul> 14061cb0ef41Sopenharmony_ci<p>Half-closes the socket. i.e., it sends a FIN packet. It is possible the 14071cb0ef41Sopenharmony_ciserver will still send some data.</p> 14081cb0ef41Sopenharmony_ci<p>See <a href="stream.html#writableendchunk-encoding-callback"><code>writable.end()</code></a> for further details.</p> 14091cb0ef41Sopenharmony_ci<h4><code>socket.localAddress</code><span><a class="mark" href="#socketlocaladdress" id="socketlocaladdress">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_localaddress"></a></h4> 14101cb0ef41Sopenharmony_ci<div class="api_metadata"> 14111cb0ef41Sopenharmony_ci<span>Added in: v0.9.6</span> 14121cb0ef41Sopenharmony_ci</div> 14131cb0ef41Sopenharmony_ci<ul> 14141cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 14151cb0ef41Sopenharmony_ci</ul> 14161cb0ef41Sopenharmony_ci<p>The string representation of the local IP address the remote client is 14171cb0ef41Sopenharmony_ciconnecting on. For example, in a server listening on <code>'0.0.0.0'</code>, if a client 14181cb0ef41Sopenharmony_ciconnects on <code>'192.168.1.1'</code>, the value of <code>socket.localAddress</code> would be 14191cb0ef41Sopenharmony_ci<code>'192.168.1.1'</code>.</p> 14201cb0ef41Sopenharmony_ci<h4><code>socket.localPort</code><span><a class="mark" href="#socketlocalport" id="socketlocalport">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_localport"></a></h4> 14211cb0ef41Sopenharmony_ci<div class="api_metadata"> 14221cb0ef41Sopenharmony_ci<span>Added in: v0.9.6</span> 14231cb0ef41Sopenharmony_ci</div> 14241cb0ef41Sopenharmony_ci<ul> 14251cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 14261cb0ef41Sopenharmony_ci</ul> 14271cb0ef41Sopenharmony_ci<p>The numeric representation of the local port. For example, <code>80</code> or <code>21</code>.</p> 14281cb0ef41Sopenharmony_ci<h4><code>socket.localFamily</code><span><a class="mark" href="#socketlocalfamily" id="socketlocalfamily">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_localfamily"></a></h4> 14291cb0ef41Sopenharmony_ci<div class="api_metadata"> 14301cb0ef41Sopenharmony_ci<span>Added in: v18.8.0</span> 14311cb0ef41Sopenharmony_ci</div> 14321cb0ef41Sopenharmony_ci<ul> 14331cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 14341cb0ef41Sopenharmony_ci</ul> 14351cb0ef41Sopenharmony_ci<p>The string representation of the local IP family. <code>'IPv4'</code> or <code>'IPv6'</code>.</p> 14361cb0ef41Sopenharmony_ci<h4><code>socket.pause()</code><span><a class="mark" href="#socketpause" id="socketpause">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_pause"></a></h4> 14371cb0ef41Sopenharmony_ci<ul> 14381cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netsocket" class="type"><net.Socket></a> The socket itself.</li> 14391cb0ef41Sopenharmony_ci</ul> 14401cb0ef41Sopenharmony_ci<p>Pauses the reading of data. That is, <a href="#event-data"><code>'data'</code></a> events will not be emitted. 14411cb0ef41Sopenharmony_ciUseful to throttle back an upload.</p> 14421cb0ef41Sopenharmony_ci<h4><code>socket.pending</code><span><a class="mark" href="#socketpending" id="socketpending">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_pending"></a></h4> 14431cb0ef41Sopenharmony_ci<div class="api_metadata"> 14441cb0ef41Sopenharmony_ci<span>Added in: v11.2.0, v10.16.0</span> 14451cb0ef41Sopenharmony_ci</div> 14461cb0ef41Sopenharmony_ci<ul> 14471cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 14481cb0ef41Sopenharmony_ci</ul> 14491cb0ef41Sopenharmony_ci<p>This is <code>true</code> if the socket is not connected yet, either because <code>.connect()</code> 14501cb0ef41Sopenharmony_cihas not yet been called or because it is still in the process of connecting 14511cb0ef41Sopenharmony_ci(see <a href="#socketconnecting"><code>socket.connecting</code></a>).</p> 14521cb0ef41Sopenharmony_ci<h4><code>socket.ref()</code><span><a class="mark" href="#socketref" id="socketref">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_ref"></a></h4> 14531cb0ef41Sopenharmony_ci<div class="api_metadata"> 14541cb0ef41Sopenharmony_ci<span>Added in: v0.9.1</span> 14551cb0ef41Sopenharmony_ci</div> 14561cb0ef41Sopenharmony_ci<ul> 14571cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netsocket" class="type"><net.Socket></a> The socket itself.</li> 14581cb0ef41Sopenharmony_ci</ul> 14591cb0ef41Sopenharmony_ci<p>Opposite of <code>unref()</code>, calling <code>ref()</code> on a previously <code>unref</code>ed socket will 14601cb0ef41Sopenharmony_ci<em>not</em> let the program exit if it's the only socket left (the default behavior). 14611cb0ef41Sopenharmony_ciIf the socket is <code>ref</code>ed calling <code>ref</code> again will have no effect.</p> 14621cb0ef41Sopenharmony_ci<h4><code>socket.remoteAddress</code><span><a class="mark" href="#socketremoteaddress" id="socketremoteaddress">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_remoteaddress"></a></h4> 14631cb0ef41Sopenharmony_ci<div class="api_metadata"> 14641cb0ef41Sopenharmony_ci<span>Added in: v0.5.10</span> 14651cb0ef41Sopenharmony_ci</div> 14661cb0ef41Sopenharmony_ci<ul> 14671cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 14681cb0ef41Sopenharmony_ci</ul> 14691cb0ef41Sopenharmony_ci<p>The string representation of the remote IP address. For example, 14701cb0ef41Sopenharmony_ci<code>'74.125.127.100'</code> or <code>'2001:4860:a005::68'</code>. Value may be <code>undefined</code> if 14711cb0ef41Sopenharmony_cithe socket is destroyed (for example, if the client disconnected).</p> 14721cb0ef41Sopenharmony_ci<h4><code>socket.remoteFamily</code><span><a class="mark" href="#socketremotefamily" id="socketremotefamily">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_remotefamily"></a></h4> 14731cb0ef41Sopenharmony_ci<div class="api_metadata"> 14741cb0ef41Sopenharmony_ci<span>Added in: v0.11.14</span> 14751cb0ef41Sopenharmony_ci</div> 14761cb0ef41Sopenharmony_ci<ul> 14771cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 14781cb0ef41Sopenharmony_ci</ul> 14791cb0ef41Sopenharmony_ci<p>The string representation of the remote IP family. <code>'IPv4'</code> or <code>'IPv6'</code>. Value may be <code>undefined</code> if 14801cb0ef41Sopenharmony_cithe socket is destroyed (for example, if the client disconnected).</p> 14811cb0ef41Sopenharmony_ci<h4><code>socket.remotePort</code><span><a class="mark" href="#socketremoteport" id="socketremoteport">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_remoteport"></a></h4> 14821cb0ef41Sopenharmony_ci<div class="api_metadata"> 14831cb0ef41Sopenharmony_ci<span>Added in: v0.5.10</span> 14841cb0ef41Sopenharmony_ci</div> 14851cb0ef41Sopenharmony_ci<ul> 14861cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 14871cb0ef41Sopenharmony_ci</ul> 14881cb0ef41Sopenharmony_ci<p>The numeric representation of the remote port. For example, <code>80</code> or <code>21</code>. Value may be <code>undefined</code> if 14891cb0ef41Sopenharmony_cithe socket is destroyed (for example, if the client disconnected).</p> 14901cb0ef41Sopenharmony_ci<h4><code>socket.resetAndDestroy()</code><span><a class="mark" href="#socketresetanddestroy" id="socketresetanddestroy">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_resetanddestroy"></a></h4> 14911cb0ef41Sopenharmony_ci<div class="api_metadata"> 14921cb0ef41Sopenharmony_ci<span>Added in: v18.3.0</span> 14931cb0ef41Sopenharmony_ci</div> 14941cb0ef41Sopenharmony_ci<ul> 14951cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netsocket" class="type"><net.Socket></a></li> 14961cb0ef41Sopenharmony_ci</ul> 14971cb0ef41Sopenharmony_ci<p>Close the TCP connection by sending an RST packet and destroy the stream. 14981cb0ef41Sopenharmony_ciIf this TCP socket is in connecting status, it will send an RST packet and destroy this TCP socket once it is connected. 14991cb0ef41Sopenharmony_ciOtherwise, it will call <code>socket.destroy</code> with an <code>ERR_SOCKET_CLOSED</code> Error. 15001cb0ef41Sopenharmony_ciIf this is not a TCP socket (for example, a pipe), calling this method will immediately throw an <code>ERR_INVALID_HANDLE_TYPE</code> Error.</p> 15011cb0ef41Sopenharmony_ci<h4><code>socket.resume()</code><span><a class="mark" href="#socketresume" id="socketresume">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_resume"></a></h4> 15021cb0ef41Sopenharmony_ci<ul> 15031cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netsocket" class="type"><net.Socket></a> The socket itself.</li> 15041cb0ef41Sopenharmony_ci</ul> 15051cb0ef41Sopenharmony_ci<p>Resumes reading after a call to <a href="#socketpause"><code>socket.pause()</code></a>.</p> 15061cb0ef41Sopenharmony_ci<h4><code>socket.setEncoding([encoding])</code><span><a class="mark" href="#socketsetencodingencoding" id="socketsetencodingencoding">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_setencoding_encoding"></a></h4> 15071cb0ef41Sopenharmony_ci<div class="api_metadata"> 15081cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 15091cb0ef41Sopenharmony_ci</div> 15101cb0ef41Sopenharmony_ci<ul> 15111cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 15121cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netsocket" class="type"><net.Socket></a> The socket itself.</li> 15131cb0ef41Sopenharmony_ci</ul> 15141cb0ef41Sopenharmony_ci<p>Set the encoding for the socket as a <a href="stream.html#class-streamreadable">Readable Stream</a>. See 15151cb0ef41Sopenharmony_ci<a href="stream.html#readablesetencodingencoding"><code>readable.setEncoding()</code></a> for more information.</p> 15161cb0ef41Sopenharmony_ci<h4><code>socket.setKeepAlive([enable][, initialDelay])</code><span><a class="mark" href="#socketsetkeepaliveenable-initialdelay" id="socketsetkeepaliveenable-initialdelay">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_setkeepalive_enable_initialdelay"></a></h4> 15171cb0ef41Sopenharmony_ci<div class="api_metadata"> 15181cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 15191cb0ef41Sopenharmony_ci<table> 15201cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 15211cb0ef41Sopenharmony_ci<tr><td>v13.12.0, v12.17.0</td> 15221cb0ef41Sopenharmony_ci<td><p>New defaults for <code>TCP_KEEPCNT</code> and <code>TCP_KEEPINTVL</code> socket options were added.</p></td></tr> 15231cb0ef41Sopenharmony_ci<tr><td>v0.1.92</td> 15241cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.92</span></p></td></tr> 15251cb0ef41Sopenharmony_ci</tbody></table> 15261cb0ef41Sopenharmony_ci</details> 15271cb0ef41Sopenharmony_ci</div> 15281cb0ef41Sopenharmony_ci<ul> 15291cb0ef41Sopenharmony_ci<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> 15301cb0ef41Sopenharmony_ci<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> 15311cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netsocket" class="type"><net.Socket></a> The socket itself.</li> 15321cb0ef41Sopenharmony_ci</ul> 15331cb0ef41Sopenharmony_ci<p>Enable/disable keep-alive functionality, and optionally set the initial 15341cb0ef41Sopenharmony_cidelay before the first keepalive probe is sent on an idle socket.</p> 15351cb0ef41Sopenharmony_ci<p>Set <code>initialDelay</code> (in milliseconds) to set the delay between the last 15361cb0ef41Sopenharmony_cidata packet received and the first keepalive probe. Setting <code>0</code> for 15371cb0ef41Sopenharmony_ci<code>initialDelay</code> will leave the value unchanged from the default 15381cb0ef41Sopenharmony_ci(or previous) setting.</p> 15391cb0ef41Sopenharmony_ci<p>Enabling the keep-alive functionality will set the following socket options:</p> 15401cb0ef41Sopenharmony_ci<ul> 15411cb0ef41Sopenharmony_ci<li><code>SO_KEEPALIVE=1</code></li> 15421cb0ef41Sopenharmony_ci<li><code>TCP_KEEPIDLE=initialDelay</code></li> 15431cb0ef41Sopenharmony_ci<li><code>TCP_KEEPCNT=10</code></li> 15441cb0ef41Sopenharmony_ci<li><code>TCP_KEEPINTVL=1</code></li> 15451cb0ef41Sopenharmony_ci</ul> 15461cb0ef41Sopenharmony_ci<h4><code>socket.setNoDelay([noDelay])</code><span><a class="mark" href="#socketsetnodelaynodelay" id="socketsetnodelaynodelay">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_setnodelay_nodelay"></a></h4> 15471cb0ef41Sopenharmony_ci<div class="api_metadata"> 15481cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 15491cb0ef41Sopenharmony_ci</div> 15501cb0ef41Sopenharmony_ci<ul> 15511cb0ef41Sopenharmony_ci<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> 15521cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netsocket" class="type"><net.Socket></a> The socket itself.</li> 15531cb0ef41Sopenharmony_ci</ul> 15541cb0ef41Sopenharmony_ci<p>Enable/disable the use of Nagle's algorithm.</p> 15551cb0ef41Sopenharmony_ci<p>When a TCP connection is created, it will have Nagle's algorithm enabled.</p> 15561cb0ef41Sopenharmony_ci<p>Nagle's algorithm delays data before it is sent via the network. It attempts 15571cb0ef41Sopenharmony_cito optimize throughput at the expense of latency.</p> 15581cb0ef41Sopenharmony_ci<p>Passing <code>true</code> for <code>noDelay</code> or not passing an argument will disable Nagle's 15591cb0ef41Sopenharmony_cialgorithm for the socket. Passing <code>false</code> for <code>noDelay</code> will enable Nagle's 15601cb0ef41Sopenharmony_cialgorithm.</p> 15611cb0ef41Sopenharmony_ci<h4><code>socket.setTimeout(timeout[, callback])</code><span><a class="mark" href="#socketsettimeouttimeout-callback" id="socketsettimeouttimeout-callback">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_settimeout_timeout_callback"></a></h4> 15621cb0ef41Sopenharmony_ci<div class="api_metadata"> 15631cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 15641cb0ef41Sopenharmony_ci<table> 15651cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 15661cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 15671cb0ef41Sopenharmony_ci<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 15681cb0ef41Sopenharmony_ci<tr><td>v0.1.90</td> 15691cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.90</span></p></td></tr> 15701cb0ef41Sopenharmony_ci</tbody></table> 15711cb0ef41Sopenharmony_ci</details> 15721cb0ef41Sopenharmony_ci</div> 15731cb0ef41Sopenharmony_ci<ul> 15741cb0ef41Sopenharmony_ci<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 15751cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 15761cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netsocket" class="type"><net.Socket></a> The socket itself.</li> 15771cb0ef41Sopenharmony_ci</ul> 15781cb0ef41Sopenharmony_ci<p>Sets the socket to timeout after <code>timeout</code> milliseconds of inactivity on 15791cb0ef41Sopenharmony_cithe socket. By default <code>net.Socket</code> do not have a timeout.</p> 15801cb0ef41Sopenharmony_ci<p>When an idle timeout is triggered the socket will receive a <a href="#event-timeout"><code>'timeout'</code></a> 15811cb0ef41Sopenharmony_cievent but the connection will not be severed. The user must manually call 15821cb0ef41Sopenharmony_ci<a href="#socketenddata-encoding-callback"><code>socket.end()</code></a> or <a href="#socketdestroyerror"><code>socket.destroy()</code></a> to end the connection.</p> 15831cb0ef41Sopenharmony_ci<pre><code class="language-js">socket.<span class="hljs-built_in">setTimeout</span>(<span class="hljs-number">3000</span>); 15841cb0ef41Sopenharmony_cisocket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'timeout'</span>, <span class="hljs-function">() =></span> { 15851cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'socket timeout'</span>); 15861cb0ef41Sopenharmony_ci socket.<span class="hljs-title function_">end</span>(); 15871cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 15881cb0ef41Sopenharmony_ci<p>If <code>timeout</code> is 0, then the existing idle timeout is disabled.</p> 15891cb0ef41Sopenharmony_ci<p>The optional <code>callback</code> parameter will be added as a one-time listener for the 15901cb0ef41Sopenharmony_ci<a href="#event-timeout"><code>'timeout'</code></a> event.</p> 15911cb0ef41Sopenharmony_ci<h4><code>socket.timeout</code><span><a class="mark" href="#sockettimeout" id="sockettimeout">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_timeout"></a></h4> 15921cb0ef41Sopenharmony_ci<div class="api_metadata"> 15931cb0ef41Sopenharmony_ci<span>Added in: v10.7.0</span> 15941cb0ef41Sopenharmony_ci</div> 15951cb0ef41Sopenharmony_ci<ul> 15961cb0ef41Sopenharmony_ci<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> 15971cb0ef41Sopenharmony_ci</ul> 15981cb0ef41Sopenharmony_ci<p>The socket timeout in milliseconds as set by <a href="#socketsettimeouttimeout-callback"><code>socket.setTimeout()</code></a>. 15991cb0ef41Sopenharmony_ciIt is <code>undefined</code> if a timeout has not been set.</p> 16001cb0ef41Sopenharmony_ci<h4><code>socket.unref()</code><span><a class="mark" href="#socketunref" id="socketunref">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_unref"></a></h4> 16011cb0ef41Sopenharmony_ci<div class="api_metadata"> 16021cb0ef41Sopenharmony_ci<span>Added in: v0.9.1</span> 16031cb0ef41Sopenharmony_ci</div> 16041cb0ef41Sopenharmony_ci<ul> 16051cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netsocket" class="type"><net.Socket></a> The socket itself.</li> 16061cb0ef41Sopenharmony_ci</ul> 16071cb0ef41Sopenharmony_ci<p>Calling <code>unref()</code> on a socket will allow the program to exit if this is the only 16081cb0ef41Sopenharmony_ciactive socket in the event system. If the socket is already <code>unref</code>ed calling 16091cb0ef41Sopenharmony_ci<code>unref()</code> again will have no effect.</p> 16101cb0ef41Sopenharmony_ci<h4><code>socket.write(data[, encoding][, callback])</code><span><a class="mark" href="#socketwritedata-encoding-callback" id="socketwritedata-encoding-callback">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_write_data_encoding_callback"></a></h4> 16111cb0ef41Sopenharmony_ci<div class="api_metadata"> 16121cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 16131cb0ef41Sopenharmony_ci</div> 16141cb0ef41Sopenharmony_ci<ul> 16151cb0ef41Sopenharmony_ci<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#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> 16161cb0ef41Sopenharmony_ci<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> 16171cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 16181cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 16191cb0ef41Sopenharmony_ci</ul> 16201cb0ef41Sopenharmony_ci<p>Sends data on the socket. The second parameter specifies the encoding in the 16211cb0ef41Sopenharmony_cicase of a string. It defaults to UTF8 encoding.</p> 16221cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if the entire data was flushed successfully to the kernel 16231cb0ef41Sopenharmony_cibuffer. Returns <code>false</code> if all or part of the data was queued in user memory. 16241cb0ef41Sopenharmony_ci<a href="#event-drain"><code>'drain'</code></a> will be emitted when the buffer is again free.</p> 16251cb0ef41Sopenharmony_ci<p>The optional <code>callback</code> parameter will be executed when the data is finally 16261cb0ef41Sopenharmony_ciwritten out, which may not be immediately.</p> 16271cb0ef41Sopenharmony_ci<p>See <code>Writable</code> stream <a href="stream.html#writablewritechunk-encoding-callback"><code>write()</code></a> method for more 16281cb0ef41Sopenharmony_ciinformation.</p> 16291cb0ef41Sopenharmony_ci<h4><code>socket.readyState</code><span><a class="mark" href="#socketreadystate" id="socketreadystate">#</a></span><a aria-hidden="true" class="legacy" id="net_socket_readystate"></a></h4> 16301cb0ef41Sopenharmony_ci<div class="api_metadata"> 16311cb0ef41Sopenharmony_ci<span>Added in: v0.5.0</span> 16321cb0ef41Sopenharmony_ci</div> 16331cb0ef41Sopenharmony_ci<ul> 16341cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 16351cb0ef41Sopenharmony_ci</ul> 16361cb0ef41Sopenharmony_ci<p>This property represents the state of the connection as a string.</p> 16371cb0ef41Sopenharmony_ci<ul> 16381cb0ef41Sopenharmony_ci<li>If the stream is connecting <code>socket.readyState</code> is <code>opening</code>.</li> 16391cb0ef41Sopenharmony_ci<li>If the stream is readable and writable, it is <code>open</code>.</li> 16401cb0ef41Sopenharmony_ci<li>If the stream is readable and not writable, it is <code>readOnly</code>.</li> 16411cb0ef41Sopenharmony_ci<li>If the stream is not readable and writable, it is <code>writeOnly</code>.</li> 16421cb0ef41Sopenharmony_ci</ul> 16431cb0ef41Sopenharmony_ci</section><section><h3><code>net.connect()</code><span><a class="mark" href="#netconnect" id="netconnect">#</a></span><a aria-hidden="true" class="legacy" id="net_net_connect"></a></h3> 16441cb0ef41Sopenharmony_ci<p>Aliases to 16451cb0ef41Sopenharmony_ci<a href="#netcreateconnection"><code>net.createConnection()</code></a>.</p> 16461cb0ef41Sopenharmony_ci<p>Possible signatures:</p> 16471cb0ef41Sopenharmony_ci<ul> 16481cb0ef41Sopenharmony_ci<li><a href="#netconnectoptions-connectlistener"><code>net.connect(options[, connectListener])</code></a></li> 16491cb0ef41Sopenharmony_ci<li><a href="#netconnectpath-connectlistener"><code>net.connect(path[, connectListener])</code></a> for <a href="#ipc-support">IPC</a> 16501cb0ef41Sopenharmony_ciconnections.</li> 16511cb0ef41Sopenharmony_ci<li><a href="#netconnectport-host-connectlistener"><code>net.connect(port[, host][, connectListener])</code></a> 16521cb0ef41Sopenharmony_cifor TCP connections.</li> 16531cb0ef41Sopenharmony_ci</ul> 16541cb0ef41Sopenharmony_ci<h4><code>net.connect(options[, connectListener])</code><span><a class="mark" href="#netconnectoptions-connectlistener" id="netconnectoptions-connectlistener">#</a></span><a aria-hidden="true" class="legacy" id="net_net_connect_options_connectlistener"></a></h4> 16551cb0ef41Sopenharmony_ci<div class="api_metadata"> 16561cb0ef41Sopenharmony_ci<span>Added in: v0.7.0</span> 16571cb0ef41Sopenharmony_ci</div> 16581cb0ef41Sopenharmony_ci<ul> 16591cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 16601cb0ef41Sopenharmony_ci<li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 16611cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netsocket" class="type"><net.Socket></a></li> 16621cb0ef41Sopenharmony_ci</ul> 16631cb0ef41Sopenharmony_ci<p>Alias to 16641cb0ef41Sopenharmony_ci<a href="#netcreateconnectionoptions-connectlistener"><code>net.createConnection(options[, connectListener])</code></a>.</p> 16651cb0ef41Sopenharmony_ci<h4><code>net.connect(path[, connectListener])</code><span><a class="mark" href="#netconnectpath-connectlistener" id="netconnectpath-connectlistener">#</a></span><a aria-hidden="true" class="legacy" id="net_net_connect_path_connectlistener"></a></h4> 16661cb0ef41Sopenharmony_ci<div class="api_metadata"> 16671cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 16681cb0ef41Sopenharmony_ci</div> 16691cb0ef41Sopenharmony_ci<ul> 16701cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 16711cb0ef41Sopenharmony_ci<li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 16721cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netsocket" class="type"><net.Socket></a></li> 16731cb0ef41Sopenharmony_ci</ul> 16741cb0ef41Sopenharmony_ci<p>Alias to 16751cb0ef41Sopenharmony_ci<a href="#netcreateconnectionpath-connectlistener"><code>net.createConnection(path[, connectListener])</code></a>.</p> 16761cb0ef41Sopenharmony_ci<h4><code>net.connect(port[, host][, connectListener])</code><span><a class="mark" href="#netconnectport-host-connectlistener" id="netconnectport-host-connectlistener">#</a></span><a aria-hidden="true" class="legacy" id="net_net_connect_port_host_connectlistener"></a></h4> 16771cb0ef41Sopenharmony_ci<div class="api_metadata"> 16781cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 16791cb0ef41Sopenharmony_ci</div> 16801cb0ef41Sopenharmony_ci<ul> 16811cb0ef41Sopenharmony_ci<li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 16821cb0ef41Sopenharmony_ci<li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 16831cb0ef41Sopenharmony_ci<li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 16841cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netsocket" class="type"><net.Socket></a></li> 16851cb0ef41Sopenharmony_ci</ul> 16861cb0ef41Sopenharmony_ci<p>Alias to 16871cb0ef41Sopenharmony_ci<a href="#netcreateconnectionport-host-connectlistener"><code>net.createConnection(port[, host][, connectListener])</code></a>.</p> 16881cb0ef41Sopenharmony_ci</section><section><h3><code>net.createConnection()</code><span><a class="mark" href="#netcreateconnection" id="netcreateconnection">#</a></span><a aria-hidden="true" class="legacy" id="net_net_createconnection"></a></h3> 16891cb0ef41Sopenharmony_ci<p>A factory function, which creates a new <a href="#class-netsocket"><code>net.Socket</code></a>, 16901cb0ef41Sopenharmony_ciimmediately initiates connection with <a href="#socketconnect"><code>socket.connect()</code></a>, 16911cb0ef41Sopenharmony_cithen returns the <code>net.Socket</code> that starts the connection.</p> 16921cb0ef41Sopenharmony_ci<p>When the connection is established, a <a href="#event-connect"><code>'connect'</code></a> event will be emitted 16931cb0ef41Sopenharmony_cion the returned socket. The last parameter <code>connectListener</code>, if supplied, 16941cb0ef41Sopenharmony_ciwill be added as a listener for the <a href="#event-connect"><code>'connect'</code></a> event <strong>once</strong>.</p> 16951cb0ef41Sopenharmony_ci<p>Possible signatures:</p> 16961cb0ef41Sopenharmony_ci<ul> 16971cb0ef41Sopenharmony_ci<li><a href="#netcreateconnectionoptions-connectlistener"><code>net.createConnection(options[, connectListener])</code></a></li> 16981cb0ef41Sopenharmony_ci<li><a href="#netcreateconnectionpath-connectlistener"><code>net.createConnection(path[, connectListener])</code></a> 16991cb0ef41Sopenharmony_cifor <a href="#ipc-support">IPC</a> connections.</li> 17001cb0ef41Sopenharmony_ci<li><a href="#netcreateconnectionport-host-connectlistener"><code>net.createConnection(port[, host][, connectListener])</code></a> 17011cb0ef41Sopenharmony_cifor TCP connections.</li> 17021cb0ef41Sopenharmony_ci</ul> 17031cb0ef41Sopenharmony_ci<p>The <a href="#netconnect"><code>net.connect()</code></a> function is an alias to this function.</p> 17041cb0ef41Sopenharmony_ci<h4><code>net.createConnection(options[, connectListener])</code><span><a class="mark" href="#netcreateconnectionoptions-connectlistener" id="netcreateconnectionoptions-connectlistener">#</a></span><a aria-hidden="true" class="legacy" id="net_net_createconnection_options_connectlistener"></a></h4> 17051cb0ef41Sopenharmony_ci<div class="api_metadata"> 17061cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 17071cb0ef41Sopenharmony_ci</div> 17081cb0ef41Sopenharmony_ci<ul> 17091cb0ef41Sopenharmony_ci<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 17101cb0ef41Sopenharmony_ci<a href="#new-netsocketoptions"><code>new net.Socket([options])</code></a> call and the 17111cb0ef41Sopenharmony_ci<a href="#socketconnectoptions-connectlistener"><code>socket.connect(options[, connectListener])</code></a> 17121cb0ef41Sopenharmony_cimethod.</li> 17131cb0ef41Sopenharmony_ci<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 17141cb0ef41Sopenharmony_ci<a href="#netcreateconnection"><code>net.createConnection()</code></a> functions. If supplied, will be added as 17151cb0ef41Sopenharmony_cia listener for the <a href="#event-connect"><code>'connect'</code></a> event on the returned socket once.</li> 17161cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netsocket" class="type"><net.Socket></a> The newly created socket used to start the connection.</li> 17171cb0ef41Sopenharmony_ci</ul> 17181cb0ef41Sopenharmony_ci<p>For available options, see 17191cb0ef41Sopenharmony_ci<a href="#new-netsocketoptions"><code>new net.Socket([options])</code></a> 17201cb0ef41Sopenharmony_ciand <a href="#socketconnectoptions-connectlistener"><code>socket.connect(options[, connectListener])</code></a>.</p> 17211cb0ef41Sopenharmony_ci<p>Additional options:</p> 17221cb0ef41Sopenharmony_ci<ul> 17231cb0ef41Sopenharmony_ci<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 17241cb0ef41Sopenharmony_ci<a href="#socketsettimeouttimeout-callback"><code>socket.setTimeout(timeout)</code></a> after the socket is created, but before 17251cb0ef41Sopenharmony_ciit starts the connection.</li> 17261cb0ef41Sopenharmony_ci</ul> 17271cb0ef41Sopenharmony_ci<p>Following is an example of a client of the echo server described 17281cb0ef41Sopenharmony_ciin the <a href="#netcreateserveroptions-connectionlistener"><code>net.createServer()</code></a> section:</p> 17291cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:net'</span>); 17301cb0ef41Sopenharmony_ci<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> { 17311cb0ef41Sopenharmony_ci <span class="hljs-comment">// 'connect' listener.</span> 17321cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'connected to server!'</span>); 17331cb0ef41Sopenharmony_ci client.<span class="hljs-title function_">write</span>(<span class="hljs-string">'world!\r\n'</span>); 17341cb0ef41Sopenharmony_ci}); 17351cb0ef41Sopenharmony_ciclient.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> { 17361cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(data.<span class="hljs-title function_">toString</span>()); 17371cb0ef41Sopenharmony_ci client.<span class="hljs-title function_">end</span>(); 17381cb0ef41Sopenharmony_ci}); 17391cb0ef41Sopenharmony_ciclient.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> { 17401cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'disconnected from server'</span>); 17411cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 17421cb0ef41Sopenharmony_ci<p>To connect on the socket <code>/tmp/echo.sock</code>:</p> 17431cb0ef41Sopenharmony_ci<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> <button class="copy-button">copy</button></pre> 17441cb0ef41Sopenharmony_ci<h4><code>net.createConnection(path[, connectListener])</code><span><a class="mark" href="#netcreateconnectionpath-connectlistener" id="netcreateconnectionpath-connectlistener">#</a></span><a aria-hidden="true" class="legacy" id="net_net_createconnection_path_connectlistener"></a></h4> 17451cb0ef41Sopenharmony_ci<div class="api_metadata"> 17461cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 17471cb0ef41Sopenharmony_ci</div> 17481cb0ef41Sopenharmony_ci<ul> 17491cb0ef41Sopenharmony_ci<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 17501cb0ef41Sopenharmony_ci<a href="#socketconnectpath-connectlistener"><code>socket.connect(path[, connectListener])</code></a>. 17511cb0ef41Sopenharmony_ciSee <a href="#identifying-paths-for-ipc-connections">Identifying paths for IPC connections</a>.</li> 17521cb0ef41Sopenharmony_ci<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 17531cb0ef41Sopenharmony_ci<a href="#netcreateconnection"><code>net.createConnection()</code></a> functions, an "once" listener for the 17541cb0ef41Sopenharmony_ci<code>'connect'</code> event on the initiating socket. Will be passed to 17551cb0ef41Sopenharmony_ci<a href="#socketconnectpath-connectlistener"><code>socket.connect(path[, connectListener])</code></a>.</li> 17561cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netsocket" class="type"><net.Socket></a> The newly created socket used to start the connection.</li> 17571cb0ef41Sopenharmony_ci</ul> 17581cb0ef41Sopenharmony_ci<p>Initiates an <a href="#ipc-support">IPC</a> connection.</p> 17591cb0ef41Sopenharmony_ci<p>This function creates a new <a href="#class-netsocket"><code>net.Socket</code></a> with all options set to default, 17601cb0ef41Sopenharmony_ciimmediately initiates connection with 17611cb0ef41Sopenharmony_ci<a href="#socketconnectpath-connectlistener"><code>socket.connect(path[, connectListener])</code></a>, 17621cb0ef41Sopenharmony_cithen returns the <code>net.Socket</code> that starts the connection.</p> 17631cb0ef41Sopenharmony_ci<h4><code>net.createConnection(port[, host][, connectListener])</code><span><a class="mark" href="#netcreateconnectionport-host-connectlistener" id="netcreateconnectionport-host-connectlistener">#</a></span><a aria-hidden="true" class="legacy" id="net_net_createconnection_port_host_connectlistener"></a></h4> 17641cb0ef41Sopenharmony_ci<div class="api_metadata"> 17651cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 17661cb0ef41Sopenharmony_ci</div> 17671cb0ef41Sopenharmony_ci<ul> 17681cb0ef41Sopenharmony_ci<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 17691cb0ef41Sopenharmony_ci<a href="#socketconnectport-host-connectlistener"><code>socket.connect(port[, host][, connectListener])</code></a>.</li> 17701cb0ef41Sopenharmony_ci<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 17711cb0ef41Sopenharmony_ci<a href="#socketconnectport-host-connectlistener"><code>socket.connect(port[, host][, connectListener])</code></a>. 17721cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>'localhost'</code>.</li> 17731cb0ef41Sopenharmony_ci<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 17741cb0ef41Sopenharmony_ci<a href="#netcreateconnection"><code>net.createConnection()</code></a> functions, an "once" listener for the 17751cb0ef41Sopenharmony_ci<code>'connect'</code> event on the initiating socket. Will be passed to 17761cb0ef41Sopenharmony_ci<a href="#socketconnectport-host-connectlistener"><code>socket.connect(port[, host][, connectListener])</code></a>.</li> 17771cb0ef41Sopenharmony_ci<li>Returns: <a href="net.html#class-netsocket" class="type"><net.Socket></a> The newly created socket used to start the connection.</li> 17781cb0ef41Sopenharmony_ci</ul> 17791cb0ef41Sopenharmony_ci<p>Initiates a TCP connection.</p> 17801cb0ef41Sopenharmony_ci<p>This function creates a new <a href="#class-netsocket"><code>net.Socket</code></a> with all options set to default, 17811cb0ef41Sopenharmony_ciimmediately initiates connection with 17821cb0ef41Sopenharmony_ci<a href="#socketconnectport-host-connectlistener"><code>socket.connect(port[, host][, connectListener])</code></a>, 17831cb0ef41Sopenharmony_cithen returns the <code>net.Socket</code> that starts the connection.</p> 17841cb0ef41Sopenharmony_ci</section><section><h3><code>net.createServer([options][, connectionListener])</code><span><a class="mark" href="#netcreateserveroptions-connectionlistener" id="netcreateserveroptions-connectionlistener">#</a></span><a aria-hidden="true" class="legacy" id="net_net_createserver_options_connectionlistener"></a></h3> 17851cb0ef41Sopenharmony_ci<div class="api_metadata"> 17861cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 17871cb0ef41Sopenharmony_ci<table> 17881cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 17891cb0ef41Sopenharmony_ci<tr><td>v18.17.0</td> 17901cb0ef41Sopenharmony_ci<td><p>The <code>highWaterMark</code> option is supported now.</p></td></tr> 17911cb0ef41Sopenharmony_ci<tr><td>v17.7.0, v16.15.0</td> 17921cb0ef41Sopenharmony_ci<td><p>The <code>noDelay</code>, <code>keepAlive</code>, and <code>keepAliveInitialDelay</code> options are supported now.</p></td></tr> 17931cb0ef41Sopenharmony_ci<tr><td>v0.5.0</td> 17941cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.0</span></p></td></tr> 17951cb0ef41Sopenharmony_ci</tbody></table> 17961cb0ef41Sopenharmony_ci</details> 17971cb0ef41Sopenharmony_ci</div> 17981cb0ef41Sopenharmony_ci<ul> 17991cb0ef41Sopenharmony_ci<li> 18001cb0ef41Sopenharmony_ci<p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></p> 18011cb0ef41Sopenharmony_ci<ul> 18021cb0ef41Sopenharmony_ci<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 18031cb0ef41Sopenharmony_ciautomatically end the writable side when the readable side ends. 18041cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>false</code>.</li> 18051cb0ef41Sopenharmony_ci<li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Optionally overrides all <a href="#class-netsocket"><code>net.Socket</code></a>s' 18061cb0ef41Sopenharmony_ci<code>readableHighWaterMark</code> and <code>writableHighWaterMark</code>. 18071cb0ef41Sopenharmony_ci<strong>Default:</strong> See <a href="stream.html#streamgetdefaulthighwatermarkobjectmode"><code>stream.getDefaultHighWaterMark()</code></a>.</li> 18081cb0ef41Sopenharmony_ci<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 18091cb0ef41Sopenharmony_cipaused on incoming connections. <strong>Default:</strong> <code>false</code>.</li> 18101cb0ef41Sopenharmony_ci<li><code>noDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If set to <code>true</code>, it disables the use of Nagle's algorithm immediately 18111cb0ef41Sopenharmony_ciafter a new incoming connection is received. <strong>Default:</strong> <code>false</code>.</li> 18121cb0ef41Sopenharmony_ci<li><code>keepAlive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If set to <code>true</code>, it enables keep-alive functionality on the socket 18131cb0ef41Sopenharmony_ciimmediately after a new incoming connection is received, similarly on what is done in 18141cb0ef41Sopenharmony_ci<a href="#socketsetkeepaliveenable-initialdelay"><code>socket.setKeepAlive([enable][, initialDelay])</code></a>. 18151cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>false</code>.</li> 18161cb0ef41Sopenharmony_ci<li><code>keepAliveInitialDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> If set to a positive number, it sets the initial delay before 18171cb0ef41Sopenharmony_cithe first keepalive probe is sent on an idle socket.<strong>Default:</strong> <code>0</code>.</li> 18181cb0ef41Sopenharmony_ci</ul> 18191cb0ef41Sopenharmony_ci</li> 18201cb0ef41Sopenharmony_ci<li> 18211cb0ef41Sopenharmony_ci<p><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 18221cb0ef41Sopenharmony_ci<a href="#event-connection"><code>'connection'</code></a> event.</p> 18231cb0ef41Sopenharmony_ci</li> 18241cb0ef41Sopenharmony_ci<li> 18251cb0ef41Sopenharmony_ci<p>Returns: <a href="net.html#class-netserver" class="type"><net.Server></a></p> 18261cb0ef41Sopenharmony_ci</li> 18271cb0ef41Sopenharmony_ci</ul> 18281cb0ef41Sopenharmony_ci<p>Creates a new TCP or <a href="#ipc-support">IPC</a> server.</p> 18291cb0ef41Sopenharmony_ci<p>If <code>allowHalfOpen</code> is set to <code>true</code>, when the other end of the socket 18301cb0ef41Sopenharmony_cisignals the end of transmission, the server will only send back the end of 18311cb0ef41Sopenharmony_citransmission when <a href="#socketenddata-encoding-callback"><code>socket.end()</code></a> is explicitly called. For example, in the 18321cb0ef41Sopenharmony_cicontext of TCP, when a FIN packed is received, a FIN packed is sent 18331cb0ef41Sopenharmony_ciback only when <a href="#socketenddata-encoding-callback"><code>socket.end()</code></a> is explicitly called. Until then the 18341cb0ef41Sopenharmony_ciconnection is half-closed (non-readable but still writable). See <a href="#event-end"><code>'end'</code></a> 18351cb0ef41Sopenharmony_cievent and <a href="https://tools.ietf.org/html/rfc1122">RFC 1122</a> (section 4.2.2.13) for more information.</p> 18361cb0ef41Sopenharmony_ci<p>If <code>pauseOnConnect</code> is set to <code>true</code>, then the socket associated with each 18371cb0ef41Sopenharmony_ciincoming connection will be paused, and no data will be read from its handle. 18381cb0ef41Sopenharmony_ciThis allows connections to be passed between processes without any data being 18391cb0ef41Sopenharmony_ciread by the original process. To begin reading data from a paused socket, call 18401cb0ef41Sopenharmony_ci<a href="#socketresume"><code>socket.resume()</code></a>.</p> 18411cb0ef41Sopenharmony_ci<p>The server can be a TCP server or an <a href="#ipc-support">IPC</a> server, depending on what it 18421cb0ef41Sopenharmony_ci<a href="#serverlisten"><code>listen()</code></a> to.</p> 18431cb0ef41Sopenharmony_ci<p>Here is an example of a TCP echo server which listens for connections 18441cb0ef41Sopenharmony_cion port 8124:</p> 18451cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:net'</span>); 18461cb0ef41Sopenharmony_ci<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> { 18471cb0ef41Sopenharmony_ci <span class="hljs-comment">// 'connection' listener.</span> 18481cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'client connected'</span>); 18491cb0ef41Sopenharmony_ci c.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> { 18501cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'client disconnected'</span>); 18511cb0ef41Sopenharmony_ci }); 18521cb0ef41Sopenharmony_ci c.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello\r\n'</span>); 18531cb0ef41Sopenharmony_ci c.<span class="hljs-title function_">pipe</span>(c); 18541cb0ef41Sopenharmony_ci}); 18551cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 18561cb0ef41Sopenharmony_ci <span class="hljs-keyword">throw</span> err; 18571cb0ef41Sopenharmony_ci}); 18581cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8124</span>, <span class="hljs-function">() =></span> { 18591cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'server bound'</span>); 18601cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 18611cb0ef41Sopenharmony_ci<p>Test this by using <code>telnet</code>:</p> 18621cb0ef41Sopenharmony_ci<pre><code class="language-console"><span class="hljs-meta prompt_">$ </span><span class="language-bash">telnet localhost 8124</span></code> <button class="copy-button">copy</button></pre> 18631cb0ef41Sopenharmony_ci<p>To listen on the socket <code>/tmp/echo.sock</code>:</p> 18641cb0ef41Sopenharmony_ci<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> { 18651cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'server bound'</span>); 18661cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 18671cb0ef41Sopenharmony_ci<p>Use <code>nc</code> to connect to a Unix domain socket server:</p> 18681cb0ef41Sopenharmony_ci<pre><code class="language-console"><span class="hljs-meta prompt_">$ </span><span class="language-bash">nc -U /tmp/echo.sock</span></code> <button class="copy-button">copy</button></pre> 18691cb0ef41Sopenharmony_ci</section><section><h3><code>net.getDefaultAutoSelectFamily()</code><span><a class="mark" href="#netgetdefaultautoselectfamily" id="netgetdefaultautoselectfamily">#</a></span><a aria-hidden="true" class="legacy" id="net_net_getdefaultautoselectfamily"></a></h3> 18701cb0ef41Sopenharmony_ci<div class="api_metadata"> 18711cb0ef41Sopenharmony_ci<span>Added in: v19.4.0</span> 18721cb0ef41Sopenharmony_ci</div> 18731cb0ef41Sopenharmony_ci<p>Gets the current default value of the <code>autoSelectFamily</code> option of <a href="#socketconnectoptions-connectlistener"><code>socket.connect(options)</code></a>.</p> 18741cb0ef41Sopenharmony_ci<ul> 18751cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> The current default value of the <code>autoSelectFamily</code> option.</li> 18761cb0ef41Sopenharmony_ci</ul> 18771cb0ef41Sopenharmony_ci</section><section><h3><code>net.setDefaultAutoSelectFamily(value)</code><span><a class="mark" href="#netsetdefaultautoselectfamilyvalue" id="netsetdefaultautoselectfamilyvalue">#</a></span><a aria-hidden="true" class="legacy" id="net_net_setdefaultautoselectfamily_value"></a></h3> 18781cb0ef41Sopenharmony_ci<div class="api_metadata"> 18791cb0ef41Sopenharmony_ci<span>Added in: v19.4.0</span> 18801cb0ef41Sopenharmony_ci</div> 18811cb0ef41Sopenharmony_ci<p>Sets the default value of the <code>autoSelectFamily</code> option of <a href="#socketconnectoptions-connectlistener"><code>socket.connect(options)</code></a>.</p> 18821cb0ef41Sopenharmony_ci<ul> 18831cb0ef41Sopenharmony_ci<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> The new default value. The initial default value is <code>false</code>.</li> 18841cb0ef41Sopenharmony_ci</ul> 18851cb0ef41Sopenharmony_ci</section><section><h3><code>net.getDefaultAutoSelectFamilyAttemptTimeout()</code><span><a class="mark" href="#netgetdefaultautoselectfamilyattempttimeout" id="netgetdefaultautoselectfamilyattempttimeout">#</a></span><a aria-hidden="true" class="legacy" id="net_net_getdefaultautoselectfamilyattempttimeout"></a></h3> 18861cb0ef41Sopenharmony_ci<div class="api_metadata"> 18871cb0ef41Sopenharmony_ci<span>Added in: v18.18.0</span> 18881cb0ef41Sopenharmony_ci</div> 18891cb0ef41Sopenharmony_ci<p>Gets the current default value of the <code>autoSelectFamilyAttemptTimeout</code> option of <a href="#socketconnectoptions-connectlistener"><code>socket.connect(options)</code></a>.</p> 18901cb0ef41Sopenharmony_ci<ul> 18911cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The current default value of the <code>autoSelectFamilyAttemptTimeout</code> option.</li> 18921cb0ef41Sopenharmony_ci</ul> 18931cb0ef41Sopenharmony_ci</section><section><h3><code>net.setDefaultAutoSelectFamilyAttemptTimeout(value)</code><span><a class="mark" href="#netsetdefaultautoselectfamilyattempttimeoutvalue" id="netsetdefaultautoselectfamilyattempttimeoutvalue">#</a></span><a aria-hidden="true" class="legacy" id="net_net_setdefaultautoselectfamilyattempttimeout_value"></a></h3> 18941cb0ef41Sopenharmony_ci<div class="api_metadata"> 18951cb0ef41Sopenharmony_ci<span>Added in: v18.18.0</span> 18961cb0ef41Sopenharmony_ci</div> 18971cb0ef41Sopenharmony_ci<p>Sets the default value of the <code>autoSelectFamilyAttemptTimeout</code> option of <a href="#socketconnectoptions-connectlistener"><code>socket.connect(options)</code></a>.</p> 18981cb0ef41Sopenharmony_ci<ul> 18991cb0ef41Sopenharmony_ci<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The new default value, which must be a positive number. If the number is less than <code>10</code>, 19001cb0ef41Sopenharmony_cithe value <code>10</code> is used instead. The initial default value is <code>250</code>.</li> 19011cb0ef41Sopenharmony_ci</ul> 19021cb0ef41Sopenharmony_ci</section><section><h3><code>net.isIP(input)</code><span><a class="mark" href="#netisipinput" id="netisipinput">#</a></span><a aria-hidden="true" class="legacy" id="net_net_isip_input"></a></h3> 19031cb0ef41Sopenharmony_ci<div class="api_metadata"> 19041cb0ef41Sopenharmony_ci<span>Added in: v0.3.0</span> 19051cb0ef41Sopenharmony_ci</div> 19061cb0ef41Sopenharmony_ci<ul> 19071cb0ef41Sopenharmony_ci<li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 19081cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 19091cb0ef41Sopenharmony_ci</ul> 19101cb0ef41Sopenharmony_ci<p>Returns <code>6</code> if <code>input</code> is an IPv6 address. Returns <code>4</code> if <code>input</code> is an IPv4 19111cb0ef41Sopenharmony_ciaddress in <a href="https://en.wikipedia.org/wiki/Dot-decimal_notation">dot-decimal notation</a> with no leading zeroes. Otherwise, returns 19121cb0ef41Sopenharmony_ci<code>0</code>.</p> 19131cb0ef41Sopenharmony_ci<pre><code class="language-js">net.<span class="hljs-title function_">isIP</span>(<span class="hljs-string">'::1'</span>); <span class="hljs-comment">// returns 6</span> 19141cb0ef41Sopenharmony_cinet.<span class="hljs-title function_">isIP</span>(<span class="hljs-string">'127.0.0.1'</span>); <span class="hljs-comment">// returns 4</span> 19151cb0ef41Sopenharmony_cinet.<span class="hljs-title function_">isIP</span>(<span class="hljs-string">'127.000.000.001'</span>); <span class="hljs-comment">// returns 0</span> 19161cb0ef41Sopenharmony_cinet.<span class="hljs-title function_">isIP</span>(<span class="hljs-string">'127.0.0.1/24'</span>); <span class="hljs-comment">// returns 0</span> 19171cb0ef41Sopenharmony_cinet.<span class="hljs-title function_">isIP</span>(<span class="hljs-string">'fhqwhgads'</span>); <span class="hljs-comment">// returns 0</span></code> <button class="copy-button">copy</button></pre> 19181cb0ef41Sopenharmony_ci</section><section><h3><code>net.isIPv4(input)</code><span><a class="mark" href="#netisipv4input" id="netisipv4input">#</a></span><a aria-hidden="true" class="legacy" id="net_net_isipv4_input"></a></h3> 19191cb0ef41Sopenharmony_ci<div class="api_metadata"> 19201cb0ef41Sopenharmony_ci<span>Added in: v0.3.0</span> 19211cb0ef41Sopenharmony_ci</div> 19221cb0ef41Sopenharmony_ci<ul> 19231cb0ef41Sopenharmony_ci<li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 19241cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 19251cb0ef41Sopenharmony_ci</ul> 19261cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if <code>input</code> is an IPv4 address in <a href="https://en.wikipedia.org/wiki/Dot-decimal_notation">dot-decimal notation</a> with no 19271cb0ef41Sopenharmony_cileading zeroes. Otherwise, returns <code>false</code>.</p> 19281cb0ef41Sopenharmony_ci<pre><code class="language-js">net.<span class="hljs-title function_">isIPv4</span>(<span class="hljs-string">'127.0.0.1'</span>); <span class="hljs-comment">// returns true</span> 19291cb0ef41Sopenharmony_cinet.<span class="hljs-title function_">isIPv4</span>(<span class="hljs-string">'127.000.000.001'</span>); <span class="hljs-comment">// returns false</span> 19301cb0ef41Sopenharmony_cinet.<span class="hljs-title function_">isIPv4</span>(<span class="hljs-string">'127.0.0.1/24'</span>); <span class="hljs-comment">// returns false</span> 19311cb0ef41Sopenharmony_cinet.<span class="hljs-title function_">isIPv4</span>(<span class="hljs-string">'fhqwhgads'</span>); <span class="hljs-comment">// returns false</span></code> <button class="copy-button">copy</button></pre> 19321cb0ef41Sopenharmony_ci</section><section><h3><code>net.isIPv6(input)</code><span><a class="mark" href="#netisipv6input" id="netisipv6input">#</a></span><a aria-hidden="true" class="legacy" id="net_net_isipv6_input"></a></h3> 19331cb0ef41Sopenharmony_ci<div class="api_metadata"> 19341cb0ef41Sopenharmony_ci<span>Added in: v0.3.0</span> 19351cb0ef41Sopenharmony_ci</div> 19361cb0ef41Sopenharmony_ci<ul> 19371cb0ef41Sopenharmony_ci<li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 19381cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 19391cb0ef41Sopenharmony_ci</ul> 19401cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if <code>input</code> is an IPv6 address. Otherwise, returns <code>false</code>.</p> 19411cb0ef41Sopenharmony_ci<pre><code class="language-js">net.<span class="hljs-title function_">isIPv6</span>(<span class="hljs-string">'::1'</span>); <span class="hljs-comment">// returns true</span> 19421cb0ef41Sopenharmony_cinet.<span class="hljs-title function_">isIPv6</span>(<span class="hljs-string">'fhqwhgads'</span>); <span class="hljs-comment">// returns false</span></code> <button class="copy-button">copy</button></pre></section> 19431cb0ef41Sopenharmony_ci <!-- API END --> 19441cb0ef41Sopenharmony_ci </div> 19451cb0ef41Sopenharmony_ci </div> 19461cb0ef41Sopenharmony_ci </div> 19471cb0ef41Sopenharmony_ci</body> 19481cb0ef41Sopenharmony_ci</html> 1949