Sophie

Sophie

distrib > Mageia > 6 > x86_64 > media > core-updates > by-pkgid > 4642cf16cdfb16e1cdaefd0999c41911 > files > 86

nodejs-docs-6.17.1-8.mga6.noarch.rpm

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Net | Node.js v6.17.1 Documentation</title>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic">
  <link rel="stylesheet" href="assets/style.css">
  <link rel="stylesheet" href="assets/sh.css">
  <link rel="canonical" href="https://nodejs.org/api/net.html">
</head>
<body class="alt apidoc" id="api-section-net">
  <div id="content" class="clearfix">
    <div id="column2" class="interior">
      <div id="intro" class="interior">
        <a href="/" title="Go back to the home page">
          Node.js
        </a>
      </div>
      <ul>
<li><a class="nav-documentation" href="documentation.html">About these Docs</a></li>
<li><a class="nav-synopsis" href="synopsis.html">Usage &amp; Example</a></li>
</ul>
<div class="line"></div>

<ul>
<li><a class="nav-assert" href="assert.html">Assertion Testing</a></li>
<li><a class="nav-buffer" href="buffer.html">Buffer</a></li>
<li><a class="nav-addons" href="addons.html">C++ Addons</a></li>
<li><a class="nav-n-api" href="n-api.html">C/C++ Addons - N-API</a></li>
<li><a class="nav-child_process" href="child_process.html">Child Processes</a></li>
<li><a class="nav-cluster" href="cluster.html">Cluster</a></li>
<li><a class="nav-cli" href="cli.html">Command Line Options</a></li>
<li><a class="nav-console" href="console.html">Console</a></li>
<li><a class="nav-crypto" href="crypto.html">Crypto</a></li>
<li><a class="nav-debugger" href="debugger.html">Debugger</a></li>
<li><a class="nav-dns" href="dns.html">DNS</a></li>
<li><a class="nav-domain" href="domain.html">Domain</a></li>
<li><a class="nav-errors" href="errors.html">Errors</a></li>
<li><a class="nav-events" href="events.html">Events</a></li>
<li><a class="nav-fs" href="fs.html">File System</a></li>
<li><a class="nav-globals" href="globals.html">Globals</a></li>
<li><a class="nav-http" href="http.html">HTTP</a></li>
<li><a class="nav-https" href="https.html">HTTPS</a></li>
<li><a class="nav-intl" href="intl.html">Internationalization</a></li>
<li><a class="nav-modules" href="modules.html">Modules</a></li>
<li><a class="nav-net active" href="net.html">Net</a></li>
<li><a class="nav-os" href="os.html">OS</a></li>
<li><a class="nav-path" href="path.html">Path</a></li>
<li><a class="nav-process" href="process.html">Process</a></li>
<li><a class="nav-punycode" href="punycode.html">Punycode</a></li>
<li><a class="nav-querystring" href="querystring.html">Query Strings</a></li>
<li><a class="nav-readline" href="readline.html">Readline</a></li>
<li><a class="nav-repl" href="repl.html">REPL</a></li>
<li><a class="nav-stream" href="stream.html">Stream</a></li>
<li><a class="nav-string_decoder" href="string_decoder.html">String Decoder</a></li>
<li><a class="nav-timers" href="timers.html">Timers</a></li>
<li><a class="nav-tls" href="tls.html">TLS/SSL</a></li>
<li><a class="nav-tty" href="tty.html">TTY</a></li>
<li><a class="nav-dgram" href="dgram.html">UDP/Datagram</a></li>
<li><a class="nav-url" href="url.html">URL</a></li>
<li><a class="nav-util" href="util.html">Utilities</a></li>
<li><a class="nav-v8" href="v8.html">V8</a></li>
<li><a class="nav-vm" href="vm.html">VM</a></li>
<li><a class="nav-zlib" href="zlib.html">ZLIB</a></li>
</ul>
<div class="line"></div>

<ul>
<li><a class="nav-https-github-com-nodejs-node" href="https://github.com/nodejs/node">GitHub Repo &amp; Issue Tracker</a></li>
<li><a class="nav-http-groups-google-com-group-nodejs" href="http://groups.google.com/group/nodejs">Mailing List</a></li>
</ul>

    </div>

    <div id="column1" data-id="net" class="interior">
      <header>
        <h1>Node.js v6.17.1 Documentation</h1>
        <div id="gtoc">
          <ul>
            <li>
              <a href="index.html" name="toc">Index</a>
            </li>
            <li>
              <a href="all.html">View on single page</a>
            </li>
            <li>
              <a href="net.json">View as JSON</a>
            </li>
            
    <li class="version-picker">
      <a href="#">View another version <span>&#x25bc;</span></a>
      <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v9.x/api/net.html">9.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v8.x/api/net.html">8.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v7.x/api/net.html">7.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v6.x/api/net.html">6.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v5.x/api/net.html">5.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v4.x/api/net.html">4.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/net.html">0.12.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/net.html">0.10.x</a></li></ol>
    </li>
  
          </ul>
        </div>
        <hr>
      </header>

      <div id="toc">
        <h2>Table of Contents</h2>
        <ul>
<li><span class="stability_2"><a href="#net_net">Net</a></span><ul>
<li><span class="stability_undefined"><a href="#net_class_net_server">Class: net.Server</a></span><ul>
<li><span class="stability_undefined"><a href="#net_event_close">Event: &#39;close&#39;</a></span></li>
<li><span class="stability_undefined"><a href="#net_event_connection">Event: &#39;connection&#39;</a></span></li>
<li><span class="stability_undefined"><a href="#net_event_error">Event: &#39;error&#39;</a></span></li>
<li><span class="stability_undefined"><a href="#net_event_listening">Event: &#39;listening&#39;</a></span></li>
<li><span class="stability_undefined"><a href="#net_server_address">server.address()</a></span></li>
<li><span class="stability_undefined"><a href="#net_server_close_callback">server.close([callback])</a></span></li>
<li><span class="stability_0"><a href="#net_server_connections">server.connections</a></span></li>
<li><span class="stability_undefined"><a href="#net_server_getconnections_callback">server.getConnections(callback)</a></span></li>
<li><span class="stability_undefined"><a href="#net_server_listen_handle_backlog_callback">server.listen(handle[, backlog][, callback])</a></span></li>
<li><span class="stability_undefined"><a href="#net_server_listen_options_callback">server.listen(options[, callback])</a></span></li>
<li><span class="stability_undefined"><a href="#net_server_listen_path_backlog_callback">server.listen(path[, backlog][, callback])</a></span></li>
<li><span class="stability_undefined"><a href="#net_server_listen_port_hostname_backlog_callback">server.listen([port][, hostname][, backlog][, callback])</a></span></li>
<li><span class="stability_undefined"><a href="#net_server_listening">server.listening</a></span></li>
<li><span class="stability_undefined"><a href="#net_server_maxconnections">server.maxConnections</a></span></li>
<li><span class="stability_undefined"><a href="#net_server_ref">server.ref()</a></span></li>
<li><span class="stability_undefined"><a href="#net_server_unref">server.unref()</a></span></li>
</ul>
</li>
<li><span class="stability_undefined"><a href="#net_class_net_socket">Class: net.Socket</a></span><ul>
<li><span class="stability_undefined"><a href="#net_new_net_socket_options">new net.Socket([options])</a></span></li>
<li><span class="stability_undefined"><a href="#net_event_close_1">Event: &#39;close&#39;</a></span></li>
<li><span class="stability_undefined"><a href="#net_event_connect">Event: &#39;connect&#39;</a></span></li>
<li><span class="stability_undefined"><a href="#net_event_data">Event: &#39;data&#39;</a></span></li>
<li><span class="stability_undefined"><a href="#net_event_drain">Event: &#39;drain&#39;</a></span></li>
<li><span class="stability_undefined"><a href="#net_event_end">Event: &#39;end&#39;</a></span></li>
<li><span class="stability_undefined"><a href="#net_event_error_1">Event: &#39;error&#39;</a></span></li>
<li><span class="stability_undefined"><a href="#net_event_lookup">Event: &#39;lookup&#39;</a></span></li>
<li><span class="stability_undefined"><a href="#net_event_timeout">Event: &#39;timeout&#39;</a></span></li>
<li><span class="stability_undefined"><a href="#net_socket_address">socket.address()</a></span></li>
<li><span class="stability_undefined"><a href="#net_socket_buffersize">socket.bufferSize</a></span></li>
<li><span class="stability_undefined"><a href="#net_socket_bytesread">socket.bytesRead</a></span></li>
<li><span class="stability_undefined"><a href="#net_socket_byteswritten">socket.bytesWritten</a></span></li>
<li><span class="stability_undefined"><a href="#net_socket_connect_options_connectlistener">socket.connect(options[, connectListener])</a></span></li>
<li><span class="stability_undefined"><a href="#net_socket_connect_path_connectlistener">socket.connect(path[, connectListener])</a></span></li>
<li><span class="stability_undefined"><a href="#net_socket_connect_port_host_connectlistener">socket.connect(port[, host][, connectListener])</a></span></li>
<li><span class="stability_undefined"><a href="#net_socket_connecting">socket.connecting</a></span></li>
<li><span class="stability_undefined"><a href="#net_socket_destroy_exception">socket.destroy([exception])</a></span></li>
<li><span class="stability_undefined"><a href="#net_socket_destroyed">socket.destroyed</a></span></li>
<li><span class="stability_undefined"><a href="#net_socket_end_data_encoding">socket.end([data][, encoding])</a></span></li>
<li><span class="stability_undefined"><a href="#net_socket_localaddress">socket.localAddress</a></span></li>
<li><span class="stability_undefined"><a href="#net_socket_localport">socket.localPort</a></span></li>
<li><span class="stability_undefined"><a href="#net_socket_pause">socket.pause()</a></span></li>
<li><span class="stability_undefined"><a href="#net_socket_ref">socket.ref()</a></span></li>
<li><span class="stability_undefined"><a href="#net_socket_remoteaddress">socket.remoteAddress</a></span></li>
<li><span class="stability_undefined"><a href="#net_socket_remotefamily">socket.remoteFamily</a></span></li>
<li><span class="stability_undefined"><a href="#net_socket_remoteport">socket.remotePort</a></span></li>
<li><span class="stability_undefined"><a href="#net_socket_resume">socket.resume()</a></span></li>
<li><span class="stability_undefined"><a href="#net_socket_setencoding_encoding">socket.setEncoding([encoding])</a></span></li>
<li><span class="stability_undefined"><a href="#net_socket_setkeepalive_enable_initialdelay">socket.setKeepAlive([enable][, initialDelay])</a></span></li>
<li><span class="stability_undefined"><a href="#net_socket_setnodelay_nodelay">socket.setNoDelay([noDelay])</a></span></li>
<li><span class="stability_undefined"><a href="#net_socket_settimeout_timeout_callback">socket.setTimeout(timeout[, callback])</a></span></li>
<li><span class="stability_undefined"><a href="#net_socket_unref">socket.unref()</a></span></li>
<li><span class="stability_undefined"><a href="#net_socket_write_data_encoding_callback">socket.write(data[, encoding][, callback])</a></span></li>
</ul>
</li>
<li><span class="stability_undefined"><a href="#net_net_connect_options_connectlistener">net.connect(options[, connectListener])</a></span></li>
<li><span class="stability_undefined"><a href="#net_net_connect_path_connectlistener">net.connect(path[, connectListener])</a></span></li>
<li><span class="stability_undefined"><a href="#net_net_connect_port_host_connectlistener">net.connect(port[, host][, connectListener])</a></span></li>
<li><span class="stability_undefined"><a href="#net_net_createconnection_options_connectlistener">net.createConnection(options[, connectListener])</a></span></li>
<li><span class="stability_undefined"><a href="#net_net_createconnection_path_connectlistener">net.createConnection(path[, connectListener])</a></span></li>
<li><span class="stability_undefined"><a href="#net_net_createconnection_port_host_connectlistener">net.createConnection(port[, host][, connectListener])</a></span></li>
<li><span class="stability_undefined"><a href="#net_net_createserver_options_connectionlistener">net.createServer([options][, connectionListener])</a></span></li>
<li><span class="stability_undefined"><a href="#net_net_isip_input">net.isIP(input)</a></span></li>
<li><span class="stability_undefined"><a href="#net_net_isipv4_input">net.isIPv4(input)</a></span></li>
<li><span class="stability_undefined"><a href="#net_net_isipv6_input">net.isIPv6(input)</a></span></li>
</ul>
</li>
</ul>

      </div>

      <div id="apicontent">
        <h1>Net<span><a class="mark" href="#net_net"id="net_net">#</a></span></h1>
<!--introduced_in=v0.10.0-->
<div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p>The <code>net</code> module provides you with an asynchronous network wrapper. It contains
functions for creating both servers and clients (called streams). You can include
this module with <code>require(&#39;net&#39;);</code>.</p>
<h2>Class: net.Server<span><a class="mark" href="#net_class_net_server"id="net_class_net_server">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v0.1.90</span>
</div><p>This class is used to create a TCP or local server.</p>
<p><code>net.Server</code> is an <a href="events.html#events_class_eventemitter"><code>EventEmitter</code></a> with the following events:</p>
<h3>Event: &#39;close&#39;<span><a class="mark" href="#net_event_close"id="net_event_close">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.5.0</span>
</div><p>Emitted when the server closes. Note that if connections exist, this
event is not emitted until all connections are ended.</p>
<h3>Event: &#39;connection&#39;<span><a class="mark" href="#net_event_connection"id="net_event_connection">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.90</span>
</div><ul>
<li><a href="net.html#net_class_net_socket" class="type">&lt;net.Socket&gt;</a> The connection object</li>
</ul>
<p>Emitted when a new connection is made. <code>socket</code> is an instance of
<code>net.Socket</code>.</p>
<h3>Event: &#39;error&#39;<span><a class="mark" href="#net_event_error"id="net_event_error">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.90</span>
</div><ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&lt;Error&gt;</a></li>
</ul>
<p>Emitted when an error occurs. Unlike <a href="#net_class_net_socket"><code>net.Socket</code></a>, the <a href="#net_event_close"><code>&#39;close&#39;</code></a>
event will <strong>not</strong> be emitted directly following this event unless
<a href="#net_server_close_callback"><code>server.close()</code></a> is manually called. See the example in discussion of
<a href="#net_server_listen_port_hostname_backlog_callback"><code>server.listen()</code></a>.</p>
<h3>Event: &#39;listening&#39;<span><a class="mark" href="#net_event_listening"id="net_event_listening">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.90</span>
</div><p>Emitted when the server has been bound after calling <code>server.listen</code>.</p>
<h3>server.address()<span><a class="mark" href="#net_server_address"id="net_server_address">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.90</span>
</div><p>Returns the bound address, the address family name, and port of the server
as reported by the operating system if listening on an IP socket.
Useful to find which port was assigned when getting an OS-assigned address.
Returns an object with <code>port</code>, <code>family</code>, and <code>address</code> properties:
<code>{ port: 12346, family: &#39;IPv4&#39;, address: &#39;127.0.0.1&#39; }</code></p>
<p>For a server listening on a pipe or UNIX domain socket, the name is returned
as a string.</p>
<p>Example:</p>
<pre><code class="lang-js">const server = net.createServer((socket) =&gt; {
  socket.end(&#39;goodbye\n&#39;);
}).on(&#39;error&#39;, (err) =&gt; {
  // handle errors here
  throw err;
});

// grab a random port.
server.listen(() =&gt; {
  console.log(&#39;opened server on&#39;, server.address());
});
</code></pre>
<p>Don&#39;t call <code>server.address()</code> until the <code>&#39;listening&#39;</code> event has been emitted.</p>
<h3>server.close([callback])<span><a class="mark" href="#net_server_close_callback"id="net_server_close_callback">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.90</span>
</div><ul>
<li>Returns: <a href="net.html#net_class_net_server" class="type">&lt;net.Server&gt;</a></li>
</ul>
<p>Stops the server from accepting new connections and keeps existing
connections. This function is asynchronous, the server is finally
closed when all connections are ended and the server emits a <a href="#net_event_close"><code>&#39;close&#39;</code></a> event.
The optional <code>callback</code> will be called once the <code>&#39;close&#39;</code> event occurs. Unlike
that event, it will be called with an Error as its only argument if the server
was not open when it was closed.</p>
<p>Returns <code>server</code>.</p>
<h3>server.connections<span><a class="mark" href="#net_server_connections"id="net_server_connections">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.2.0</span>
<span>Deprecated since: v0.9.7 </span>
</div><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="#net_server_getconnections_callback"><code>server.getConnections()</code></a> instead.</div><p>The number of concurrent connections on the server.</p>
<p>This becomes <code>null</code> when sending a socket to a child with
<a href="child_process.html#child_process_child_process_fork_modulepath_args_options"><code>child_process.fork()</code></a>. To poll forks and get current number of active
connections use asynchronous <code>server.getConnections</code> instead.</p>
<h3>server.getConnections(callback)<span><a class="mark" href="#net_server_getconnections_callback"id="net_server_getconnections_callback">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.9.7</span>
</div><ul>
<li>Returns: <a href="net.html#net_class_net_server" class="type">&lt;net.Server&gt;</a></li>
</ul>
<p>Asynchronously get the number of concurrent connections on the server. Works
when sockets were sent to forks.</p>
<p>Callback should take two arguments <code>err</code> and <code>count</code>.</p>
<h3>server.listen(handle[, backlog][, callback])<span><a class="mark" href="#net_server_listen_handle_backlog_callback"id="net_server_listen_handle_backlog_callback">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.5.10</span>
</div><ul>
<li><code>handle</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a></li>
<li><code>backlog</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a></li>
<li>Returns: <a href="net.html#net_class_net_server" class="type">&lt;net.Server&gt;</a></li>
</ul>
<p>The <code>handle</code> object can be set to either a server or socket (anything
with an underlying <code>_handle</code> member), or a <code>{fd: &lt;n&gt;}</code> object.</p>
<p>This will cause the server to accept connections on the specified
handle, but it is presumed that the file descriptor or handle has
already been bound to a port or domain socket.</p>
<p>Listening on a file descriptor is not supported on Windows.</p>
<p>This function is asynchronous. When the server has been bound,
<a href="#net_event_listening"><code>&#39;listening&#39;</code></a> event will be emitted.
The last parameter <code>callback</code> will be added as a listener for the
<a href="#net_event_listening"><code>&#39;listening&#39;</code></a> event.</p>
<p>The parameter <code>backlog</code> behaves the same as in
<a href="#net_server_listen_port_hostname_backlog_callback"><code>server.listen([port][, hostname][, backlog][, callback])</code></a>.</p>
<h3>server.listen(options[, callback])<span><a class="mark" href="#net_server_listen_options_callback"id="net_server_listen_options_callback">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.11.14</span>
</div><ul>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> - Required. Supports the following properties:<ul>
<li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> - Optional.</li>
<li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> - Optional.</li>
<li><code>backlog</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> - Optional.</li>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> - Optional.</li>
<li><code>exclusive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a> - Optional.</li>
</ul>
</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a> - Optional.</li>
<li>Returns: <a href="net.html#net_class_net_server" class="type">&lt;net.Server&gt;</a></li>
</ul>
<p>The <code>port</code>, <code>host</code>, and <code>backlog</code> properties of <code>options</code>, as well as the
optional callback function, behave as they do on a call to
<a href="#net_server_listen_port_hostname_backlog_callback"><code>server.listen([port][, hostname][, backlog][, callback])</code></a>.
Alternatively, the <code>path</code> option can be used to specify a UNIX socket.</p>
<p>If <code>exclusive</code> is <code>false</code> (default), then cluster workers will use the same
underlying handle, allowing connection handling duties to be shared. When
<code>exclusive</code> is <code>true</code>, the handle is not shared, and attempted port sharing
results in an error. An example which listens on an exclusive port is
shown below.</p>
<pre><code class="lang-js">server.listen({
  host: &#39;localhost&#39;,
  port: 80,
  exclusive: true
});
</code></pre>
<p><em>Note</em>: The <code>server.listen()</code> method may be called multiple times. Each
subsequent call will <em>re-open</em> the server using the provided options.</p>
<h3>server.listen(path[, backlog][, callback])<span><a class="mark" href="#net_server_listen_path_backlog_callback"id="net_server_listen_path_backlog_callback">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.90</span>
</div><ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
<li><code>backlog</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a></li>
<li>Returns: <a href="net.html#net_class_net_server" class="type">&lt;net.Server&gt;</a></li>
</ul>
<p>Start a local socket server listening for connections on the given <code>path</code>.</p>
<p>This function is asynchronous. When the server has been bound,
<a href="#net_event_listening"><code>&#39;listening&#39;</code></a> event will be emitted. The last parameter <code>callback</code>
will be added as a listener for the <a href="#net_event_listening"><code>&#39;listening&#39;</code></a> event.</p>
<p>On UNIX, the local domain is usually known as the UNIX domain. The path is a
filesystem path name. It gets truncated to <code>sizeof(sockaddr_un.sun_path)</code>
bytes, decreased by 1. It varies on different operating system between 91 and
107 bytes. The typical values are 107 on Linux and 103 on OS X. The path is
subject to the same naming conventions and permissions checks as would be done
on file creation, will be visible in the filesystem, and will <em>persist until
unlinked</em>.</p>
<p>On Windows, the local domain is implemented using a named pipe. The path <em>must</em>
refer to an entry in <code>\\?\pipe\</code> or <code>\\.\pipe\</code>. Any characters are permitted,
but the latter may do some processing of pipe names, such as resolving <code>..</code>
sequences. Despite appearances, the pipe name space is flat. Pipes will <em>not
persist</em>, they are removed when the last reference to them is closed. Do not
forget JavaScript string escaping requires paths to be specified with
double-backslashes, such as:</p>
<pre><code class="lang-js">net.createServer().listen(
  path.join(&#39;\\\\?\\pipe&#39;, process.cwd(), &#39;myctl&#39;));
</code></pre>
<p>The parameter <code>backlog</code> behaves the same as in
<a href="#net_server_listen_port_hostname_backlog_callback"><code>server.listen([port][, hostname][, backlog][, callback])</code></a>.</p>
<p><em>Note</em>: The <code>server.listen()</code> method may be called multiple times. Each
subsequent call will <em>re-open</em> the server using the provided options.</p>
<h3>server.listen([port][, hostname][, backlog][, callback])<span><a class="mark" href="#net_server_listen_port_hostname_backlog_callback"id="net_server_listen_port_hostname_backlog_callback">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.90</span>
</div><ul>
<li>Returns: <a href="net.html#net_class_net_server" class="type">&lt;net.Server&gt;</a></li>
</ul>
<p>Begin accepting connections on the specified <code>port</code> and <code>hostname</code>. If the
<code>hostname</code> is omitted, the server will accept connections on any IPv6 address
(<code>::</code>) when IPv6 is available, or any IPv4 address (<code>0.0.0.0</code>) otherwise.
Omit the port argument, or use a port value of <code>0</code>, to have the operating system
assign a random port, which can be retrieved by using <code>server.address().port</code>
after the <code>&#39;listening&#39;</code> event has been emitted.</p>
<p>Backlog is the maximum length of the queue of pending connections.
The actual length will be determined by the OS through sysctl settings such as
<code>tcp_max_syn_backlog</code> and <code>somaxconn</code> on Linux. The default value of this
parameter is 511 (not 512).</p>
<p>This function is asynchronous. When the server has been bound,
<a href="#net_event_listening"><code>&#39;listening&#39;</code></a> event will be emitted. The last parameter <code>callback</code>
will be added as a listener for the <a href="#net_event_listening"><code>&#39;listening&#39;</code></a> event.</p>
<p>One issue some users run into is getting <code>EADDRINUSE</code> errors. This means that
another server is already running on the requested port. One way of handling this
would be to wait a second and then try again:</p>
<pre><code class="lang-js">server.on(&#39;error&#39;, (e) =&gt; {
  if (e.code == &#39;EADDRINUSE&#39;) {
    console.log(&#39;Address in use, retrying...&#39;);
    setTimeout(() =&gt; {
      server.close();
      server.listen(PORT, HOST);
    }, 1000);
  }
});
</code></pre>
<p>(Note: All sockets in Node.js are set <code>SO_REUSEADDR</code>.)</p>
<p><em>Note</em>: The <code>server.listen()</code> method may be called multiple times. Each
subsequent call will <em>re-open</em> the server using the provided options.</p>
<h3>server.listening<span><a class="mark" href="#net_server_listening"id="net_server_listening">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v5.7.0</span>
</div><p>A Boolean indicating whether or not the server is listening for
connections.</p>
<h3>server.maxConnections<span><a class="mark" href="#net_server_maxconnections"id="net_server_maxconnections">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.2.0</span>
</div><p>Set this property to reject connections when the server&#39;s connection count gets
high.</p>
<p>It is not recommended to use this option once a socket has been sent to a child
with <a href="child_process.html#child_process_child_process_fork_modulepath_args_options"><code>child_process.fork()</code></a>.</p>
<h3>server.ref()<span><a class="mark" href="#net_server_ref"id="net_server_ref">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.9.1</span>
</div><ul>
<li>Returns: <a href="net.html#net_class_net_server" class="type">&lt;net.Server&gt;</a></li>
</ul>
<p>Opposite of <code>unref</code>, calling <code>ref</code> on a previously <code>unref</code>d server will <em>not</em>
let the program exit if it&#39;s the only server left (the default behavior). If
the server is <code>ref</code>d calling <code>ref</code> again will have no effect.</p>
<h3>server.unref()<span><a class="mark" href="#net_server_unref"id="net_server_unref">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.9.1</span>
</div><ul>
<li>Returns: <a href="net.html#net_class_net_server" class="type">&lt;net.Server&gt;</a></li>
</ul>
<p>Calling <code>unref</code> on a server will allow the program to exit if this is the only
active server in the event system. If the server is already <code>unref</code>d calling
<code>unref</code> again will have no effect.</p>
<h2>Class: net.Socket<span><a class="mark" href="#net_class_net_socket"id="net_class_net_socket">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v0.3.4</span>
</div><p>This object is an abstraction of a TCP or local socket. <code>net.Socket</code>
instances implement a duplex Stream interface. They can be created by the
user and used as a client (with <a href="#net_socket_connect_options_connectlistener"><code>connect()</code></a>) or they can be created by Node.js
and passed to the user through the <code>&#39;connection&#39;</code> event of a server.</p>
<h3>new net.Socket([options])<span><a class="mark" href="#net_new_net_socket_options"id="net_new_net_socket_options">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.3.4</span>
</div><p>Construct a new socket object.</p>
<p><code>options</code> is an object with the following defaults:</p>
<!-- eslint-skip -->
<pre><code class="lang-js">{
  fd: null,
  allowHalfOpen: false,
  readable: false,
  writable: false
}
</code></pre>
<p><code>fd</code> allows you to specify the existing file descriptor of socket.
Set <code>readable</code> and/or <code>writable</code> to <code>true</code> to allow reads and/or writes on this
socket (NOTE: Works only when <code>fd</code> is passed).
About <code>allowHalfOpen</code>, refer to <a href="#net_net_createserver_options_connectionlistener"><code>net.createServer()</code></a> and <a href="#net_event_end"><code>&#39;end&#39;</code></a> event.</p>
<p><code>net.Socket</code> instances are <a href="events.html#events_class_eventemitter"><code>EventEmitter</code></a> with the following events:</p>
<h3>Event: &#39;close&#39;<span><a class="mark" href="#net_event_close_1"id="net_event_close_1">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.90</span>
</div><ul>
<li><code>had_error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a> <code>true</code> if the socket had a transmission error.</li>
</ul>
<p>Emitted once the socket is fully closed. The argument <code>had_error</code> is a boolean
which says if the socket was closed due to a transmission error.</p>
<h3>Event: &#39;connect&#39;<span><a class="mark" href="#net_event_connect"id="net_event_connect">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.90</span>
</div><p>Emitted when a socket connection is successfully established.
See <a href="#net_socket_connect_options_connectlistener"><code>connect()</code></a>.</p>
<h3>Event: &#39;data&#39;<span><a class="mark" href="#net_event_data"id="net_event_data">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.90</span>
</div><ul>
<li><a href="buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a></li>
</ul>
<p>Emitted when data is received. The argument <code>data</code> will be a <code>Buffer</code> or
<code>String</code>. Encoding of data is set by <code>socket.setEncoding()</code>.
(See the <a href="stream.html#stream_class_stream_readable">Readable Stream</a> section for more information.)</p>
<p>Note that the <strong>data will be lost</strong> if there is no listener when a <code>Socket</code>
emits a <code>&#39;data&#39;</code> event.</p>
<h3>Event: &#39;drain&#39;<span><a class="mark" href="#net_event_drain"id="net_event_drain">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.90</span>
</div><p>Emitted when the write buffer becomes empty. Can be used to throttle uploads.</p>
<p>See also: the return values of <code>socket.write()</code></p>
<h3>Event: &#39;end&#39;<span><a class="mark" href="#net_event_end"id="net_event_end">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.90</span>
</div><p>Emitted when the other end of the socket sends a FIN packet.</p>
<p>By default (<code>allowHalfOpen == false</code>) the socket will destroy its file
descriptor once it has written out its pending write queue. However, by
setting <code>allowHalfOpen == true</code> the socket will not automatically <code>end()</code>
its side allowing the user to write arbitrary amounts of data, with the
caveat that the user is required to <code>end()</code> their side now.</p>
<h3>Event: &#39;error&#39;<span><a class="mark" href="#net_event_error_1"id="net_event_error_1">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.90</span>
</div><ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&lt;Error&gt;</a></li>
</ul>
<p>Emitted when an error occurs. The <code>&#39;close&#39;</code> event will be called directly
following this event.</p>
<h3>Event: &#39;lookup&#39;<span><a class="mark" href="#net_event_lookup"id="net_event_lookup">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.11.3</span>
</div><p>Emitted after resolving the hostname but before connecting.
Not applicable to UNIX sockets.</p>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&lt;Error&gt;</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&lt;null&gt;</a> The error object. See <a href="dns.html#dns_dns_lookup_hostname_options_callback"><code>dns.lookup()</code></a>.</li>
<li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> The IP address.</li>
<li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&lt;null&gt;</a> The address type. See <a href="dns.html#dns_dns_lookup_hostname_options_callback"><code>dns.lookup()</code></a>.</li>
<li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> The hostname.</li>
</ul>
<h3>Event: &#39;timeout&#39;<span><a class="mark" href="#net_event_timeout"id="net_event_timeout">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.90</span>
</div><p>Emitted if the socket times out from inactivity. This is only to notify that
the socket has been idle. The user must manually close the connection.</p>
<p>See also: <a href="#net_socket_settimeout_timeout_callback"><code>socket.setTimeout()</code></a></p>
<h3>socket.address()<span><a class="mark" href="#net_socket_address"id="net_socket_address">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.90</span>
</div><p>Returns the bound address, the address family name and port of the
socket as reported by the operating system. Returns an object with
three properties, e.g.
<code>{ port: 12346, family: &#39;IPv4&#39;, address: &#39;127.0.0.1&#39; }</code></p>
<h3>socket.bufferSize<span><a class="mark" href="#net_socket_buffersize"id="net_socket_buffersize">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.3.8</span>
</div><p><code>net.Socket</code> has the property that <code>socket.write()</code> always works. This is to
help users get up and running quickly. The computer cannot always keep up
with the amount of data that is written to a socket - the network connection
simply might be too slow. Node.js will internally queue up the data written to a
socket and send it out over the wire when it is possible. (Internally it is
polling on the socket&#39;s file descriptor for being writable).</p>
<p>The consequence of this internal buffering is that memory may grow. This
property shows the number of characters currently buffered to be written.
(Number of characters is approximately equal to the number of bytes to be
written, but the buffer may contain strings, and the strings are lazily
encoded, so the exact number of bytes is not known.)</p>
<p>Users who experience large or growing <code>bufferSize</code> should attempt to
&quot;throttle&quot; the data flows in their program with <a href="#net_socket_pause"><code>pause()</code></a> and <a href="#net_socket_resume"><code>resume()</code></a>.</p>
<h3>socket.bytesRead<span><a class="mark" href="#net_socket_bytesread"id="net_socket_bytesread">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.5.3</span>
</div><p>The amount of received bytes.</p>
<h3>socket.bytesWritten<span><a class="mark" href="#net_socket_byteswritten"id="net_socket_byteswritten">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.5.3</span>
</div><p>The amount of bytes sent.</p>
<h3>socket.connect(options[, connectListener])<span><a class="mark" href="#net_socket_connect_options_connectlistener"id="net_socket_connect_options_connectlistener">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.90</span>
</div><p>Opens the connection for a given socket.</p>
<p>For TCP sockets, <code>options</code> argument should be an object which specifies:</p>
<ul>
<li><p><code>port</code>: Port the client should connect to (Required).</p>
</li>
<li><p><code>host</code>: Host the client should connect to. Defaults to <code>&#39;localhost&#39;</code>.</p>
</li>
<li><p><code>localAddress</code>: Local interface to bind to for network connections.</p>
</li>
<li><p><code>localPort</code>: Local port to bind to for network connections.</p>
</li>
<li><p><code>family</code> : Version of IP stack. Defaults to <code>4</code>.</p>
</li>
<li><p><code>hints</code>: <a href="dns.html#dns_supported_getaddrinfo_flags"><code>dns.lookup()</code> hints</a>. Defaults to <code>0</code>.</p>
</li>
<li><p><code>lookup</code> : Custom lookup function. Defaults to <code>dns.lookup</code>.</p>
</li>
</ul>
<p>For local domain sockets, <code>options</code> argument should be an object which
specifies:</p>
<ul>
<li><code>path</code>: Path the client should connect to (Required).</li>
</ul>
<p>For either case:</p>
<ul>
<li>Returns: <a href="net.html#net_class_net_socket" class="type">&lt;net.Socket&gt;</a> The socket itself.</li>
</ul>
<p>Normally this method is not needed, as <code>net.createConnection</code> opens the
socket. Use this only if you are implementing a custom Socket.</p>
<p>This function is asynchronous. When the <a href="#net_event_connect"><code>&#39;connect&#39;</code></a> event is emitted the
socket is established. If there is a problem connecting, the <code>&#39;connect&#39;</code> event
will not be emitted, the <a href="#net_event_error_1"><code>&#39;error&#39;</code></a> event will be emitted with the exception.</p>
<p>The <code>connectListener</code> parameter will be added as a listener for the
<a href="#net_event_connect"><code>&#39;connect&#39;</code></a> event.</p>
<h3>socket.connect(path[, connectListener])<span><a class="mark" href="#net_socket_connect_path_connectlistener"id="net_socket_connect_path_connectlistener">#</a></span></h3>
<h3>socket.connect(port[, host][, connectListener])<span><a class="mark" href="#net_socket_connect_port_host_connectlistener"id="net_socket_connect_port_host_connectlistener">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.90</span>
</div><p>As <a href="#net_socket_connect_options_connectlistener"><code>socket.connect(options[, connectListener])</code></a>,
with options as either <code>{port: port, host: host}</code> or <code>{path: path}</code>.</p>
<ul>
<li>Returns: <a href="net.html#net_class_net_socket" class="type">&lt;net.Socket&gt;</a> The socket itself.</li>
</ul>
<h3>socket.connecting<span><a class="mark" href="#net_socket_connecting"id="net_socket_connecting">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v6.1.0</span>
</div><p>If <code>true</code> - <a href="#net_socket_connect_options_connectlistener"><code>socket.connect(options[, connectListener])</code></a> was called and
haven&#39;t yet finished. Will be set to <code>false</code> before emitting <code>connect</code> event
and/or calling <a href="#net_socket_connect_options_connectlistener"><code>socket.connect(options[, connectListener])</code></a>&#39;s callback.</p>
<h3>socket.destroy([exception])<span><a class="mark" href="#net_socket_destroy_exception"id="net_socket_destroy_exception">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.90</span>
</div><p>Ensures that no more I/O activity happens on this socket. Only necessary in
case of errors (parse error or so).</p>
<p>If <code>exception</code> is specified, an <a href="#net_event_error_1"><code>&#39;error&#39;</code></a> event will be emitted and any
listeners for that event will receive <code>exception</code> as an argument.</p>
<h3>socket.destroyed<span><a class="mark" href="#net_socket_destroyed"id="net_socket_destroyed">#</a></span></h3>
<p>A Boolean value that indicates if the connection is destroyed or not. Once a
connection is destroyed no further data can be transferred using it.</p>
<h3>socket.end([data][, encoding])<span><a class="mark" href="#net_socket_end_data_encoding"id="net_socket_end_data_encoding">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.90</span>
</div><ul>
<li>Returns: <a href="net.html#net_class_net_socket" class="type">&lt;net.Socket&gt;</a> The socket itself.</li>
</ul>
<p>Half-closes the socket. i.e., it sends a FIN packet. It is possible the
server will still send some data.</p>
<p>If <code>data</code> is specified, it is equivalent to calling
<code>socket.write(data, encoding)</code> followed by <code>socket.end()</code>.</p>
<p>Returns <code>socket</code>.</p>
<h3>socket.localAddress<span><a class="mark" href="#net_socket_localaddress"id="net_socket_localaddress">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.9.6</span>
</div><p>The string representation of the local IP address the remote client is
connecting on. For example, if you are listening on <code>&#39;0.0.0.0&#39;</code> and the
client connects on <code>&#39;192.168.1.1&#39;</code>, the value would be <code>&#39;192.168.1.1&#39;</code>.</p>
<h3>socket.localPort<span><a class="mark" href="#net_socket_localport"id="net_socket_localport">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.9.6</span>
</div><p>The numeric representation of the local port. For example,
<code>80</code> or <code>21</code>.</p>
<h3>socket.pause()<span><a class="mark" href="#net_socket_pause"id="net_socket_pause">#</a></span></h3>
<div class="signature"><ul>
<li>Returns: <a href="net.html#net_class_net_socket" class="type">&lt;net.Socket&gt;</a> The socket itself.</li>
</ul>
</div><p>Pauses the reading of data. That is, <a href="#net_event_data"><code>&#39;data&#39;</code></a> events will not be emitted.
Useful to throttle back an upload.</p>
<h3>socket.ref()<span><a class="mark" href="#net_socket_ref"id="net_socket_ref">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.9.1</span>
</div><ul>
<li>Returns: <a href="net.html#net_class_net_socket" class="type">&lt;net.Socket&gt;</a> The socket itself.</li>
</ul>
<p>Opposite of <code>unref</code>, calling <code>ref</code> on a previously <code>unref</code>d socket will <em>not</em>
let the program exit if it&#39;s the only socket left (the default behavior). If
the socket is <code>ref</code>d calling <code>ref</code> again will have no effect.</p>
<h3>socket.remoteAddress<span><a class="mark" href="#net_socket_remoteaddress"id="net_socket_remoteaddress">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.5.10</span>
</div><p>The string representation of the remote IP address. For example,
<code>&#39;74.125.127.100&#39;</code> or <code>&#39;2001:4860:a005::68&#39;</code>. Value may be <code>undefined</code> if
the socket is destroyed (for example, if the client disconnected).</p>
<h3>socket.remoteFamily<span><a class="mark" href="#net_socket_remotefamily"id="net_socket_remotefamily">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.11.14</span>
</div><p>The string representation of the remote IP family. <code>&#39;IPv4&#39;</code> or <code>&#39;IPv6&#39;</code>.</p>
<h3>socket.remotePort<span><a class="mark" href="#net_socket_remoteport"id="net_socket_remoteport">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.5.10</span>
</div><p>The numeric representation of the remote port. For example,
<code>80</code> or <code>21</code>.</p>
<h3>socket.resume()<span><a class="mark" href="#net_socket_resume"id="net_socket_resume">#</a></span></h3>
<div class="signature"><ul>
<li>Returns: <a href="net.html#net_class_net_socket" class="type">&lt;net.Socket&gt;</a> The socket itself.</li>
</ul>
</div><p>Resumes reading after a call to <a href="#net_socket_pause"><code>pause()</code></a>.</p>
<h3>socket.setEncoding([encoding])<span><a class="mark" href="#net_socket_setencoding_encoding"id="net_socket_setencoding_encoding">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.90</span>
</div><ul>
<li>Returns: <a href="net.html#net_class_net_socket" class="type">&lt;net.Socket&gt;</a> The socket itself.</li>
</ul>
<p>Set the encoding for the socket as a <a href="stream.html#stream_class_stream_readable">Readable Stream</a>. See
<a href="stream.html#stream_readable_setencoding_encoding"><code>stream.setEncoding()</code></a> for more information.</p>
<h3>socket.setKeepAlive([enable][, initialDelay])<span><a class="mark" href="#net_socket_setkeepalive_enable_initialdelay"id="net_socket_setkeepalive_enable_initialdelay">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.92</span>
</div><ul>
<li>Returns: <a href="net.html#net_class_net_socket" class="type">&lt;net.Socket&gt;</a> The socket itself.</li>
</ul>
<p>Enable/disable keep-alive functionality, and optionally set the initial
delay before the first keepalive probe is sent on an idle socket.
<code>enable</code> defaults to <code>false</code>.</p>
<p>Set <code>initialDelay</code> (in milliseconds) to set the delay between the last
data packet received and the first keepalive probe. Setting 0 for
initialDelay will leave the value unchanged from the default
(or previous) setting. Defaults to <code>0</code>.</p>
<h3>socket.setNoDelay([noDelay])<span><a class="mark" href="#net_socket_setnodelay_nodelay"id="net_socket_setnodelay_nodelay">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.90</span>
</div><ul>
<li>Returns: <a href="net.html#net_class_net_socket" class="type">&lt;net.Socket&gt;</a> The socket itself.</li>
</ul>
<p>Disables the Nagle algorithm. By default TCP connections use the Nagle
algorithm, they buffer data before sending it off. Setting <code>true</code> for
<code>noDelay</code> will immediately fire off data each time <code>socket.write()</code> is called.
<code>noDelay</code> defaults to <code>true</code>.</p>
<h3>socket.setTimeout(timeout[, callback])<span><a class="mark" href="#net_socket_settimeout_timeout_callback"id="net_socket_settimeout_timeout_callback">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.90</span>
</div><ul>
<li>Returns: <a href="net.html#net_class_net_socket" class="type">&lt;net.Socket&gt;</a> The socket itself.</li>
</ul>
<p>Sets the socket to timeout after <code>timeout</code> milliseconds of inactivity on
the socket. By default <code>net.Socket</code> do not have a timeout.</p>
<p>When an idle timeout is triggered the socket will receive a <a href="#net_event_timeout"><code>&#39;timeout&#39;</code></a>
event but the connection will not be severed. The user must manually <a href="#net_socket_end_data_encoding"><code>end()</code></a>
or <a href="#net_socket_destroy_exception"><code>destroy()</code></a> the socket.</p>
<p>If <code>timeout</code> is 0, then the existing idle timeout is disabled.</p>
<p>The optional <code>callback</code> parameter will be added as a one-time listener for the
<a href="#net_event_timeout"><code>&#39;timeout&#39;</code></a> event.</p>
<h3>socket.unref()<span><a class="mark" href="#net_socket_unref"id="net_socket_unref">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.9.1</span>
</div><ul>
<li>Returns: <a href="net.html#net_class_net_socket" class="type">&lt;net.Socket&gt;</a> The socket itself.</li>
</ul>
<p>Calling <code>unref</code> on a socket will allow the program to exit if this is the only
active socket in the event system. If the socket is already <code>unref</code>d calling
<code>unref</code> again will have no effect.</p>
<h3>socket.write(data[, encoding][, callback])<span><a class="mark" href="#net_socket_write_data_encoding_callback"id="net_socket_write_data_encoding_callback">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.90</span>
</div><p>Sends data on the socket. The second parameter specifies the encoding in the
case of a string — it defaults to UTF8 encoding.</p>
<p>Returns <code>true</code> if the entire data was flushed successfully to the kernel
buffer. Returns <code>false</code> if all or part of the data was queued in user memory.
<a href="#net_event_drain"><code>&#39;drain&#39;</code></a> will be emitted when the buffer is again free.</p>
<p>The optional <code>callback</code> parameter will be executed when the data is finally
written out - this may not be immediately.</p>
<h2>net.connect(options[, connectListener])<span><a class="mark" href="#net_net_connect_options_connectlistener"id="net_net_connect_options_connectlistener">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v0.7.0</span>
</div><p>A factory function, which returns a new <a href="#net_class_net_socket"><code>net.Socket</code></a> and automatically
connects with the supplied <code>options</code>.</p>
<p>The options are passed to both the <a href="#net_class_net_socket"><code>net.Socket</code></a> constructor and the
<a href="#net_socket_connect_options_connectlistener"><code>socket.connect</code></a> method.</p>
<p>The <code>connectListener</code> parameter will be added as a listener for the
<a href="#net_event_connect"><code>&#39;connect&#39;</code></a> event once.</p>
<p>Here is an example of a client of the previously described echo server:</p>
<pre><code class="lang-js">const net = require(&#39;net&#39;);
const client = net.connect({port: 8124}, () =&gt; {
  // &#39;connect&#39; listener
  console.log(&#39;connected to server!&#39;);
  client.write(&#39;world!\r\n&#39;);
});
client.on(&#39;data&#39;, (data) =&gt; {
  console.log(data.toString());
  client.end();
});
client.on(&#39;end&#39;, () =&gt; {
  console.log(&#39;disconnected from server&#39;);
});
</code></pre>
<p>To connect on the socket <code>/tmp/echo.sock</code> the second line would just be
changed to</p>
<pre><code class="lang-js">const client = net.connect({path: &#39;/tmp/echo.sock&#39;});
</code></pre>
<h2>net.connect(path[, connectListener])<span><a class="mark" href="#net_net_connect_path_connectlistener"id="net_net_connect_path_connectlistener">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v0.1.90</span>
</div><p>A factory function, which returns a new unix <a href="#net_class_net_socket"><code>net.Socket</code></a> and automatically
connects to the supplied <code>path</code>.</p>
<p>The <code>connectListener</code> parameter will be added as a listener for the
<a href="#net_event_connect"><code>&#39;connect&#39;</code></a> event once.</p>
<h2>net.connect(port[, host][, connectListener])<span><a class="mark" href="#net_net_connect_port_host_connectlistener"id="net_net_connect_port_host_connectlistener">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v0.1.90</span>
</div><p>A factory function, which returns a new <a href="#net_class_net_socket"><code>net.Socket</code></a> and automatically
connects to the supplied <code>port</code> and <code>host</code>.</p>
<p>If <code>host</code> is omitted, <code>&#39;localhost&#39;</code> will be assumed.</p>
<p>The <code>connectListener</code> parameter will be added as a listener for the
<a href="#net_event_connect"><code>&#39;connect&#39;</code></a> event once.</p>
<h2>net.createConnection(options[, connectListener])<span><a class="mark" href="#net_net_createconnection_options_connectlistener"id="net_net_createconnection_options_connectlistener">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v0.1.90</span>
</div><p>A factory function, which returns a new <a href="#net_class_net_socket"><code>net.Socket</code></a> and automatically
connects with the supplied <code>options</code>.</p>
<p>The options are passed to both the <a href="#net_class_net_socket"><code>net.Socket</code></a> constructor and the
<a href="#net_socket_connect_options_connectlistener"><code>socket.connect</code></a> method.</p>
<p>Passing <code>timeout</code> as an option will call <a href="#net_socket_settimeout_timeout_callback"><code>socket.setTimeout()</code></a> after the socket is created, but before it is connecting.</p>
<p>The <code>connectListener</code> parameter will be added as a listener for the
<a href="#net_event_connect"><code>&#39;connect&#39;</code></a> event once.</p>
<p>Following is an example of a client of the echo server described
in the <a href="#net_net_createserver_options_connectionlistener"><code>net.createServer()</code></a> section:</p>
<pre><code class="lang-js">const net = require(&#39;net&#39;);
const client = net.createConnection({port: 8124}, () =&gt; {
  //&#39;connect&#39; listener
  console.log(&#39;connected to server!&#39;);
  client.write(&#39;world!\r\n&#39;);
});
client.on(&#39;data&#39;, (data) =&gt; {
  console.log(data.toString());
  client.end();
});
client.on(&#39;end&#39;, () =&gt; {
  console.log(&#39;disconnected from server&#39;);
});
</code></pre>
<p>To connect on the socket <code>/tmp/echo.sock</code> the second line would just be
changed to</p>
<pre><code class="lang-js">const client = net.connect({path: &#39;/tmp/echo.sock&#39;});
</code></pre>
<h2>net.createConnection(path[, connectListener])<span><a class="mark" href="#net_net_createconnection_path_connectlistener"id="net_net_createconnection_path_connectlistener">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v0.1.90</span>
</div><p>A factory function, which returns a new unix <a href="#net_class_net_socket"><code>net.Socket</code></a> and automatically
connects to the supplied <code>path</code>.</p>
<p>The <code>connectListener</code> parameter will be added as a listener for the
<a href="#net_event_connect"><code>&#39;connect&#39;</code></a> event once.</p>
<h2>net.createConnection(port[, host][, connectListener])<span><a class="mark" href="#net_net_createconnection_port_host_connectlistener"id="net_net_createconnection_port_host_connectlistener">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v0.1.90</span>
</div><p>A factory function, which returns a new <a href="#net_class_net_socket"><code>net.Socket</code></a> and automatically
connects to the supplied <code>port</code> and <code>host</code>.</p>
<p>If <code>host</code> is omitted, <code>&#39;localhost&#39;</code> will be assumed.</p>
<p>The <code>connectListener</code> parameter will be added as a listener for the
<a href="#net_event_connect"><code>&#39;connect&#39;</code></a> event once.</p>
<h2>net.createServer([options][, connectionListener])<span><a class="mark" href="#net_net_createserver_options_connectionlistener"id="net_net_createserver_options_connectionlistener">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v0.5.0</span>
</div><p>Creates a new server. The <code>connectionListener</code> argument is
automatically set as a listener for the <a href="#net_event_connection"><code>&#39;connection&#39;</code></a> event.</p>
<p><code>options</code> is an object with the following defaults:</p>
<!-- eslint-skip -->
<pre><code class="lang-js">{
  allowHalfOpen: false,
  pauseOnConnect: false
}
</code></pre>
<ul>
<li>Returns: <a href="net.html#net_class_net_server" class="type">&lt;net.Server&gt;</a></li>
</ul>
<p>If <code>allowHalfOpen</code> is <code>true</code>, then the socket won&#39;t automatically send a FIN
packet when the other end of the socket sends a FIN packet. The socket becomes
non-readable, but still writable. You should call the <a href="#net_socket_end_data_encoding"><code>end()</code></a> method explicitly.
See <a href="#net_event_end"><code>&#39;end&#39;</code></a> event for more information.</p>
<p>If <code>pauseOnConnect</code> is <code>true</code>, then the socket associated with each incoming
connection will be paused, and no data will be read from its handle. This allows
connections to be passed between processes without any data being read by the
original process. To begin reading data from a paused socket, call <a href="#net_socket_resume"><code>resume()</code></a>.</p>
<p>Here is an example of an echo server which listens for connections
on port 8124:</p>
<pre><code class="lang-js">const net = require(&#39;net&#39;);
const server = net.createServer((c) =&gt; {
  // &#39;connection&#39; listener
  console.log(&#39;client connected&#39;);
  c.on(&#39;end&#39;, () =&gt; {
    console.log(&#39;client disconnected&#39;);
  });
  c.write(&#39;hello\r\n&#39;);
  c.pipe(c);
});
server.on(&#39;error&#39;, (err) =&gt; {
  throw err;
});
server.listen(8124, () =&gt; {
  console.log(&#39;server bound&#39;);
});
</code></pre>
<p>Test this by using <code>telnet</code>:</p>
<pre><code class="lang-console">$ telnet localhost 8124
</code></pre>
<p>To listen on the socket <code>/tmp/echo.sock</code> the third line from the last would
just be changed to</p>
<pre><code class="lang-js">server.listen(&#39;/tmp/echo.sock&#39;, () =&gt; {
  console.log(&#39;server bound&#39;);
});
</code></pre>
<p>Use <code>nc</code> to connect to a UNIX domain socket server:</p>
<pre><code class="lang-console">$ nc -U /tmp/echo.sock
</code></pre>
<h2>net.isIP(input)<span><a class="mark" href="#net_net_isip_input"id="net_net_isip_input">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v0.3.0</span>
</div><p>Tests if input is an IP address. Returns 0 for invalid strings,
returns 4 for IP version 4 addresses, and returns 6 for IP version 6 addresses.</p>
<h2>net.isIPv4(input)<span><a class="mark" href="#net_net_isipv4_input"id="net_net_isipv4_input">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v0.3.0</span>
</div><p>Returns true if input is a version 4 IP address, otherwise returns false.</p>
<h2>net.isIPv6(input)<span><a class="mark" href="#net_net_isipv6_input"id="net_net_isipv6_input">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v0.3.0</span>
</div><p>Returns true if input is a version 6 IP address, otherwise returns false.</p>

      </div>
    </div>
  </div>
  <script src="assets/sh_main.js"></script>
  <script src="assets/sh_javascript.min.js"></script>
  <script>highlight(undefined, undefined, 'pre');</script>
  <!-- __TRACKING__ -->
</body>
</html>