• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html>
2<html lang="en">
3<head>
4  <meta charset="utf-8">
5  <meta name="viewport" content="width=device-width">
6  <meta name="nodejs.org:node-version" content="v18.20.1">
7  <title>Net | Node.js v18.20.1 Documentation</title>
8  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
9  <link rel="stylesheet" href="assets/style.css">
10  <link rel="stylesheet" href="assets/hljs.css">
11  <link rel="canonical" href="https://nodejs.org/api/net.html">
12  <script async defer src="assets/api.js" type="text/javascript"></script>
13
14</head>
15<body class="alt apidoc" id="api-section-net">
16  <div id="content" class="clearfix">
17    <div id="column2" class="interior">
18      <div id="intro" class="interior">
19        <a href="/" title="Go back to the home page">
20          Node.js
21        </a>
22      </div>
23      <ul>
24<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
25<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
26</ul>
27<hr class="line">
28<ul>
29<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
30<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
31<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
32<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
33<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
34<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
35<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
36<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
37<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
38<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
39<li><a href="console.html" class="nav-console">Console</a></li>
40<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
41<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
42<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
43<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
44<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
45<li><a href="dns.html" class="nav-dns">DNS</a></li>
46<li><a href="domain.html" class="nav-domain">Domain</a></li>
47<li><a href="errors.html" class="nav-errors">Errors</a></li>
48<li><a href="events.html" class="nav-events">Events</a></li>
49<li><a href="fs.html" class="nav-fs">File system</a></li>
50<li><a href="globals.html" class="nav-globals">Globals</a></li>
51<li><a href="http.html" class="nav-http">HTTP</a></li>
52<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
53<li><a href="https.html" class="nav-https">HTTPS</a></li>
54<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
55<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
56<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
57<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
58<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
59<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
60<li><a href="net.html" class="nav-net active">Net</a></li>
61<li><a href="os.html" class="nav-os">OS</a></li>
62<li><a href="path.html" class="nav-path">Path</a></li>
63<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
64<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
65<li><a href="process.html" class="nav-process">Process</a></li>
66<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
67<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
68<li><a href="readline.html" class="nav-readline">Readline</a></li>
69<li><a href="repl.html" class="nav-repl">REPL</a></li>
70<li><a href="report.html" class="nav-report">Report</a></li>
71<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
72<li><a href="stream.html" class="nav-stream">Stream</a></li>
73<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
74<li><a href="test.html" class="nav-test">Test runner</a></li>
75<li><a href="timers.html" class="nav-timers">Timers</a></li>
76<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
77<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
78<li><a href="tty.html" class="nav-tty">TTY</a></li>
79<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
80<li><a href="url.html" class="nav-url">URL</a></li>
81<li><a href="util.html" class="nav-util">Utilities</a></li>
82<li><a href="v8.html" class="nav-v8">V8</a></li>
83<li><a href="vm.html" class="nav-vm">VM</a></li>
84<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
85<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
86<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
87<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
88<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
89</ul>
90<hr class="line">
91<ul>
92<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
93</ul>
94    </div>
95
96    <div id="column1" data-id="net" class="interior">
97      <header class="header">
98        <div class="header-container">
99          <h1>Node.js v18.20.1 documentation</h1>
100          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
101            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
102              <path fill="none" d="M0 0h24v24H0z" />
103              <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"/>
104              <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"/>
105            </svg>
106            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
107              <path d="M0 0h24v24H0z" fill="none" />
108              <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"/>
109            </svg>
110          </button>
111        </div>
112        <div id="gtoc">
113          <ul>
114            <li class="pinned-header">Node.js v18.20.1</li>
115
116    <li class="picker-header">
117      <a href="#">
118        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
119        Table of contents
120      </a>
121
122      <div class="picker"><div class="toc"><ul>
123<li><span class="stability_2"><a href="#net">Net</a></span>
124<ul>
125<li><a href="#ipc-support">IPC support</a>
126<ul>
127<li><a href="#identifying-paths-for-ipc-connections">Identifying paths for IPC connections</a></li>
128</ul>
129</li>
130<li><a href="#class-netblocklist">Class: <code>net.BlockList</code></a>
131<ul>
132<li><a href="#blocklistaddaddressaddress-type"><code>blockList.addAddress(address[, type])</code></a></li>
133<li><a href="#blocklistaddrangestart-end-type"><code>blockList.addRange(start, end[, type])</code></a></li>
134<li><a href="#blocklistaddsubnetnet-prefix-type"><code>blockList.addSubnet(net, prefix[, type])</code></a></li>
135<li><a href="#blocklistcheckaddress-type"><code>blockList.check(address[, type])</code></a></li>
136<li><a href="#blocklistrules"><code>blockList.rules</code></a></li>
137</ul>
138</li>
139<li><a href="#class-netsocketaddress">Class: <code>net.SocketAddress</code></a>
140<ul>
141<li><a href="#new-netsocketaddressoptions"><code>new net.SocketAddress([options])</code></a></li>
142<li><a href="#socketaddressaddress"><code>socketaddress.address</code></a></li>
143<li><a href="#socketaddressfamily"><code>socketaddress.family</code></a></li>
144<li><a href="#socketaddressflowlabel"><code>socketaddress.flowlabel</code></a></li>
145<li><a href="#socketaddressport"><code>socketaddress.port</code></a></li>
146</ul>
147</li>
148<li><a href="#class-netserver">Class: <code>net.Server</code></a>
149<ul>
150<li><a href="#new-netserveroptions-connectionlistener"><code>new net.Server([options][, connectionListener])</code></a></li>
151<li><a href="#event-close">Event: <code>'close'</code></a></li>
152<li><a href="#event-connection">Event: <code>'connection'</code></a></li>
153<li><a href="#event-error">Event: <code>'error'</code></a></li>
154<li><a href="#event-listening">Event: <code>'listening'</code></a></li>
155<li><a href="#event-drop">Event: <code>'drop'</code></a></li>
156<li><a href="#serveraddress"><code>server.address()</code></a></li>
157<li><a href="#serverclosecallback"><code>server.close([callback])</code></a></li>
158<li><span class="stability_1"><a href="#serversymbolasyncdispose"><code>server[Symbol.asyncDispose]()</code></a></span></li>
159<li><a href="#servergetconnectionscallback"><code>server.getConnections(callback)</code></a></li>
160<li><a href="#serverlisten"><code>server.listen()</code></a>
161<ul>
162<li><a href="#serverlistenhandle-backlog-callback"><code>server.listen(handle[, backlog][, callback])</code></a></li>
163<li><a href="#serverlistenoptions-callback"><code>server.listen(options[, callback])</code></a></li>
164<li><a href="#serverlistenpath-backlog-callback"><code>server.listen(path[, backlog][, callback])</code></a></li>
165<li><a href="#serverlistenport-host-backlog-callback"><code>server.listen([port[, host[, backlog]]][, callback])</code></a></li>
166</ul>
167</li>
168<li><a href="#serverlistening"><code>server.listening</code></a></li>
169<li><a href="#servermaxconnections"><code>server.maxConnections</code></a></li>
170<li><a href="#serverref"><code>server.ref()</code></a></li>
171<li><a href="#serverunref"><code>server.unref()</code></a></li>
172</ul>
173</li>
174<li><a href="#class-netsocket">Class: <code>net.Socket</code></a>
175<ul>
176<li><a href="#new-netsocketoptions"><code>new net.Socket([options])</code></a></li>
177<li><a href="#event-close_1">Event: <code>'close'</code></a></li>
178<li><a href="#event-connect">Event: <code>'connect'</code></a></li>
179<li><a href="#event-data">Event: <code>'data'</code></a></li>
180<li><a href="#event-drain">Event: <code>'drain'</code></a></li>
181<li><a href="#event-end">Event: <code>'end'</code></a></li>
182<li><a href="#event-error_1">Event: <code>'error'</code></a></li>
183<li><a href="#event-lookup">Event: <code>'lookup'</code></a></li>
184<li><a href="#event-ready">Event: <code>'ready'</code></a></li>
185<li><a href="#event-timeout">Event: <code>'timeout'</code></a></li>
186<li><a href="#socketaddress"><code>socket.address()</code></a></li>
187<li><a href="#socketautoselectfamilyattemptedaddresses"><code>socket.autoSelectFamilyAttemptedAddresses</code></a></li>
188<li><span class="stability_0"><a href="#socketbuffersize"><code>socket.bufferSize</code></a></span></li>
189<li><a href="#socketbytesread"><code>socket.bytesRead</code></a></li>
190<li><a href="#socketbyteswritten"><code>socket.bytesWritten</code></a></li>
191<li><a href="#socketconnect"><code>socket.connect()</code></a>
192<ul>
193<li><a href="#socketconnectoptions-connectlistener"><code>socket.connect(options[, connectListener])</code></a></li>
194<li><a href="#socketconnectpath-connectlistener"><code>socket.connect(path[, connectListener])</code></a></li>
195<li><a href="#socketconnectport-host-connectlistener"><code>socket.connect(port[, host][, connectListener])</code></a></li>
196</ul>
197</li>
198<li><a href="#socketconnecting"><code>socket.connecting</code></a></li>
199<li><a href="#socketdestroyerror"><code>socket.destroy([error])</code></a></li>
200<li><a href="#socketdestroyed"><code>socket.destroyed</code></a></li>
201<li><a href="#socketdestroysoon"><code>socket.destroySoon()</code></a></li>
202<li><a href="#socketenddata-encoding-callback"><code>socket.end([data[, encoding]][, callback])</code></a></li>
203<li><a href="#socketlocaladdress"><code>socket.localAddress</code></a></li>
204<li><a href="#socketlocalport"><code>socket.localPort</code></a></li>
205<li><a href="#socketlocalfamily"><code>socket.localFamily</code></a></li>
206<li><a href="#socketpause"><code>socket.pause()</code></a></li>
207<li><a href="#socketpending"><code>socket.pending</code></a></li>
208<li><a href="#socketref"><code>socket.ref()</code></a></li>
209<li><a href="#socketremoteaddress"><code>socket.remoteAddress</code></a></li>
210<li><a href="#socketremotefamily"><code>socket.remoteFamily</code></a></li>
211<li><a href="#socketremoteport"><code>socket.remotePort</code></a></li>
212<li><a href="#socketresetanddestroy"><code>socket.resetAndDestroy()</code></a></li>
213<li><a href="#socketresume"><code>socket.resume()</code></a></li>
214<li><a href="#socketsetencodingencoding"><code>socket.setEncoding([encoding])</code></a></li>
215<li><a href="#socketsetkeepaliveenable-initialdelay"><code>socket.setKeepAlive([enable][, initialDelay])</code></a></li>
216<li><a href="#socketsetnodelaynodelay"><code>socket.setNoDelay([noDelay])</code></a></li>
217<li><a href="#socketsettimeouttimeout-callback"><code>socket.setTimeout(timeout[, callback])</code></a></li>
218<li><a href="#sockettimeout"><code>socket.timeout</code></a></li>
219<li><a href="#socketunref"><code>socket.unref()</code></a></li>
220<li><a href="#socketwritedata-encoding-callback"><code>socket.write(data[, encoding][, callback])</code></a></li>
221<li><a href="#socketreadystate"><code>socket.readyState</code></a></li>
222</ul>
223</li>
224<li><a href="#netconnect"><code>net.connect()</code></a>
225<ul>
226<li><a href="#netconnectoptions-connectlistener"><code>net.connect(options[, connectListener])</code></a></li>
227<li><a href="#netconnectpath-connectlistener"><code>net.connect(path[, connectListener])</code></a></li>
228<li><a href="#netconnectport-host-connectlistener"><code>net.connect(port[, host][, connectListener])</code></a></li>
229</ul>
230</li>
231<li><a href="#netcreateconnection"><code>net.createConnection()</code></a>
232<ul>
233<li><a href="#netcreateconnectionoptions-connectlistener"><code>net.createConnection(options[, connectListener])</code></a></li>
234<li><a href="#netcreateconnectionpath-connectlistener"><code>net.createConnection(path[, connectListener])</code></a></li>
235<li><a href="#netcreateconnectionport-host-connectlistener"><code>net.createConnection(port[, host][, connectListener])</code></a></li>
236</ul>
237</li>
238<li><a href="#netcreateserveroptions-connectionlistener"><code>net.createServer([options][, connectionListener])</code></a></li>
239<li><a href="#netgetdefaultautoselectfamily"><code>net.getDefaultAutoSelectFamily()</code></a></li>
240<li><a href="#netsetdefaultautoselectfamilyvalue"><code>net.setDefaultAutoSelectFamily(value)</code></a></li>
241<li><a href="#netgetdefaultautoselectfamilyattempttimeout"><code>net.getDefaultAutoSelectFamilyAttemptTimeout()</code></a></li>
242<li><a href="#netsetdefaultautoselectfamilyattempttimeoutvalue"><code>net.setDefaultAutoSelectFamilyAttemptTimeout(value)</code></a></li>
243<li><a href="#netisipinput"><code>net.isIP(input)</code></a></li>
244<li><a href="#netisipv4input"><code>net.isIPv4(input)</code></a></li>
245<li><a href="#netisipv6input"><code>net.isIPv6(input)</code></a></li>
246</ul>
247</li>
248</ul></div></div>
249    </li>
250
251
252    <li class="picker-header">
253      <a href="#">
254        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
255        Index
256      </a>
257
258      <div class="picker"><ul>
259<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
260<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
261
262      <li>
263        <a href="index.html">Index</a>
264      </li>
265    </ul>
266
267<hr class="line">
268<ul>
269<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
270<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
271<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
272<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
273<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
274<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
275<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
276<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
277<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
278<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
279<li><a href="console.html" class="nav-console">Console</a></li>
280<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
281<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
282<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
283<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
284<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
285<li><a href="dns.html" class="nav-dns">DNS</a></li>
286<li><a href="domain.html" class="nav-domain">Domain</a></li>
287<li><a href="errors.html" class="nav-errors">Errors</a></li>
288<li><a href="events.html" class="nav-events">Events</a></li>
289<li><a href="fs.html" class="nav-fs">File system</a></li>
290<li><a href="globals.html" class="nav-globals">Globals</a></li>
291<li><a href="http.html" class="nav-http">HTTP</a></li>
292<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
293<li><a href="https.html" class="nav-https">HTTPS</a></li>
294<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
295<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
296<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
297<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
298<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
299<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
300<li><a href="net.html" class="nav-net active">Net</a></li>
301<li><a href="os.html" class="nav-os">OS</a></li>
302<li><a href="path.html" class="nav-path">Path</a></li>
303<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
304<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
305<li><a href="process.html" class="nav-process">Process</a></li>
306<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
307<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
308<li><a href="readline.html" class="nav-readline">Readline</a></li>
309<li><a href="repl.html" class="nav-repl">REPL</a></li>
310<li><a href="report.html" class="nav-report">Report</a></li>
311<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
312<li><a href="stream.html" class="nav-stream">Stream</a></li>
313<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
314<li><a href="test.html" class="nav-test">Test runner</a></li>
315<li><a href="timers.html" class="nav-timers">Timers</a></li>
316<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
317<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
318<li><a href="tty.html" class="nav-tty">TTY</a></li>
319<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
320<li><a href="url.html" class="nav-url">URL</a></li>
321<li><a href="util.html" class="nav-util">Utilities</a></li>
322<li><a href="v8.html" class="nav-v8">V8</a></li>
323<li><a href="vm.html" class="nav-vm">VM</a></li>
324<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
325<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
326<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
327<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
328<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
329</ul>
330<hr class="line">
331<ul>
332<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
333</ul></div>
334    </li>
335
336
337    <li class="picker-header">
338      <a href="#">
339        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
340        Other versions
341      </a>
342      <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/net.html">21.x</a></li>
343<li><a href="https://nodejs.org/docs/latest-v20.x/api/net.html">20.x <b>LTS</b></a></li>
344<li><a href="https://nodejs.org/docs/latest-v19.x/api/net.html">19.x</a></li>
345<li><a href="https://nodejs.org/docs/latest-v18.x/api/net.html">18.x <b>LTS</b></a></li>
346<li><a href="https://nodejs.org/docs/latest-v17.x/api/net.html">17.x</a></li>
347<li><a href="https://nodejs.org/docs/latest-v16.x/api/net.html">16.x</a></li>
348<li><a href="https://nodejs.org/docs/latest-v15.x/api/net.html">15.x</a></li>
349<li><a href="https://nodejs.org/docs/latest-v14.x/api/net.html">14.x</a></li>
350<li><a href="https://nodejs.org/docs/latest-v13.x/api/net.html">13.x</a></li>
351<li><a href="https://nodejs.org/docs/latest-v12.x/api/net.html">12.x</a></li>
352<li><a href="https://nodejs.org/docs/latest-v11.x/api/net.html">11.x</a></li>
353<li><a href="https://nodejs.org/docs/latest-v10.x/api/net.html">10.x</a></li>
354<li><a href="https://nodejs.org/docs/latest-v9.x/api/net.html">9.x</a></li>
355<li><a href="https://nodejs.org/docs/latest-v8.x/api/net.html">8.x</a></li>
356<li><a href="https://nodejs.org/docs/latest-v7.x/api/net.html">7.x</a></li>
357<li><a href="https://nodejs.org/docs/latest-v6.x/api/net.html">6.x</a></li>
358<li><a href="https://nodejs.org/docs/latest-v5.x/api/net.html">5.x</a></li>
359<li><a href="https://nodejs.org/docs/latest-v4.x/api/net.html">4.x</a></li>
360<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/net.html">0.12.x</a></li>
361<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/net.html">0.10.x</a></li></ol></div>
362    </li>
363
364            <li class="picker-header">
365              <a href="#">
366                <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
367                Options
368              </a>
369
370              <div class="picker">
371                <ul>
372                  <li>
373                    <a href="all.html">View on single page</a>
374                  </li>
375                  <li>
376                    <a href="net.json">View as JSON</a>
377                  </li>
378                  <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/net.md">Edit on GitHub</a></li>
379                </ul>
380              </div>
381            </li>
382          </ul>
383        </div>
384        <hr>
385      </header>
386
387      <details id="toc" open><summary>Table of contents</summary><ul>
388<li><span class="stability_2"><a href="#net">Net</a></span>
389<ul>
390<li><a href="#ipc-support">IPC support</a>
391<ul>
392<li><a href="#identifying-paths-for-ipc-connections">Identifying paths for IPC connections</a></li>
393</ul>
394</li>
395<li><a href="#class-netblocklist">Class: <code>net.BlockList</code></a>
396<ul>
397<li><a href="#blocklistaddaddressaddress-type"><code>blockList.addAddress(address[, type])</code></a></li>
398<li><a href="#blocklistaddrangestart-end-type"><code>blockList.addRange(start, end[, type])</code></a></li>
399<li><a href="#blocklistaddsubnetnet-prefix-type"><code>blockList.addSubnet(net, prefix[, type])</code></a></li>
400<li><a href="#blocklistcheckaddress-type"><code>blockList.check(address[, type])</code></a></li>
401<li><a href="#blocklistrules"><code>blockList.rules</code></a></li>
402</ul>
403</li>
404<li><a href="#class-netsocketaddress">Class: <code>net.SocketAddress</code></a>
405<ul>
406<li><a href="#new-netsocketaddressoptions"><code>new net.SocketAddress([options])</code></a></li>
407<li><a href="#socketaddressaddress"><code>socketaddress.address</code></a></li>
408<li><a href="#socketaddressfamily"><code>socketaddress.family</code></a></li>
409<li><a href="#socketaddressflowlabel"><code>socketaddress.flowlabel</code></a></li>
410<li><a href="#socketaddressport"><code>socketaddress.port</code></a></li>
411</ul>
412</li>
413<li><a href="#class-netserver">Class: <code>net.Server</code></a>
414<ul>
415<li><a href="#new-netserveroptions-connectionlistener"><code>new net.Server([options][, connectionListener])</code></a></li>
416<li><a href="#event-close">Event: <code>'close'</code></a></li>
417<li><a href="#event-connection">Event: <code>'connection'</code></a></li>
418<li><a href="#event-error">Event: <code>'error'</code></a></li>
419<li><a href="#event-listening">Event: <code>'listening'</code></a></li>
420<li><a href="#event-drop">Event: <code>'drop'</code></a></li>
421<li><a href="#serveraddress"><code>server.address()</code></a></li>
422<li><a href="#serverclosecallback"><code>server.close([callback])</code></a></li>
423<li><span class="stability_1"><a href="#serversymbolasyncdispose"><code>server[Symbol.asyncDispose]()</code></a></span></li>
424<li><a href="#servergetconnectionscallback"><code>server.getConnections(callback)</code></a></li>
425<li><a href="#serverlisten"><code>server.listen()</code></a>
426<ul>
427<li><a href="#serverlistenhandle-backlog-callback"><code>server.listen(handle[, backlog][, callback])</code></a></li>
428<li><a href="#serverlistenoptions-callback"><code>server.listen(options[, callback])</code></a></li>
429<li><a href="#serverlistenpath-backlog-callback"><code>server.listen(path[, backlog][, callback])</code></a></li>
430<li><a href="#serverlistenport-host-backlog-callback"><code>server.listen([port[, host[, backlog]]][, callback])</code></a></li>
431</ul>
432</li>
433<li><a href="#serverlistening"><code>server.listening</code></a></li>
434<li><a href="#servermaxconnections"><code>server.maxConnections</code></a></li>
435<li><a href="#serverref"><code>server.ref()</code></a></li>
436<li><a href="#serverunref"><code>server.unref()</code></a></li>
437</ul>
438</li>
439<li><a href="#class-netsocket">Class: <code>net.Socket</code></a>
440<ul>
441<li><a href="#new-netsocketoptions"><code>new net.Socket([options])</code></a></li>
442<li><a href="#event-close_1">Event: <code>'close'</code></a></li>
443<li><a href="#event-connect">Event: <code>'connect'</code></a></li>
444<li><a href="#event-data">Event: <code>'data'</code></a></li>
445<li><a href="#event-drain">Event: <code>'drain'</code></a></li>
446<li><a href="#event-end">Event: <code>'end'</code></a></li>
447<li><a href="#event-error_1">Event: <code>'error'</code></a></li>
448<li><a href="#event-lookup">Event: <code>'lookup'</code></a></li>
449<li><a href="#event-ready">Event: <code>'ready'</code></a></li>
450<li><a href="#event-timeout">Event: <code>'timeout'</code></a></li>
451<li><a href="#socketaddress"><code>socket.address()</code></a></li>
452<li><a href="#socketautoselectfamilyattemptedaddresses"><code>socket.autoSelectFamilyAttemptedAddresses</code></a></li>
453<li><span class="stability_0"><a href="#socketbuffersize"><code>socket.bufferSize</code></a></span></li>
454<li><a href="#socketbytesread"><code>socket.bytesRead</code></a></li>
455<li><a href="#socketbyteswritten"><code>socket.bytesWritten</code></a></li>
456<li><a href="#socketconnect"><code>socket.connect()</code></a>
457<ul>
458<li><a href="#socketconnectoptions-connectlistener"><code>socket.connect(options[, connectListener])</code></a></li>
459<li><a href="#socketconnectpath-connectlistener"><code>socket.connect(path[, connectListener])</code></a></li>
460<li><a href="#socketconnectport-host-connectlistener"><code>socket.connect(port[, host][, connectListener])</code></a></li>
461</ul>
462</li>
463<li><a href="#socketconnecting"><code>socket.connecting</code></a></li>
464<li><a href="#socketdestroyerror"><code>socket.destroy([error])</code></a></li>
465<li><a href="#socketdestroyed"><code>socket.destroyed</code></a></li>
466<li><a href="#socketdestroysoon"><code>socket.destroySoon()</code></a></li>
467<li><a href="#socketenddata-encoding-callback"><code>socket.end([data[, encoding]][, callback])</code></a></li>
468<li><a href="#socketlocaladdress"><code>socket.localAddress</code></a></li>
469<li><a href="#socketlocalport"><code>socket.localPort</code></a></li>
470<li><a href="#socketlocalfamily"><code>socket.localFamily</code></a></li>
471<li><a href="#socketpause"><code>socket.pause()</code></a></li>
472<li><a href="#socketpending"><code>socket.pending</code></a></li>
473<li><a href="#socketref"><code>socket.ref()</code></a></li>
474<li><a href="#socketremoteaddress"><code>socket.remoteAddress</code></a></li>
475<li><a href="#socketremotefamily"><code>socket.remoteFamily</code></a></li>
476<li><a href="#socketremoteport"><code>socket.remotePort</code></a></li>
477<li><a href="#socketresetanddestroy"><code>socket.resetAndDestroy()</code></a></li>
478<li><a href="#socketresume"><code>socket.resume()</code></a></li>
479<li><a href="#socketsetencodingencoding"><code>socket.setEncoding([encoding])</code></a></li>
480<li><a href="#socketsetkeepaliveenable-initialdelay"><code>socket.setKeepAlive([enable][, initialDelay])</code></a></li>
481<li><a href="#socketsetnodelaynodelay"><code>socket.setNoDelay([noDelay])</code></a></li>
482<li><a href="#socketsettimeouttimeout-callback"><code>socket.setTimeout(timeout[, callback])</code></a></li>
483<li><a href="#sockettimeout"><code>socket.timeout</code></a></li>
484<li><a href="#socketunref"><code>socket.unref()</code></a></li>
485<li><a href="#socketwritedata-encoding-callback"><code>socket.write(data[, encoding][, callback])</code></a></li>
486<li><a href="#socketreadystate"><code>socket.readyState</code></a></li>
487</ul>
488</li>
489<li><a href="#netconnect"><code>net.connect()</code></a>
490<ul>
491<li><a href="#netconnectoptions-connectlistener"><code>net.connect(options[, connectListener])</code></a></li>
492<li><a href="#netconnectpath-connectlistener"><code>net.connect(path[, connectListener])</code></a></li>
493<li><a href="#netconnectport-host-connectlistener"><code>net.connect(port[, host][, connectListener])</code></a></li>
494</ul>
495</li>
496<li><a href="#netcreateconnection"><code>net.createConnection()</code></a>
497<ul>
498<li><a href="#netcreateconnectionoptions-connectlistener"><code>net.createConnection(options[, connectListener])</code></a></li>
499<li><a href="#netcreateconnectionpath-connectlistener"><code>net.createConnection(path[, connectListener])</code></a></li>
500<li><a href="#netcreateconnectionport-host-connectlistener"><code>net.createConnection(port[, host][, connectListener])</code></a></li>
501</ul>
502</li>
503<li><a href="#netcreateserveroptions-connectionlistener"><code>net.createServer([options][, connectionListener])</code></a></li>
504<li><a href="#netgetdefaultautoselectfamily"><code>net.getDefaultAutoSelectFamily()</code></a></li>
505<li><a href="#netsetdefaultautoselectfamilyvalue"><code>net.setDefaultAutoSelectFamily(value)</code></a></li>
506<li><a href="#netgetdefaultautoselectfamilyattempttimeout"><code>net.getDefaultAutoSelectFamilyAttemptTimeout()</code></a></li>
507<li><a href="#netsetdefaultautoselectfamilyattempttimeoutvalue"><code>net.setDefaultAutoSelectFamilyAttemptTimeout(value)</code></a></li>
508<li><a href="#netisipinput"><code>net.isIP(input)</code></a></li>
509<li><a href="#netisipv4input"><code>net.isIPv4(input)</code></a></li>
510<li><a href="#netisipv6input"><code>net.isIPv6(input)</code></a></li>
511</ul>
512</li>
513</ul></details>
514
515      <div id="apicontent">
516        <h2>Net<span><a class="mark" href="#net" id="net">#</a></span><a aria-hidden="true" class="legacy" id="net_net"></a></h2>
517
518<!--lint disable maximum-line-length-->
519<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p>
520<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/net.js">lib/net.js</a></p>
521<p>The <code>node:net</code> module provides an asynchronous network API for creating stream-based
522TCP or <a href="#ipc-support">IPC</a> servers (<a href="#netcreateserveroptions-connectionlistener"><code>net.createServer()</code></a>) and clients
523(<a href="#netcreateconnection"><code>net.createConnection()</code></a>).</p>
524<p>It can be accessed using:</p>
525<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>
526<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>
527<p>The <code>node:net</code> module supports IPC with named pipes on Windows, and Unix domain
528sockets on other operating systems.</p>
529<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>
530<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
531<a href="#socketconnect"><code>socket.connect()</code></a> take a <code>path</code> parameter to identify IPC endpoints.</p>
532<p>On Unix, the local domain is also known as the Unix domain. The path is a
533file system pathname. It gets truncated to an OS-dependent length of
534<code>sizeof(sockaddr_un.sun_path) - 1</code>. Typical values are 107 bytes on Linux and
535103 bytes on macOS. If a Node.js API abstraction creates the Unix domain socket,
536it will unlink the Unix domain socket as well. For example,
537<a href="#netcreateserveroptions-connectionlistener"><code>net.createServer()</code></a> may create a Unix domain socket and
538<a href="#serverclosecallback"><code>server.close()</code></a> will unlink it. But if a user creates the Unix domain
539socket outside of these abstractions, the user will need to remove it. The same
540applies when a Node.js API creates a Unix domain socket but the program then
541crashes. In short, a Unix domain socket will be visible in the file system and
542will persist until unlinked.</p>
543<p>On Windows, the local domain is implemented using a named pipe. The path <em>must</em>
544refer to an entry in <code>\\?\pipe\</code> or <code>\\.\pipe\</code>. Any characters are permitted,
545but the latter may do some processing of pipe names, such as resolving <code>..</code>
546sequences. Despite how it might look, the pipe namespace is flat. Pipes will
547<em>not persist</em>. They are removed when the last reference to them is closed.
548Unlike Unix domain sockets, Windows will close and remove the pipe when the
549owning process exits.</p>
550<p>JavaScript string escaping requires paths to be specified with extra backslash
551escaping such as:</p>
552<pre><code class="language-js">net.<span class="hljs-title function_">createServer</span>().<span class="hljs-title function_">listen</span>(
553  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>
554</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>
555<div class="api_metadata">
556<span>Added in: v15.0.0, v14.18.0</span>
557</div>
558<p>The <code>BlockList</code> object can be used with some network APIs to specify rules for
559disabling inbound or outbound access to specific IP addresses, IP ranges, or
560IP subnets.</p>
561<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>
562<div class="api_metadata">
563<span>Added in: v15.0.0, v14.18.0</span>
564</div>
565<ul>
566<li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="net.html#class-netsocketaddress" class="type">&#x3C;net.SocketAddress></a> An IPv4 or IPv6 address.</li>
567<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Either <code>'ipv4'</code> or <code>'ipv6'</code>. <strong>Default:</strong> <code>'ipv4'</code>.</li>
568</ul>
569<p>Adds a rule to block the given IP address.</p>
570<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>
571<div class="api_metadata">
572<span>Added in: v15.0.0, v14.18.0</span>
573</div>
574<ul>
575<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="net.html#class-netsocketaddress" class="type">&#x3C;net.SocketAddress></a> The starting IPv4 or IPv6 address in the
576range.</li>
577<li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="net.html#class-netsocketaddress" class="type">&#x3C;net.SocketAddress></a> The ending IPv4 or IPv6 address in the range.</li>
578<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Either <code>'ipv4'</code> or <code>'ipv6'</code>. <strong>Default:</strong> <code>'ipv4'</code>.</li>
579</ul>
580<p>Adds a rule to block a range of IP addresses from <code>start</code> (inclusive) to
581<code>end</code> (inclusive).</p>
582<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>
583<div class="api_metadata">
584<span>Added in: v15.0.0, v14.18.0</span>
585</div>
586<ul>
587<li><code>net</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="net.html#class-netsocketaddress" class="type">&#x3C;net.SocketAddress></a> The network IPv4 or IPv6 address.</li>
588<li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of CIDR prefix bits. For IPv4, this
589must be a value between <code>0</code> and <code>32</code>. For IPv6, this must be between
590<code>0</code> and <code>128</code>.</li>
591<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Either <code>'ipv4'</code> or <code>'ipv6'</code>. <strong>Default:</strong> <code>'ipv4'</code>.</li>
592</ul>
593<p>Adds a rule to block a range of IP addresses specified as a subnet mask.</p>
594<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>
595<div class="api_metadata">
596<span>Added in: v15.0.0, v14.18.0</span>
597</div>
598<ul>
599<li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="net.html#class-netsocketaddress" class="type">&#x3C;net.SocketAddress></a> The IP address to check</li>
600<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Either <code>'ipv4'</code> or <code>'ipv6'</code>. <strong>Default:</strong> <code>'ipv4'</code>.</li>
601<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
602</ul>
603<p>Returns <code>true</code> if the given IP address matches any of the rules added to the
604<code>BlockList</code>.</p>
605<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>();
606blockList.<span class="hljs-title function_">addAddress</span>(<span class="hljs-string">'123.123.123.123'</span>);
607blockList.<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>);
608blockList.<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>);
609
610<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>
611<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>
612<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>
613
614<span class="hljs-comment">// IPv6 notation for IPv4 addresses works:</span>
615<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>
616<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>
617<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>
618<div class="api_metadata">
619<span>Added in: v15.0.0, v14.18.0</span>
620</div>
621<ul>
622<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a></li>
623</ul>
624<p>The list of rules added to the blocklist.</p>
625</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>
626<div class="api_metadata">
627<span>Added in: v15.14.0, v14.18.0</span>
628</div>
629<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>
630<div class="api_metadata">
631<span>Added in: v15.14.0, v14.18.0</span>
632</div>
633<ul>
634<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
635<ul>
636<li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The network address as either an IPv4 or IPv6 string.
637<strong>Default</strong>: <code>'127.0.0.1'</code> if <code>family</code> is <code>'ipv4'</code>; <code>'::'</code> if <code>family</code> is
638<code>'ipv6'</code>.</li>
639<li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> One of either <code>'ipv4'</code> or <code>'ipv6'</code>.
640<strong>Default</strong>: <code>'ipv4'</code>.</li>
641<li><code>flowlabel</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> An IPv6 flow-label used only if <code>family</code> is <code>'ipv6'</code>.</li>
642<li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> An IP port.</li>
643</ul>
644</li>
645</ul>
646<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>
647<div class="api_metadata">
648<span>Added in: v15.14.0, v14.18.0</span>
649</div>
650<ul>
651<li>Type <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
652</ul>
653<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>
654<div class="api_metadata">
655<span>Added in: v15.14.0, v14.18.0</span>
656</div>
657<ul>
658<li>Type <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Either <code>'ipv4'</code> or <code>'ipv6'</code>.</li>
659</ul>
660<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>
661<div class="api_metadata">
662<span>Added in: v15.14.0, v14.18.0</span>
663</div>
664<ul>
665<li>Type <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
666</ul>
667<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>
668<div class="api_metadata">
669<span>Added in: v15.14.0, v14.18.0</span>
670</div>
671<ul>
672<li>Type <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
673</ul>
674</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>
675<div class="api_metadata">
676<span>Added in: v0.1.90</span>
677</div>
678<ul>
679<li>Extends: <a href="events.html#class-eventemitter" class="type">&#x3C;EventEmitter></a></li>
680</ul>
681<p>This class is used to create a TCP or <a href="#ipc-support">IPC</a> server.</p>
682<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>
683<ul>
684<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> See
685<a href="#netcreateserveroptions-connectionlistener"><code>net.createServer([options][, connectionListener])</code></a>.</li>
686<li><code>connectionListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Automatically set as a listener for the
687<a href="#event-connection"><code>'connection'</code></a> event.</li>
688<li>Returns: <a href="net.html#class-netserver" class="type">&#x3C;net.Server></a></li>
689</ul>
690<p><code>net.Server</code> is an <a href="events.html#class-eventemitter"><code>EventEmitter</code></a> with the following events:</p>
691<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>
692<div class="api_metadata">
693<span>Added in: v0.5.0</span>
694</div>
695<p>Emitted when the server closes. If connections exist, this
696event is not emitted until all connections are ended.</p>
697<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>
698<div class="api_metadata">
699<span>Added in: v0.1.90</span>
700</div>
701<ul>
702<li><a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a> The connection object</li>
703</ul>
704<p>Emitted when a new connection is made. <code>socket</code> is an instance of
705<code>net.Socket</code>.</p>
706<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>
707<div class="api_metadata">
708<span>Added in: v0.1.90</span>
709</div>
710<ul>
711<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
712</ul>
713<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>
714event will <strong>not</strong> be emitted directly following this event unless
715<a href="#serverclosecallback"><code>server.close()</code></a> is manually called. See the example in discussion of
716<a href="#serverlisten"><code>server.listen()</code></a>.</p>
717<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>
718<div class="api_metadata">
719<span>Added in: v0.1.90</span>
720</div>
721<p>Emitted when the server has been bound after calling <a href="#serverlisten"><code>server.listen()</code></a>.</p>
722<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>
723<div class="api_metadata">
724<span>Added in: v18.6.0</span>
725</div>
726<p>When the number of connections reaches the threshold of <code>server.maxConnections</code>,
727the server will drop new connections and emit <code>'drop'</code> event instead. If it is a
728TCP server, the argument is as follows, otherwise the argument is <code>undefined</code>.</p>
729<ul>
730<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> The argument passed to event listener.
731<ul>
732<li><code>localAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a>  Local address.</li>
733<li><code>localPort</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Local port.</li>
734<li><code>localFamily</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Local family.</li>
735<li><code>remoteAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Remote address.</li>
736<li><code>remotePort</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Remote port.</li>
737<li><code>remoteFamily</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Remote IP family. <code>'IPv4'</code> or <code>'IPv6'</code>.</li>
738</ul>
739</li>
740</ul>
741<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>
742<div class="api_metadata">
743<details class="changelog"><summary>History</summary>
744<table>
745<tbody><tr><th>Version</th><th>Changes</th></tr>
746<tr><td>v18.4.0</td>
747<td><p>The <code>family</code> property now returns a string instead of a number.</p></td></tr>
748<tr><td>v18.0.0</td>
749<td><p>The <code>family</code> property now returns a number instead of a string.</p></td></tr>
750<tr><td>v0.1.90</td>
751<td><p><span>Added in: v0.1.90</span></p></td></tr>
752</tbody></table>
753</details>
754</div>
755<ul>
756<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a></li>
757</ul>
758<p>Returns the bound <code>address</code>, the address <code>family</code> name, and <code>port</code> of the server
759as reported by the operating system if listening on an IP socket
760(useful to find which port was assigned when getting an OS-assigned address):
761<code>{ port: 12346, family: 'IPv4', address: '127.0.0.1' }</code>.</p>
762<p>For a server listening on a pipe or Unix domain socket, the name is returned
763as a string.</p>
764<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> {
765  socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'goodbye\n'</span>);
766}).<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
767  <span class="hljs-comment">// Handle errors here.</span>
768  <span class="hljs-keyword">throw</span> err;
769});
770
771<span class="hljs-comment">// Grab an arbitrary unused port.</span>
772server.<span class="hljs-title function_">listen</span>(<span class="hljs-function">() =></span> {
773  <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>());
774});</code> <button class="copy-button">copy</button></pre>
775<p><code>server.address()</code> returns <code>null</code> before the <code>'listening'</code> event has been
776emitted or after calling <code>server.close()</code>.</p>
777<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>
778<div class="api_metadata">
779<span>Added in: v0.1.90</span>
780</div>
781<ul>
782<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Called when the server is closed.</li>
783<li>Returns: <a href="net.html#class-netserver" class="type">&#x3C;net.Server></a></li>
784</ul>
785<p>Stops the server from accepting new connections and keeps existing
786connections. This function is asynchronous, the server is finally closed
787when all connections are ended and the server emits a <a href="#event-close"><code>'close'</code></a> event.
788The optional <code>callback</code> will be called once the <code>'close'</code> event occurs. Unlike
789that event, it will be called with an <code>Error</code> as its only argument if the server
790was not open when it was closed.</p>
791<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>
792<div class="api_metadata">
793<span>Added in: v18.18.0</span>
794</div>
795<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
796<p>Calls <a href="#serverclosecallback"><code>server.close()</code></a> and returns a promise that fulfills when the
797server has closed.</p>
798<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>
799<div class="api_metadata">
800<span>Added in: v0.9.7</span>
801</div>
802<ul>
803<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
804<li>Returns: <a href="net.html#class-netserver" class="type">&#x3C;net.Server></a></li>
805</ul>
806<p>Asynchronously get the number of concurrent connections on the server. Works
807when sockets were sent to forks.</p>
808<p>Callback should take two arguments <code>err</code> and <code>count</code>.</p>
809<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>
810<p>Start a server listening for connections. A <code>net.Server</code> can be a TCP or
811an <a href="#ipc-support">IPC</a> server depending on what it listens to.</p>
812<p>Possible signatures:</p>
813<ul>
814<li><a href="#serverlistenhandle-backlog-callback"><code>server.listen(handle[, backlog][, callback])</code></a></li>
815<li><a href="#serverlistenoptions-callback"><code>server.listen(options[, callback])</code></a></li>
816<li><a href="#serverlistenpath-backlog-callback"><code>server.listen(path[, backlog][, callback])</code></a>
817for <a href="#ipc-support">IPC</a> servers</li>
818<li><a href="#serverlistenport-host-backlog-callback"><code>server.listen([port[, host[, backlog]]][, callback])</code></a>
819for TCP servers</li>
820</ul>
821<p>This function is asynchronous. When the server starts listening, the
822<a href="#event-listening"><code>'listening'</code></a> event will be emitted. The last parameter <code>callback</code>
823will be added as a listener for the <a href="#event-listening"><code>'listening'</code></a> event.</p>
824<p>All <code>listen()</code> methods can take a <code>backlog</code> parameter to specify the maximum
825length of the queue of pending connections. The actual length will be determined
826by the OS through sysctl settings such as <code>tcp_max_syn_backlog</code> and <code>somaxconn</code>
827on Linux. The default value of this parameter is 511 (not 512).</p>
828<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
829details).</p>
830<p>The <code>server.listen()</code> method can be called again if and only if there was an
831error during the first <code>server.listen()</code> call or <code>server.close()</code> has been
832called. Otherwise, an <code>ERR_SERVER_ALREADY_LISTEN</code> error will be thrown.</p>
833<p>One of the most common errors raised when listening is <code>EADDRINUSE</code>.
834This happens when another server is already listening on the requested
835<code>port</code>/<code>path</code>/<code>handle</code>. One way to handle this would be to retry
836after a certain amount of time:</p>
837<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> {
838  <span class="hljs-keyword">if</span> (e.<span class="hljs-property">code</span> === <span class="hljs-string">'EADDRINUSE'</span>) {
839    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'Address in use, retrying...'</span>);
840    <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
841      server.<span class="hljs-title function_">close</span>();
842      server.<span class="hljs-title function_">listen</span>(<span class="hljs-variable constant_">PORT</span>, <span class="hljs-variable constant_">HOST</span>);
843    }, <span class="hljs-number">1000</span>);
844  }
845});</code> <button class="copy-button">copy</button></pre>
846<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>
847<div class="api_metadata">
848<span>Added in: v0.5.10</span>
849</div>
850<ul>
851<li><code>handle</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
852<li><code>backlog</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Common parameter of <a href="#serverlisten"><code>server.listen()</code></a> functions</li>
853<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
854<li>Returns: <a href="net.html#class-netserver" class="type">&#x3C;net.Server></a></li>
855</ul>
856<p>Start a server listening for connections on a given <code>handle</code> that has
857already been bound to a port, a Unix domain socket, or a Windows named pipe.</p>
858<p>The <code>handle</code> object can be either a server, a socket (anything with an
859underlying <code>_handle</code> member), or an object with an <code>fd</code> member that is a
860valid file descriptor.</p>
861<p>Listening on a file descriptor is not supported on Windows.</p>
862<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>
863<div class="api_metadata">
864<details class="changelog"><summary>History</summary>
865<table>
866<tbody><tr><th>Version</th><th>Changes</th></tr>
867<tr><td>v15.6.0</td>
868<td><p>AbortSignal support was added.</p></td></tr>
869<tr><td>v11.4.0</td>
870<td><p>The <code>ipv6Only</code> option is supported.</p></td></tr>
871<tr><td>v0.11.14</td>
872<td><p><span>Added in: v0.11.14</span></p></td></tr>
873</tbody></table>
874</details>
875</div>
876<ul>
877<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> Required. Supports the following properties:
878<ul>
879<li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
880<li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
881<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Will be ignored if <code>port</code> is specified. See
882<a href="#identifying-paths-for-ipc-connections">Identifying paths for IPC connections</a>.</li>
883<li><code>backlog</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Common parameter of <a href="#serverlisten"><code>server.listen()</code></a>
884functions.</li>
885<li><code>exclusive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <strong>Default:</strong> <code>false</code></li>
886<li><code>readableAll</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> For IPC servers makes the pipe readable
887for all users. <strong>Default:</strong> <code>false</code>.</li>
888<li><code>writableAll</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> For IPC servers makes the pipe writable
889for all users. <strong>Default:</strong> <code>false</code>.</li>
890<li><code>ipv6Only</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> For TCP servers, setting <code>ipv6Only</code> to <code>true</code> will
891disable dual-stack support, i.e., binding to host <code>::</code> won't make
892<code>0.0.0.0</code> be bound. <strong>Default:</strong> <code>false</code>.</li>
893<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type">&#x3C;AbortSignal></a> An AbortSignal that may be used to close a listening server.</li>
894</ul>
895</li>
896<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
897functions.</li>
898<li>Returns: <a href="net.html#class-netserver" class="type">&#x3C;net.Server></a></li>
899</ul>
900<p>If <code>port</code> is specified, it behaves the same as
901<a href="#serverlistenport-host-backlog-callback"><code>server.listen([port[, host[, backlog]]][, callback])</code></a>.
902Otherwise, if <code>path</code> is specified, it behaves the same as
903<a href="#serverlistenpath-backlog-callback"><code>server.listen(path[, backlog][, callback])</code></a>.
904If none of them is specified, an error will be thrown.</p>
905<p>If <code>exclusive</code> is <code>false</code> (default), then cluster workers will use the same
906underlying handle, allowing connection handling duties to be shared. When
907<code>exclusive</code> is <code>true</code>, the handle is not shared, and attempted port sharing
908results in an error. An example which listens on an exclusive port is
909shown below.</p>
910<pre><code class="language-js">server.<span class="hljs-title function_">listen</span>({
911  <span class="hljs-attr">host</span>: <span class="hljs-string">'localhost'</span>,
912  <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>,
913  <span class="hljs-attr">exclusive</span>: <span class="hljs-literal">true</span>,
914});</code> <button class="copy-button">copy</button></pre>
915<p>When <code>exclusive</code> is <code>true</code> and the underlying handle is shared, it is
916possible that several workers query a handle with different backlogs.
917In this case, the first <code>backlog</code> passed to the master process will be used.</p>
918<p>Starting an IPC server as root may cause the server path to be inaccessible for
919unprivileged users. Using <code>readableAll</code> and <code>writableAll</code> will make the server
920accessible for all users.</p>
921<p>If the <code>signal</code> option is enabled, calling <code>.abort()</code> on the corresponding
922<code>AbortController</code> is similar to calling <code>.close()</code> on the server:</p>
923<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>();
924server.<span class="hljs-title function_">listen</span>({
925  <span class="hljs-attr">host</span>: <span class="hljs-string">'localhost'</span>,
926  <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>,
927  <span class="hljs-attr">signal</span>: controller.<span class="hljs-property">signal</span>,
928});
929<span class="hljs-comment">// Later, when you want to close the server.</span>
930controller.<span class="hljs-title function_">abort</span>();</code> <button class="copy-button">copy</button></pre>
931<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>
932<div class="api_metadata">
933<span>Added in: v0.1.90</span>
934</div>
935<ul>
936<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Path the server should listen to. See
937<a href="#identifying-paths-for-ipc-connections">Identifying paths for IPC connections</a>.</li>
938<li><code>backlog</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Common parameter of <a href="#serverlisten"><code>server.listen()</code></a> functions.</li>
939<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>.</li>
940<li>Returns: <a href="net.html#class-netserver" class="type">&#x3C;net.Server></a></li>
941</ul>
942<p>Start an <a href="#ipc-support">IPC</a> server listening for connections on the given <code>path</code>.</p>
943<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>
944<div class="api_metadata">
945<span>Added in: v0.1.90</span>
946</div>
947<ul>
948<li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
949<li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
950<li><code>backlog</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Common parameter of <a href="#serverlisten"><code>server.listen()</code></a> functions.</li>
951<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>.</li>
952<li>Returns: <a href="net.html#class-netserver" class="type">&#x3C;net.Server></a></li>
953</ul>
954<p>Start a TCP server listening for connections on the given <code>port</code> and <code>host</code>.</p>
955<p>If <code>port</code> is omitted or is 0, the operating system will assign an arbitrary
956unused port, which can be retrieved by using <code>server.address().port</code>
957after the <a href="#event-listening"><code>'listening'</code></a> event has been emitted.</p>
958<p>If <code>host</code> is omitted, the server will accept connections on the
959<a href="https://en.wikipedia.org/wiki/IPv6_address#Unspecified_address">unspecified IPv6 address</a> (<code>::</code>) when IPv6 is available, or the
960<a href="https://en.wikipedia.org/wiki/0.0.0.0">unspecified IPv4 address</a> (<code>0.0.0.0</code>) otherwise.</p>
961<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>)
962may 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>
963(<code>0.0.0.0</code>).</p>
964<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>
965<div class="api_metadata">
966<span>Added in: v5.7.0</span>
967</div>
968<ul>
969<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Indicates whether or not the server is listening for connections.</li>
970</ul>
971<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>
972<div class="api_metadata">
973<span>Added in: v0.2.0</span>
974</div>
975<ul>
976<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
977</ul>
978<p>Set this property to reject connections when the server's connection count gets
979high.</p>
980<p>It is not recommended to use this option once a socket has been sent to a child
981with <a href="child_process.html#child_processforkmodulepath-args-options"><code>child_process.fork()</code></a>.</p>
982<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>
983<div class="api_metadata">
984<span>Added in: v0.9.1</span>
985</div>
986<ul>
987<li>Returns: <a href="net.html#class-netserver" class="type">&#x3C;net.Server></a></li>
988</ul>
989<p>Opposite of <code>unref()</code>, calling <code>ref()</code> on a previously <code>unref</code>ed server will
990<em>not</em> let the program exit if it's the only server left (the default behavior).
991If the server is <code>ref</code>ed calling <code>ref()</code> again will have no effect.</p>
992<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>
993<div class="api_metadata">
994<span>Added in: v0.9.1</span>
995</div>
996<ul>
997<li>Returns: <a href="net.html#class-netserver" class="type">&#x3C;net.Server></a></li>
998</ul>
999<p>Calling <code>unref()</code> on a server will allow the program to exit if this is the only
1000active server in the event system. If the server is already <code>unref</code>ed calling
1001<code>unref()</code> again will have no effect.</p>
1002</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>
1003<div class="api_metadata">
1004<span>Added in: v0.3.4</span>
1005</div>
1006<ul>
1007<li>Extends: <a href="stream.html#class-streamduplex" class="type">&#x3C;stream.Duplex></a></li>
1008</ul>
1009<p>This class is an abstraction of a TCP socket or a streaming <a href="#ipc-support">IPC</a> endpoint
1010(uses named pipes on Windows, and Unix domain sockets otherwise). It is also
1011an <a href="events.html#class-eventemitter"><code>EventEmitter</code></a>.</p>
1012<p>A <code>net.Socket</code> can be created by the user and used directly to interact with
1013a server. For example, it is returned by <a href="#netcreateconnection"><code>net.createConnection()</code></a>,
1014so the user can use it to talk to the server.</p>
1015<p>It can also be created by Node.js and passed to the user when a connection
1016is received. For example, it is passed to the listeners of a
1017<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
1018it to interact with the client.</p>
1019<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>
1020<div class="api_metadata">
1021<details class="changelog"><summary>History</summary>
1022<table>
1023<tbody><tr><th>Version</th><th>Changes</th></tr>
1024<tr><td>v15.14.0</td>
1025<td><p>AbortSignal support was added.</p></td></tr>
1026<tr><td>v0.3.4</td>
1027<td><p><span>Added in: v0.3.4</span></p></td></tr>
1028</tbody></table>
1029</details>
1030</div>
1031<ul>
1032<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> Available options are:
1033<ul>
1034<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> If specified, wrap around an existing socket with
1035the given file descriptor, otherwise a new socket will be created.</li>
1036<li><code>allowHalfOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If set to <code>false</code>, then the socket will
1037automatically end the writable side when the readable side ends. See
1038<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>
1039<code>false</code>.</li>
1040<li><code>readable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Allow reads on the socket when an <code>fd</code> is passed,
1041otherwise ignored. <strong>Default:</strong> <code>false</code>.</li>
1042<li><code>writable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Allow writes on the socket when an <code>fd</code> is passed,
1043otherwise ignored. <strong>Default:</strong> <code>false</code>.</li>
1044<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type">&#x3C;AbortSignal></a> An Abort signal that may be used to destroy the
1045socket.</li>
1046</ul>
1047</li>
1048<li>Returns: <a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a></li>
1049</ul>
1050<p>Creates a new socket object.</p>
1051<p>The newly created socket can be either a TCP socket or a streaming <a href="#ipc-support">IPC</a>
1052endpoint, depending on what it <a href="#socketconnect"><code>connect()</code></a> to.</p>
1053<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>
1054<div class="api_metadata">
1055<span>Added in: v0.1.90</span>
1056</div>
1057<ul>
1058<li><code>hadError</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <code>true</code> if the socket had a transmission error.</li>
1059</ul>
1060<p>Emitted once the socket is fully closed. The argument <code>hadError</code> is a boolean
1061which says if the socket was closed due to a transmission error.</p>
1062<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>
1063<div class="api_metadata">
1064<span>Added in: v0.1.90</span>
1065</div>
1066<p>Emitted when a socket connection is successfully established.
1067See <a href="#netcreateconnection"><code>net.createConnection()</code></a>.</p>
1068<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>
1069<div class="api_metadata">
1070<span>Added in: v0.1.90</span>
1071</div>
1072<ul>
1073<li><a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1074</ul>
1075<p>Emitted when data is received. The argument <code>data</code> will be a <code>Buffer</code> or
1076<code>String</code>. Encoding of data is set by <a href="#socketsetencodingencoding"><code>socket.setEncoding()</code></a>.</p>
1077<p>The data will be lost if there is no listener when a <code>Socket</code>
1078emits a <code>'data'</code> event.</p>
1079<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>
1080<div class="api_metadata">
1081<span>Added in: v0.1.90</span>
1082</div>
1083<p>Emitted when the write buffer becomes empty. Can be used to throttle uploads.</p>
1084<p>See also: the return values of <code>socket.write()</code>.</p>
1085<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>
1086<div class="api_metadata">
1087<span>Added in: v0.1.90</span>
1088</div>
1089<p>Emitted when the other end of the socket signals the end of transmission, thus
1090ending the readable side of the socket.</p>
1091<p>By default (<code>allowHalfOpen</code> is <code>false</code>) the socket will send an end of
1092transmission packet back and destroy its file descriptor once it has written out
1093its pending write queue. However, if <code>allowHalfOpen</code> is set to <code>true</code>, the
1094socket will not automatically <a href="#socketenddata-encoding-callback"><code>end()</code></a> its writable side,
1095allowing the user to write arbitrary amounts of data. The user must call
1096<a href="#socketenddata-encoding-callback"><code>end()</code></a> explicitly to close the connection (i.e. sending a
1097FIN packet back).</p>
1098<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>
1099<div class="api_metadata">
1100<span>Added in: v0.1.90</span>
1101</div>
1102<ul>
1103<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
1104</ul>
1105<p>Emitted when an error occurs. The <code>'close'</code> event will be called directly
1106following this event.</p>
1107<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>
1108<div class="api_metadata">
1109<details class="changelog"><summary>History</summary>
1110<table>
1111<tbody><tr><th>Version</th><th>Changes</th></tr>
1112<tr><td>v5.10.0</td>
1113<td><p>The <code>host</code> parameter is supported now.</p></td></tr>
1114<tr><td>v0.11.3</td>
1115<td><p><span>Added in: v0.11.3</span></p></td></tr>
1116</tbody></table>
1117</details>
1118</div>
1119<p>Emitted after resolving the host name but before connecting.
1120Not applicable to Unix sockets.</p>
1121<ul>
1122<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> The error object. See <a href="dns.html#dnslookuphostname-options-callback"><code>dns.lookup()</code></a>.</li>
1123<li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The IP address.</li>
1124<li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> The address type. See <a href="dns.html#dnslookuphostname-options-callback"><code>dns.lookup()</code></a>.</li>
1125<li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The host name.</li>
1126</ul>
1127<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>
1128<div class="api_metadata">
1129<span>Added in: v9.11.0</span>
1130</div>
1131<p>Emitted when a socket is ready to be used.</p>
1132<p>Triggered immediately after <code>'connect'</code>.</p>
1133<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>
1134<div class="api_metadata">
1135<span>Added in: v0.1.90</span>
1136</div>
1137<p>Emitted if the socket times out from inactivity. This is only to notify that
1138the socket has been idle. The user must manually close the connection.</p>
1139<p>See also: <a href="#socketsettimeouttimeout-callback"><code>socket.setTimeout()</code></a>.</p>
1140<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>
1141<div class="api_metadata">
1142<details class="changelog"><summary>History</summary>
1143<table>
1144<tbody><tr><th>Version</th><th>Changes</th></tr>
1145<tr><td>v18.4.0</td>
1146<td><p>The <code>family</code> property now returns a string instead of a number.</p></td></tr>
1147<tr><td>v18.0.0</td>
1148<td><p>The <code>family</code> property now returns a number instead of a string.</p></td></tr>
1149<tr><td>v0.1.90</td>
1150<td><p><span>Added in: v0.1.90</span></p></td></tr>
1151</tbody></table>
1152</details>
1153</div>
1154<ul>
1155<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
1156</ul>
1157<p>Returns the bound <code>address</code>, the address <code>family</code> name and <code>port</code> of the
1158socket as reported by the operating system:
1159<code>{ port: 12346, family: 'IPv4', address: '127.0.0.1' }</code></p>
1160<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>
1161<div class="api_metadata">
1162<span>Added in: v18.18.0</span>
1163</div>
1164<ul>
1165<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a></li>
1166</ul>
1167<p>This property is only present if the family autoselection algorithm is enabled in
1168<a href="#socketconnectoptions-connectlistener"><code>socket.connect(options)</code></a> and it is an array of the addresses that have been attempted.</p>
1169<p>Each address is a string in the form of <code>$IP:$PORT</code>. If the connection was successful,
1170then the last address is the one that the socket is currently connected to.</p>
1171<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>
1172<div class="api_metadata">
1173<span>Added in: v0.3.8</span><span>Deprecated since: v14.6.0</span>
1174</div>
1175<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>
1176<ul>
1177<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
1178</ul>
1179<p>This property shows the number of characters buffered for writing. The buffer
1180may contain strings whose length after encoding is not yet known. So this number
1181is only an approximation of the number of bytes in the buffer.</p>
1182<p><code>net.Socket</code> has the property that <code>socket.write()</code> always works. This is to
1183help users get up and running quickly. The computer cannot always keep up
1184with the amount of data that is written to a socket. The network connection
1185simply might be too slow. Node.js will internally queue up the data written to a
1186socket and send it out over the wire when it is possible.</p>
1187<p>The consequence of this internal buffering is that memory may grow.
1188Users who experience large or growing <code>bufferSize</code> should attempt to
1189"throttle" the data flows in their program with
1190<a href="#socketpause"><code>socket.pause()</code></a> and <a href="#socketresume"><code>socket.resume()</code></a>.</p>
1191<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>
1192<div class="api_metadata">
1193<span>Added in: v0.5.3</span>
1194</div>
1195<ul>
1196<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
1197</ul>
1198<p>The amount of received bytes.</p>
1199<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>
1200<div class="api_metadata">
1201<span>Added in: v0.5.3</span>
1202</div>
1203<ul>
1204<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
1205</ul>
1206<p>The amount of bytes sent.</p>
1207<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>
1208<p>Initiate a connection on a given socket.</p>
1209<p>Possible signatures:</p>
1210<ul>
1211<li><a href="#socketconnectoptions-connectlistener"><code>socket.connect(options[, connectListener])</code></a></li>
1212<li><a href="#socketconnectpath-connectlistener"><code>socket.connect(path[, connectListener])</code></a>
1213for <a href="#ipc-support">IPC</a> connections.</li>
1214<li><a href="#socketconnectport-host-connectlistener"><code>socket.connect(port[, host][, connectListener])</code></a>
1215for TCP connections.</li>
1216<li>Returns: <a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a> The socket itself.</li>
1217</ul>
1218<p>This function is asynchronous. When the connection is established, the
1219<a href="#event-connect"><code>'connect'</code></a> event will be emitted. If there is a problem connecting,
1220instead 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
1221the error passed to the <a href="#event-error_1"><code>'error'</code></a> listener.
1222The last parameter <code>connectListener</code>, if supplied, will be added as a listener
1223for the <a href="#event-connect"><code>'connect'</code></a> event <strong>once</strong>.</p>
1224<p>This function should only be used for reconnecting a socket after
1225<code>'close'</code> has been emitted or otherwise it may lead to undefined
1226behavior.</p>
1227<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>
1228<div class="api_metadata">
1229<details class="changelog"><summary>History</summary>
1230<table>
1231<tbody><tr><th>Version</th><th>Changes</th></tr>
1232<tr><td>v18.18.0</td>
1233<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>
1234<tr><td>v18.13.0</td>
1235<td><p>Added the <code>autoSelectFamily</code> option.</p></td></tr>
1236<tr><td>v17.7.0</td>
1237<td><p>The <code>noDelay</code>, <code>keepAlive</code>, and <code>keepAliveInitialDelay</code> options are supported now.</p></td></tr>
1238<tr><td>v12.10.0</td>
1239<td><p>Added <code>onread</code> option.</p></td></tr>
1240<tr><td>v6.0.0</td>
1241<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>
1242<tr><td>v5.11.0</td>
1243<td><p>The <code>hints</code> option is supported now.</p></td></tr>
1244<tr><td>v0.1.90</td>
1245<td><p><span>Added in: v0.1.90</span></p></td></tr>
1246</tbody></table>
1247</details>
1248</div>
1249<ul>
1250<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
1251<li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Common parameter of <a href="#socketconnect"><code>socket.connect()</code></a>
1252methods. Will be added as a listener for the <a href="#event-connect"><code>'connect'</code></a> event once.</li>
1253<li>Returns: <a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a> The socket itself.</li>
1254</ul>
1255<p>Initiate a connection on a given socket. Normally this method is not needed,
1256the socket should be created and opened with <a href="#netcreateconnection"><code>net.createConnection()</code></a>. Use
1257this only when implementing a custom Socket.</p>
1258<p>For TCP connections, available <code>options</code> are:</p>
1259<ul>
1260<li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Required. Port the socket should connect to.</li>
1261<li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Host the socket should connect to. <strong>Default:</strong> <code>'localhost'</code>.</li>
1262<li><code>localAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Local address the socket should connect from.</li>
1263<li><code>localPort</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Local port the socket should connect from.</li>
1264<li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a>: Version of IP stack. Must be <code>4</code>, <code>6</code>, or <code>0</code>. The value
1265<code>0</code> indicates that both IPv4 and IPv6 addresses are allowed. <strong>Default:</strong> <code>0</code>.</li>
1266<li><code>hints</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Optional <a href="dns.html#supported-getaddrinfo-flags"><code>dns.lookup()</code> hints</a>.</li>
1267<li><code>lookup</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Custom lookup function. <strong>Default:</strong> <a href="dns.html#dnslookuphostname-options-callback"><code>dns.lookup()</code></a>.</li>
1268<li><code>noDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If set to <code>true</code>, it disables the use of Nagle's algorithm immediately
1269after the socket is established. <strong>Default:</strong> <code>false</code>.</li>
1270<li><code>keepAlive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If set to <code>true</code>, it enables keep-alive functionality on the socket
1271immediately after the connection is established, similarly on what is done in
1272<a href="#socketsetkeepaliveenable-initialdelay"><code>socket.setKeepAlive([enable][, initialDelay])</code></a>.
1273<strong>Default:</strong> <code>false</code>.</li>
1274<li><code>keepAliveInitialDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> If set to a positive number, it sets the initial delay before
1275the first keepalive probe is sent on an idle socket.<strong>Default:</strong> <code>0</code>.</li>
1276<li><code>autoSelectFamily</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a>: If set to <code>true</code>, it enables a family autodetection algorithm
1277that loosely implements section 5 of <a href="https://www.rfc-editor.org/rfc/rfc8305.txt">RFC 8305</a>.
1278The <code>all</code> option passed to lookup is set to <code>true</code> and the sockets attempts to connect to all
1279obtained IPv6 and IPv4 addresses, in sequence, until a connection is established.
1280The first returned AAAA address is tried first, then the first returned A address,
1281then the second returned AAAA address and so on.
1282Each connection attempt is given the amount of time specified by the <code>autoSelectFamilyAttemptTimeout</code>
1283option before timing out and trying the next address.
1284Ignored if the <code>family</code> option is not <code>0</code> or if <code>localAddress</code> is set.
1285Connection errors are not emitted if at least one connection succeeds.
1286<strong>Default:</strong> initially <code>false</code>, but it can be changed at runtime using <a href="#netsetdefaultautoselectfamilyvalue"><code>net.setDefaultAutoSelectFamily(value)</code></a>
1287or via the command line option <code>--enable-network-family-autoselection</code>.</li>
1288<li><code>autoSelectFamilyAttemptTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a>: The amount of time in milliseconds to wait
1289for a connection attempt to finish before trying the next address when using the <code>autoSelectFamily</code> option.
1290If set to a positive integer less than <code>10</code>, then the value <code>10</code> will be used instead.
1291<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>
1292</ul>
1293<p>For <a href="#ipc-support">IPC</a> connections, available <code>options</code> are:</p>
1294<ul>
1295<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Required. Path the client should connect to.
1296See <a href="#identifying-paths-for-ipc-connections">Identifying paths for IPC connections</a>. If provided, the TCP-specific
1297options above are ignored.</li>
1298</ul>
1299<p>For both types, available <code>options</code> include:</p>
1300<ul>
1301<li><code>onread</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> If specified, incoming data is stored in a single <code>buffer</code>
1302and passed to the supplied <code>callback</code> when data arrives on the socket.
1303This will cause the streaming functionality to not provide any data.
1304The socket will emit events like <code>'error'</code>, <code>'end'</code>, and <code>'close'</code>
1305as usual. Methods like <code>pause()</code> and <code>resume()</code> will also behave as
1306expected.
1307<ul>
1308<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Either a reusable chunk of memory to
1309use for storing incoming data or a function that returns such.</li>
1310<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> This function is called for every chunk of incoming
1311data. Two arguments are passed to it: the number of bytes written to
1312<code>buffer</code> and a reference to <code>buffer</code>. Return <code>false</code> from this function to
1313implicitly <code>pause()</code> the socket. This function will be executed in the
1314global context.</li>
1315</ul>
1316</li>
1317</ul>
1318<p>Following is an example of a client using the <code>onread</code> option:</p>
1319<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>);
1320net.<span class="hljs-title function_">connect</span>({
1321  <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>,
1322  <span class="hljs-attr">onread</span>: {
1323    <span class="hljs-comment">// Reuses a 4KiB Buffer for every read from the socket.</span>
1324    <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>),
1325    <span class="hljs-attr">callback</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">nread, buf</span>) {
1326      <span class="hljs-comment">// Received data is available in `buf` from 0 to `nread`.</span>
1327      <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));
1328    },
1329  },
1330});</code> <button class="copy-button">copy</button></pre>
1331<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>
1332<ul>
1333<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Path the client should connect to. See
1334<a href="#identifying-paths-for-ipc-connections">Identifying paths for IPC connections</a>.</li>
1335<li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Common parameter of <a href="#socketconnect"><code>socket.connect()</code></a>
1336methods. Will be added as a listener for the <a href="#event-connect"><code>'connect'</code></a> event once.</li>
1337<li>Returns: <a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a> The socket itself.</li>
1338</ul>
1339<p>Initiate an <a href="#ipc-support">IPC</a> connection on the given socket.</p>
1340<p>Alias to
1341<a href="#socketconnectoptions-connectlistener"><code>socket.connect(options[, connectListener])</code></a>
1342called with <code>{ path: path }</code> as <code>options</code>.</p>
1343<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>
1344<div class="api_metadata">
1345<span>Added in: v0.1.90</span>
1346</div>
1347<ul>
1348<li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Port the client should connect to.</li>
1349<li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Host the client should connect to.</li>
1350<li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Common parameter of <a href="#socketconnect"><code>socket.connect()</code></a>
1351methods. Will be added as a listener for the <a href="#event-connect"><code>'connect'</code></a> event once.</li>
1352<li>Returns: <a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a> The socket itself.</li>
1353</ul>
1354<p>Initiate a TCP connection on the given socket.</p>
1355<p>Alias to
1356<a href="#socketconnectoptions-connectlistener"><code>socket.connect(options[, connectListener])</code></a>
1357called with <code>{port: port, host: host}</code> as <code>options</code>.</p>
1358<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>
1359<div class="api_metadata">
1360<span>Added in: v6.1.0</span>
1361</div>
1362<ul>
1363<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1364</ul>
1365<p>If <code>true</code>,
1366<a href="#socketconnectoptions-connectlistener"><code>socket.connect(options[, connectListener])</code></a> was
1367called and has not yet finished. It will stay <code>true</code> until the socket becomes
1368connected, then it is set to <code>false</code> and the <code>'connect'</code> event is emitted. Note
1369that the
1370<a href="#socketconnectoptions-connectlistener"><code>socket.connect(options[, connectListener])</code></a>
1371callback is a listener for the <code>'connect'</code> event.</p>
1372<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>
1373<div class="api_metadata">
1374<span>Added in: v0.1.90</span>
1375</div>
1376<ul>
1377<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
1378<li>Returns: <a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a></li>
1379</ul>
1380<p>Ensures that no more I/O activity happens on this socket.
1381Destroys the stream and closes the connection.</p>
1382<p>See <a href="stream.html#writabledestroyerror"><code>writable.destroy()</code></a> for further details.</p>
1383<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>
1384<ul>
1385<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Indicates if the connection is destroyed or not. Once a
1386connection is destroyed no further data can be transferred using it.</li>
1387</ul>
1388<p>See <a href="stream.html#writabledestroyed"><code>writable.destroyed</code></a> for further details.</p>
1389<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>
1390<div class="api_metadata">
1391<span>Added in: v0.3.4</span>
1392</div>
1393<p>Destroys the socket after all data is written. If the <code>'finish'</code> event was
1394already emitted the socket is destroyed immediately. If the socket is still
1395writable it implicitly calls <code>socket.end()</code>.</p>
1396<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>
1397<div class="api_metadata">
1398<span>Added in: v0.1.90</span>
1399</div>
1400<ul>
1401<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a></li>
1402<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Only used when data is <code>string</code>. <strong>Default:</strong> <code>'utf8'</code>.</li>
1403<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Optional callback for when the socket is finished.</li>
1404<li>Returns: <a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a> The socket itself.</li>
1405</ul>
1406<p>Half-closes the socket. i.e., it sends a FIN packet. It is possible the
1407server will still send some data.</p>
1408<p>See <a href="stream.html#writableendchunk-encoding-callback"><code>writable.end()</code></a> for further details.</p>
1409<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>
1410<div class="api_metadata">
1411<span>Added in: v0.9.6</span>
1412</div>
1413<ul>
1414<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1415</ul>
1416<p>The string representation of the local IP address the remote client is
1417connecting on. For example, in a server listening on <code>'0.0.0.0'</code>, if a client
1418connects on <code>'192.168.1.1'</code>, the value of <code>socket.localAddress</code> would be
1419<code>'192.168.1.1'</code>.</p>
1420<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>
1421<div class="api_metadata">
1422<span>Added in: v0.9.6</span>
1423</div>
1424<ul>
1425<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
1426</ul>
1427<p>The numeric representation of the local port. For example, <code>80</code> or <code>21</code>.</p>
1428<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>
1429<div class="api_metadata">
1430<span>Added in: v18.8.0</span>
1431</div>
1432<ul>
1433<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1434</ul>
1435<p>The string representation of the local IP family. <code>'IPv4'</code> or <code>'IPv6'</code>.</p>
1436<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>
1437<ul>
1438<li>Returns: <a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a> The socket itself.</li>
1439</ul>
1440<p>Pauses the reading of data. That is, <a href="#event-data"><code>'data'</code></a> events will not be emitted.
1441Useful to throttle back an upload.</p>
1442<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>
1443<div class="api_metadata">
1444<span>Added in: v11.2.0, v10.16.0</span>
1445</div>
1446<ul>
1447<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1448</ul>
1449<p>This is <code>true</code> if the socket is not connected yet, either because <code>.connect()</code>
1450has not yet been called or because it is still in the process of connecting
1451(see <a href="#socketconnecting"><code>socket.connecting</code></a>).</p>
1452<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>
1453<div class="api_metadata">
1454<span>Added in: v0.9.1</span>
1455</div>
1456<ul>
1457<li>Returns: <a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a> The socket itself.</li>
1458</ul>
1459<p>Opposite of <code>unref()</code>, calling <code>ref()</code> on a previously <code>unref</code>ed socket will
1460<em>not</em> let the program exit if it's the only socket left (the default behavior).
1461If the socket is <code>ref</code>ed calling <code>ref</code> again will have no effect.</p>
1462<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>
1463<div class="api_metadata">
1464<span>Added in: v0.5.10</span>
1465</div>
1466<ul>
1467<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1468</ul>
1469<p>The string representation of the remote IP address. For example,
1470<code>'74.125.127.100'</code> or <code>'2001:4860:a005::68'</code>. Value may be <code>undefined</code> if
1471the socket is destroyed (for example, if the client disconnected).</p>
1472<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>
1473<div class="api_metadata">
1474<span>Added in: v0.11.14</span>
1475</div>
1476<ul>
1477<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1478</ul>
1479<p>The string representation of the remote IP family. <code>'IPv4'</code> or <code>'IPv6'</code>. Value may be <code>undefined</code> if
1480the socket is destroyed (for example, if the client disconnected).</p>
1481<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>
1482<div class="api_metadata">
1483<span>Added in: v0.5.10</span>
1484</div>
1485<ul>
1486<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
1487</ul>
1488<p>The numeric representation of the remote port. For example, <code>80</code> or <code>21</code>. Value may be <code>undefined</code> if
1489the socket is destroyed (for example, if the client disconnected).</p>
1490<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>
1491<div class="api_metadata">
1492<span>Added in: v18.3.0</span>
1493</div>
1494<ul>
1495<li>Returns: <a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a></li>
1496</ul>
1497<p>Close the TCP connection by sending an RST packet and destroy the stream.
1498If this TCP socket is in connecting status, it will send an RST packet and destroy this TCP socket once it is connected.
1499Otherwise, it will call <code>socket.destroy</code> with an <code>ERR_SOCKET_CLOSED</code> Error.
1500If 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>
1501<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>
1502<ul>
1503<li>Returns: <a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a> The socket itself.</li>
1504</ul>
1505<p>Resumes reading after a call to <a href="#socketpause"><code>socket.pause()</code></a>.</p>
1506<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>
1507<div class="api_metadata">
1508<span>Added in: v0.1.90</span>
1509</div>
1510<ul>
1511<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1512<li>Returns: <a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a> The socket itself.</li>
1513</ul>
1514<p>Set the encoding for the socket as a <a href="stream.html#class-streamreadable">Readable Stream</a>. See
1515<a href="stream.html#readablesetencodingencoding"><code>readable.setEncoding()</code></a> for more information.</p>
1516<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>
1517<div class="api_metadata">
1518<details class="changelog"><summary>History</summary>
1519<table>
1520<tbody><tr><th>Version</th><th>Changes</th></tr>
1521<tr><td>v13.12.0, v12.17.0</td>
1522<td><p>New defaults for <code>TCP_KEEPCNT</code> and <code>TCP_KEEPINTVL</code> socket options were added.</p></td></tr>
1523<tr><td>v0.1.92</td>
1524<td><p><span>Added in: v0.1.92</span></p></td></tr>
1525</tbody></table>
1526</details>
1527</div>
1528<ul>
1529<li><code>enable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <strong>Default:</strong> <code>false</code></li>
1530<li><code>initialDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <strong>Default:</strong> <code>0</code></li>
1531<li>Returns: <a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a> The socket itself.</li>
1532</ul>
1533<p>Enable/disable keep-alive functionality, and optionally set the initial
1534delay before the first keepalive probe is sent on an idle socket.</p>
1535<p>Set <code>initialDelay</code> (in milliseconds) to set the delay between the last
1536data packet received and the first keepalive probe. Setting <code>0</code> for
1537<code>initialDelay</code> will leave the value unchanged from the default
1538(or previous) setting.</p>
1539<p>Enabling the keep-alive functionality will set the following socket options:</p>
1540<ul>
1541<li><code>SO_KEEPALIVE=1</code></li>
1542<li><code>TCP_KEEPIDLE=initialDelay</code></li>
1543<li><code>TCP_KEEPCNT=10</code></li>
1544<li><code>TCP_KEEPINTVL=1</code></li>
1545</ul>
1546<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>
1547<div class="api_metadata">
1548<span>Added in: v0.1.90</span>
1549</div>
1550<ul>
1551<li><code>noDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <strong>Default:</strong> <code>true</code></li>
1552<li>Returns: <a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a> The socket itself.</li>
1553</ul>
1554<p>Enable/disable the use of Nagle's algorithm.</p>
1555<p>When a TCP connection is created, it will have Nagle's algorithm enabled.</p>
1556<p>Nagle's algorithm delays data before it is sent via the network. It attempts
1557to optimize throughput at the expense of latency.</p>
1558<p>Passing <code>true</code> for <code>noDelay</code> or not passing an argument will disable Nagle's
1559algorithm for the socket. Passing <code>false</code> for <code>noDelay</code> will enable Nagle's
1560algorithm.</p>
1561<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>
1562<div class="api_metadata">
1563<details class="changelog"><summary>History</summary>
1564<table>
1565<tbody><tr><th>Version</th><th>Changes</th></tr>
1566<tr><td>v18.0.0</td>
1567<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>
1568<tr><td>v0.1.90</td>
1569<td><p><span>Added in: v0.1.90</span></p></td></tr>
1570</tbody></table>
1571</details>
1572</div>
1573<ul>
1574<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1575<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
1576<li>Returns: <a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a> The socket itself.</li>
1577</ul>
1578<p>Sets the socket to timeout after <code>timeout</code> milliseconds of inactivity on
1579the socket. By default <code>net.Socket</code> do not have a timeout.</p>
1580<p>When an idle timeout is triggered the socket will receive a <a href="#event-timeout"><code>'timeout'</code></a>
1581event but the connection will not be severed. The user must manually call
1582<a href="#socketenddata-encoding-callback"><code>socket.end()</code></a> or <a href="#socketdestroyerror"><code>socket.destroy()</code></a> to end the connection.</p>
1583<pre><code class="language-js">socket.<span class="hljs-built_in">setTimeout</span>(<span class="hljs-number">3000</span>);
1584socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'timeout'</span>, <span class="hljs-function">() =></span> {
1585  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'socket timeout'</span>);
1586  socket.<span class="hljs-title function_">end</span>();
1587});</code> <button class="copy-button">copy</button></pre>
1588<p>If <code>timeout</code> is 0, then the existing idle timeout is disabled.</p>
1589<p>The optional <code>callback</code> parameter will be added as a one-time listener for the
1590<a href="#event-timeout"><code>'timeout'</code></a> event.</p>
1591<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>
1592<div class="api_metadata">
1593<span>Added in: v10.7.0</span>
1594</div>
1595<ul>
1596<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a></li>
1597</ul>
1598<p>The socket timeout in milliseconds as set by <a href="#socketsettimeouttimeout-callback"><code>socket.setTimeout()</code></a>.
1599It is <code>undefined</code> if a timeout has not been set.</p>
1600<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>
1601<div class="api_metadata">
1602<span>Added in: v0.9.1</span>
1603</div>
1604<ul>
1605<li>Returns: <a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a> The socket itself.</li>
1606</ul>
1607<p>Calling <code>unref()</code> on a socket will allow the program to exit if this is the only
1608active socket in the event system. If the socket is already <code>unref</code>ed calling
1609<code>unref()</code> again will have no effect.</p>
1610<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>
1611<div class="api_metadata">
1612<span>Added in: v0.1.90</span>
1613</div>
1614<ul>
1615<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a></li>
1616<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Only used when data is <code>string</code>. <strong>Default:</strong> <code>utf8</code>.</li>
1617<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
1618<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1619</ul>
1620<p>Sends data on the socket. The second parameter specifies the encoding in the
1621case of a string. It defaults to UTF8 encoding.</p>
1622<p>Returns <code>true</code> if the entire data was flushed successfully to the kernel
1623buffer. Returns <code>false</code> if all or part of the data was queued in user memory.
1624<a href="#event-drain"><code>'drain'</code></a> will be emitted when the buffer is again free.</p>
1625<p>The optional <code>callback</code> parameter will be executed when the data is finally
1626written out, which may not be immediately.</p>
1627<p>See <code>Writable</code> stream <a href="stream.html#writablewritechunk-encoding-callback"><code>write()</code></a> method for more
1628information.</p>
1629<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>
1630<div class="api_metadata">
1631<span>Added in: v0.5.0</span>
1632</div>
1633<ul>
1634<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1635</ul>
1636<p>This property represents the state of the connection as a string.</p>
1637<ul>
1638<li>If the stream is connecting <code>socket.readyState</code> is <code>opening</code>.</li>
1639<li>If the stream is readable and writable, it is <code>open</code>.</li>
1640<li>If the stream is readable and not writable, it is <code>readOnly</code>.</li>
1641<li>If the stream is not readable and writable, it is <code>writeOnly</code>.</li>
1642</ul>
1643</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>
1644<p>Aliases to
1645<a href="#netcreateconnection"><code>net.createConnection()</code></a>.</p>
1646<p>Possible signatures:</p>
1647<ul>
1648<li><a href="#netconnectoptions-connectlistener"><code>net.connect(options[, connectListener])</code></a></li>
1649<li><a href="#netconnectpath-connectlistener"><code>net.connect(path[, connectListener])</code></a> for <a href="#ipc-support">IPC</a>
1650connections.</li>
1651<li><a href="#netconnectport-host-connectlistener"><code>net.connect(port[, host][, connectListener])</code></a>
1652for TCP connections.</li>
1653</ul>
1654<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>
1655<div class="api_metadata">
1656<span>Added in: v0.7.0</span>
1657</div>
1658<ul>
1659<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
1660<li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
1661<li>Returns: <a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a></li>
1662</ul>
1663<p>Alias to
1664<a href="#netcreateconnectionoptions-connectlistener"><code>net.createConnection(options[, connectListener])</code></a>.</p>
1665<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>
1666<div class="api_metadata">
1667<span>Added in: v0.1.90</span>
1668</div>
1669<ul>
1670<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1671<li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
1672<li>Returns: <a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a></li>
1673</ul>
1674<p>Alias to
1675<a href="#netcreateconnectionpath-connectlistener"><code>net.createConnection(path[, connectListener])</code></a>.</p>
1676<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>
1677<div class="api_metadata">
1678<span>Added in: v0.1.90</span>
1679</div>
1680<ul>
1681<li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1682<li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1683<li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
1684<li>Returns: <a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a></li>
1685</ul>
1686<p>Alias to
1687<a href="#netcreateconnectionport-host-connectlistener"><code>net.createConnection(port[, host][, connectListener])</code></a>.</p>
1688</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>
1689<p>A factory function, which creates a new <a href="#class-netsocket"><code>net.Socket</code></a>,
1690immediately initiates connection with <a href="#socketconnect"><code>socket.connect()</code></a>,
1691then returns the <code>net.Socket</code> that starts the connection.</p>
1692<p>When the connection is established, a <a href="#event-connect"><code>'connect'</code></a> event will be emitted
1693on the returned socket. The last parameter <code>connectListener</code>, if supplied,
1694will be added as a listener for the <a href="#event-connect"><code>'connect'</code></a> event <strong>once</strong>.</p>
1695<p>Possible signatures:</p>
1696<ul>
1697<li><a href="#netcreateconnectionoptions-connectlistener"><code>net.createConnection(options[, connectListener])</code></a></li>
1698<li><a href="#netcreateconnectionpath-connectlistener"><code>net.createConnection(path[, connectListener])</code></a>
1699for <a href="#ipc-support">IPC</a> connections.</li>
1700<li><a href="#netcreateconnectionport-host-connectlistener"><code>net.createConnection(port[, host][, connectListener])</code></a>
1701for TCP connections.</li>
1702</ul>
1703<p>The <a href="#netconnect"><code>net.connect()</code></a> function is an alias to this function.</p>
1704<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>
1705<div class="api_metadata">
1706<span>Added in: v0.1.90</span>
1707</div>
1708<ul>
1709<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> Required. Will be passed to both the
1710<a href="#new-netsocketoptions"><code>new net.Socket([options])</code></a> call and the
1711<a href="#socketconnectoptions-connectlistener"><code>socket.connect(options[, connectListener])</code></a>
1712method.</li>
1713<li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Common parameter of the
1714<a href="#netcreateconnection"><code>net.createConnection()</code></a> functions. If supplied, will be added as
1715a listener for the <a href="#event-connect"><code>'connect'</code></a> event on the returned socket once.</li>
1716<li>Returns: <a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a> The newly created socket used to start the connection.</li>
1717</ul>
1718<p>For available options, see
1719<a href="#new-netsocketoptions"><code>new net.Socket([options])</code></a>
1720and <a href="#socketconnectoptions-connectlistener"><code>socket.connect(options[, connectListener])</code></a>.</p>
1721<p>Additional options:</p>
1722<ul>
1723<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> If set, will be used to call
1724<a href="#socketsettimeouttimeout-callback"><code>socket.setTimeout(timeout)</code></a> after the socket is created, but before
1725it starts the connection.</li>
1726</ul>
1727<p>Following is an example of a client of the echo server described
1728in the <a href="#netcreateserveroptions-connectionlistener"><code>net.createServer()</code></a> section:</p>
1729<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>);
1730<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> {
1731  <span class="hljs-comment">// 'connect' listener.</span>
1732  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'connected to server!'</span>);
1733  client.<span class="hljs-title function_">write</span>(<span class="hljs-string">'world!\r\n'</span>);
1734});
1735client.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> {
1736  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(data.<span class="hljs-title function_">toString</span>());
1737  client.<span class="hljs-title function_">end</span>();
1738});
1739client.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> {
1740  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'disconnected from server'</span>);
1741});</code> <button class="copy-button">copy</button></pre>
1742<p>To connect on the socket <code>/tmp/echo.sock</code>:</p>
1743<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>
1744<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>
1745<div class="api_metadata">
1746<span>Added in: v0.1.90</span>
1747</div>
1748<ul>
1749<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Path the socket should connect to. Will be passed to
1750<a href="#socketconnectpath-connectlistener"><code>socket.connect(path[, connectListener])</code></a>.
1751See <a href="#identifying-paths-for-ipc-connections">Identifying paths for IPC connections</a>.</li>
1752<li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Common parameter of the
1753<a href="#netcreateconnection"><code>net.createConnection()</code></a> functions, an "once" listener for the
1754<code>'connect'</code> event on the initiating socket. Will be passed to
1755<a href="#socketconnectpath-connectlistener"><code>socket.connect(path[, connectListener])</code></a>.</li>
1756<li>Returns: <a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a> The newly created socket used to start the connection.</li>
1757</ul>
1758<p>Initiates an <a href="#ipc-support">IPC</a> connection.</p>
1759<p>This function creates a new <a href="#class-netsocket"><code>net.Socket</code></a> with all options set to default,
1760immediately initiates connection with
1761<a href="#socketconnectpath-connectlistener"><code>socket.connect(path[, connectListener])</code></a>,
1762then returns the <code>net.Socket</code> that starts the connection.</p>
1763<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>
1764<div class="api_metadata">
1765<span>Added in: v0.1.90</span>
1766</div>
1767<ul>
1768<li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Port the socket should connect to. Will be passed to
1769<a href="#socketconnectport-host-connectlistener"><code>socket.connect(port[, host][, connectListener])</code></a>.</li>
1770<li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Host the socket should connect to. Will be passed to
1771<a href="#socketconnectport-host-connectlistener"><code>socket.connect(port[, host][, connectListener])</code></a>.
1772<strong>Default:</strong> <code>'localhost'</code>.</li>
1773<li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Common parameter of the
1774<a href="#netcreateconnection"><code>net.createConnection()</code></a> functions, an "once" listener for the
1775<code>'connect'</code> event on the initiating socket. Will be passed to
1776<a href="#socketconnectport-host-connectlistener"><code>socket.connect(port[, host][, connectListener])</code></a>.</li>
1777<li>Returns: <a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a> The newly created socket used to start the connection.</li>
1778</ul>
1779<p>Initiates a TCP connection.</p>
1780<p>This function creates a new <a href="#class-netsocket"><code>net.Socket</code></a> with all options set to default,
1781immediately initiates connection with
1782<a href="#socketconnectport-host-connectlistener"><code>socket.connect(port[, host][, connectListener])</code></a>,
1783then returns the <code>net.Socket</code> that starts the connection.</p>
1784</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>
1785<div class="api_metadata">
1786<details class="changelog"><summary>History</summary>
1787<table>
1788<tbody><tr><th>Version</th><th>Changes</th></tr>
1789<tr><td>v18.17.0</td>
1790<td><p>The <code>highWaterMark</code> option is supported now.</p></td></tr>
1791<tr><td>v17.7.0, v16.15.0</td>
1792<td><p>The <code>noDelay</code>, <code>keepAlive</code>, and <code>keepAliveInitialDelay</code> options are supported now.</p></td></tr>
1793<tr><td>v0.5.0</td>
1794<td><p><span>Added in: v0.5.0</span></p></td></tr>
1795</tbody></table>
1796</details>
1797</div>
1798<ul>
1799<li>
1800<p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></p>
1801<ul>
1802<li><code>allowHalfOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If set to <code>false</code>, then the socket will
1803automatically end the writable side when the readable side ends.
1804<strong>Default:</strong> <code>false</code>.</li>
1805<li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Optionally overrides all <a href="#class-netsocket"><code>net.Socket</code></a>s'
1806<code>readableHighWaterMark</code> and <code>writableHighWaterMark</code>.
1807<strong>Default:</strong> See <a href="stream.html#streamgetdefaulthighwatermarkobjectmode"><code>stream.getDefaultHighWaterMark()</code></a>.</li>
1808<li><code>pauseOnConnect</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Indicates whether the socket should be
1809paused on incoming connections. <strong>Default:</strong> <code>false</code>.</li>
1810<li><code>noDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If set to <code>true</code>, it disables the use of Nagle's algorithm immediately
1811after a new incoming connection is received. <strong>Default:</strong> <code>false</code>.</li>
1812<li><code>keepAlive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If set to <code>true</code>, it enables keep-alive functionality on the socket
1813immediately after a new incoming connection is received, similarly on what is done in
1814<a href="#socketsetkeepaliveenable-initialdelay"><code>socket.setKeepAlive([enable][, initialDelay])</code></a>.
1815<strong>Default:</strong> <code>false</code>.</li>
1816<li><code>keepAliveInitialDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> If set to a positive number, it sets the initial delay before
1817the first keepalive probe is sent on an idle socket.<strong>Default:</strong> <code>0</code>.</li>
1818</ul>
1819</li>
1820<li>
1821<p><code>connectionListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Automatically set as a listener for the
1822<a href="#event-connection"><code>'connection'</code></a> event.</p>
1823</li>
1824<li>
1825<p>Returns: <a href="net.html#class-netserver" class="type">&#x3C;net.Server></a></p>
1826</li>
1827</ul>
1828<p>Creates a new TCP or <a href="#ipc-support">IPC</a> server.</p>
1829<p>If <code>allowHalfOpen</code> is set to <code>true</code>, when the other end of the socket
1830signals the end of transmission, the server will only send back the end of
1831transmission when <a href="#socketenddata-encoding-callback"><code>socket.end()</code></a> is explicitly called. For example, in the
1832context of TCP, when a FIN packed is received, a FIN packed is sent
1833back only when <a href="#socketenddata-encoding-callback"><code>socket.end()</code></a> is explicitly called. Until then the
1834connection is half-closed (non-readable but still writable). See <a href="#event-end"><code>'end'</code></a>
1835event and <a href="https://tools.ietf.org/html/rfc1122">RFC 1122</a> (section 4.2.2.13) for more information.</p>
1836<p>If <code>pauseOnConnect</code> is set to <code>true</code>, then the socket associated with each
1837incoming connection will be paused, and no data will be read from its handle.
1838This allows connections to be passed between processes without any data being
1839read by the original process. To begin reading data from a paused socket, call
1840<a href="#socketresume"><code>socket.resume()</code></a>.</p>
1841<p>The server can be a TCP server or an <a href="#ipc-support">IPC</a> server, depending on what it
1842<a href="#serverlisten"><code>listen()</code></a> to.</p>
1843<p>Here is an example of a TCP echo server which listens for connections
1844on port 8124:</p>
1845<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>);
1846<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> {
1847  <span class="hljs-comment">// 'connection' listener.</span>
1848  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'client connected'</span>);
1849  c.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> {
1850    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'client disconnected'</span>);
1851  });
1852  c.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello\r\n'</span>);
1853  c.<span class="hljs-title function_">pipe</span>(c);
1854});
1855server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
1856  <span class="hljs-keyword">throw</span> err;
1857});
1858server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8124</span>, <span class="hljs-function">() =></span> {
1859  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'server bound'</span>);
1860});</code> <button class="copy-button">copy</button></pre>
1861<p>Test this by using <code>telnet</code>:</p>
1862<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>
1863<p>To listen on the socket <code>/tmp/echo.sock</code>:</p>
1864<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> {
1865  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'server bound'</span>);
1866});</code> <button class="copy-button">copy</button></pre>
1867<p>Use <code>nc</code> to connect to a Unix domain socket server:</p>
1868<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>
1869</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>
1870<div class="api_metadata">
1871<span>Added in: v19.4.0</span>
1872</div>
1873<p>Gets the current default value of the <code>autoSelectFamily</code> option of <a href="#socketconnectoptions-connectlistener"><code>socket.connect(options)</code></a>.</p>
1874<ul>
1875<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> The current default value of the <code>autoSelectFamily</code> option.</li>
1876</ul>
1877</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>
1878<div class="api_metadata">
1879<span>Added in: v19.4.0</span>
1880</div>
1881<p>Sets the default value of the <code>autoSelectFamily</code> option of <a href="#socketconnectoptions-connectlistener"><code>socket.connect(options)</code></a>.</p>
1882<ul>
1883<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> The new default value. The initial default value is <code>false</code>.</li>
1884</ul>
1885</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>
1886<div class="api_metadata">
1887<span>Added in: v18.18.0</span>
1888</div>
1889<p>Gets the current default value of the <code>autoSelectFamilyAttemptTimeout</code> option of <a href="#socketconnectoptions-connectlistener"><code>socket.connect(options)</code></a>.</p>
1890<ul>
1891<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The current default value of the <code>autoSelectFamilyAttemptTimeout</code> option.</li>
1892</ul>
1893</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>
1894<div class="api_metadata">
1895<span>Added in: v18.18.0</span>
1896</div>
1897<p>Sets the default value of the <code>autoSelectFamilyAttemptTimeout</code> option of <a href="#socketconnectoptions-connectlistener"><code>socket.connect(options)</code></a>.</p>
1898<ul>
1899<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The new default value, which must be a positive number. If the number is less than <code>10</code>,
1900the value <code>10</code> is used instead. The initial default value is <code>250</code>.</li>
1901</ul>
1902</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>
1903<div class="api_metadata">
1904<span>Added in: v0.3.0</span>
1905</div>
1906<ul>
1907<li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1908<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
1909</ul>
1910<p>Returns <code>6</code> if <code>input</code> is an IPv6 address. Returns <code>4</code> if <code>input</code> is an IPv4
1911address in <a href="https://en.wikipedia.org/wiki/Dot-decimal_notation">dot-decimal notation</a> with no leading zeroes. Otherwise, returns
1912<code>0</code>.</p>
1913<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>
1914net.<span class="hljs-title function_">isIP</span>(<span class="hljs-string">'127.0.0.1'</span>); <span class="hljs-comment">// returns 4</span>
1915net.<span class="hljs-title function_">isIP</span>(<span class="hljs-string">'127.000.000.001'</span>); <span class="hljs-comment">// returns 0</span>
1916net.<span class="hljs-title function_">isIP</span>(<span class="hljs-string">'127.0.0.1/24'</span>); <span class="hljs-comment">// returns 0</span>
1917net.<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>
1918</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>
1919<div class="api_metadata">
1920<span>Added in: v0.3.0</span>
1921</div>
1922<ul>
1923<li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1924<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1925</ul>
1926<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
1927leading zeroes. Otherwise, returns <code>false</code>.</p>
1928<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>
1929net.<span class="hljs-title function_">isIPv4</span>(<span class="hljs-string">'127.000.000.001'</span>); <span class="hljs-comment">// returns false</span>
1930net.<span class="hljs-title function_">isIPv4</span>(<span class="hljs-string">'127.0.0.1/24'</span>); <span class="hljs-comment">// returns false</span>
1931net.<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>
1932</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>
1933<div class="api_metadata">
1934<span>Added in: v0.3.0</span>
1935</div>
1936<ul>
1937<li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1938<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1939</ul>
1940<p>Returns <code>true</code> if <code>input</code> is an IPv6 address. Otherwise, returns <code>false</code>.</p>
1941<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>
1942net.<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>
1943        <!-- API END -->
1944      </div>
1945    </div>
1946  </div>
1947</body>
1948</html>
1949