1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
|
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Net | Node.js v4.8.2 Manual & 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 & 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/C++ Addons</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-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 & 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 v4.8.2 Documentation</h1>
<div id="gtoc">
<p>
<a href="index.html" name="toc">Index</a> |
<a href="all.html">View on single page</a> |
<a href="net.json">View as JSON</a>
</p>
</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: 'close'</a></span></li>
<li><span class="stability_undefined"><a href="#net_event_connection">Event: 'connection'</a></span></li>
<li><span class="stability_undefined"><a href="#net_event_error">Event: 'error'</a></span></li>
<li><span class="stability_undefined"><a href="#net_event_listening">Event: 'listening'</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_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: 'close'</a></span></li>
<li><span class="stability_undefined"><a href="#net_event_connect">Event: 'connect'</a></span></li>
<li><span class="stability_undefined"><a href="#net_event_data">Event: 'data'</a></span></li>
<li><span class="stability_undefined"><a href="#net_event_drain">Event: 'drain'</a></span></li>
<li><span class="stability_undefined"><a href="#net_event_end">Event: 'end'</a></span></li>
<li><span class="stability_undefined"><a href="#net_event_error_1">Event: 'error'</a></span></li>
<li><span class="stability_undefined"><a href="#net_event_lookup">Event: 'lookup'</a></span></li>
<li><span class="stability_undefined"><a href="#net_event_timeout">Event: 'timeout'</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_destroy">socket.destroy()</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>
<pre class="api_stability api_stability_2">Stability: 2 - Stable</pre><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('net');</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_events_eventemitter"><code>EventEmitter</code></a> with the following events:</p>
<h3>Event: 'close'<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: 'connection'<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"><net.Socket></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: 'error'<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"><Error></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>'close'</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: 'listening'<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.
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: 'IPv4', address: '127.0.0.1' }</code></p>
<p>Example:</p>
<pre><code class="lang-js">var server = net.createServer((socket) => {
socket.end('goodbye\n');
}).on('error', (err) => {
// handle errors here
throw err;
});
// grab a random port.
server.listen(() => {
console.log('opened server on', server.address());
});
</code></pre>
<p>Don't call <code>server.address()</code> until the <code>'listening'</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><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>'close'</code></a> event.
The optional <code>callback</code> will be called once the <code>'close'</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>
<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><pre class="api_stability api_stability_0">Stability: 0 - Deprecated: Use <a href="#net_server_getconnections_callback"><code>server.getConnections()</code></a> instead.</pre><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><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"><Object></a></li>
<li><code>backlog</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><Number></a></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></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: <n>}</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>'listening'</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>'listening'</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"><Object></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"><Number></a> - Optional.</li>
<li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><String></a> - Optional.</li>
<li><code>backlog</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><Number></a> - Optional.</li>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><String></a> - Optional.</li>
<li><code>exclusive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><Boolean></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"><Function></a> - Optional.</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: 'localhost',
port: 80,
exclusive: true
});
</code></pre>
<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"><String></a></li>
<li><code>backlog</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><Number></a></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></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>'listening'</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>'listening'</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>net.createServer().listen(
path.join('\\\\?\\pipe', process.cwd(), 'myctl'))
</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>
<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><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>'listening'</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>'listening'</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>'listening'</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('error', (e) => {
if (e.code == 'EADDRINUSE') {
console.log('Address in use, retrying...');
setTimeout(() => {
server.close();
server.listen(PORT, HOST);
}, 1000);
}
});
</code></pre>
<p>(Note: All sockets in Node.js are set <code>SO_REUSEADDR</code>.)</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'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><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'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>
<p>Returns <code>server</code>.</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><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>
<p>Returns <code>server</code>.</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>'connection'</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>
<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 <code>createServer()</code> and <code>'end'</code> event.</p>
<p><code>net.Socket</code> instances are <a href="events.html#events_class_events_eventemitter"><code>EventEmitter</code></a> with the following events:</p>
<h3>Event: 'close'<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"><Boolean></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: 'connect'<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: 'data'<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"><Buffer></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>'data'</code> event.</p>
<h3>Event: 'drain'<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: 'end'<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: 'error'<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"><Error></a></li>
</ul>
<p>Emitted when an error occurs. The <code>'close'</code> event will be called directly
following this event.</p>
<h3>Event: 'lookup'<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"><Error></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><Null></a> The error object. See <a href="dns.html#dns_dns_lookup_hostname_options_callback"><code>dns.lookup()</code></a>.</li>
<li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><String></a> The IP address.</li>
<li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><String></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><Null></a> The address type. See <a href="dns.html#dns_dns_lookup_hostname_options_callback"><code>dns.lookup()</code></a>.</li>
</ul>
<h3>Event: 'timeout'<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: 'IPv4', address: '127.0.0.1' }</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'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
"throttle" 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>'localhost'</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>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>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>'connect'</code></a> event is emitted the
socket is established. If there is a problem connecting, the <code>'connect'</code> event
will not be emitted, the <a href="#net_event_error_1"><code>'error'</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>'connect'</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 either as either <code>{port: port, host: host}</code> or <code>{path: path}</code>.</p>
<h3>socket.destroy()<span><a class="mark" href="#net_socket_destroy" id="net_socket_destroy">#</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>
<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><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>
<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>'0.0.0.0'</code> and the
client connects on <code>'192.168.1.1'</code>, the value would be <code>'192.168.1.1'</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>
<p>Pauses the reading of data. That is, <a href="#net_event_data"><code>'data'</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><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'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>
<p>Returns <code>socket</code>.</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>'74.125.127.100'</code> or <code>'2001:4860:a005::68'</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>'IPv4'</code> or <code>'IPv6'</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>
<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><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><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>
<p>Returns <code>socket</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><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>
<p>Returns <code>socket</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><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>'timeout'</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"><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>'timeout'</code></a> event.</p>
<p>Returns <code>socket</code>.</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><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>
<p>Returns <code>socket</code>.</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>'drain'</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>'connect'</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('net');
const client = net.connect({port: 8124}, () => {
// 'connect' listener
console.log('connected to server!');
client.write('world!\r\n');
});
client.on('data', (data) => {
console.log(data.toString());
client.end();
});
client.on('end', () => {
console.log('disconnected from server');
});
</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: '/tmp/echo.sock'});
</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>'connect'</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>'localhost'</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>'connect'</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>The <code>connectListener</code> parameter will be added as a listener for the
<a href="#net_event_connect"><code>'connect'</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('net');
const client = net.createConnection({port: 8124}, () => {
//'connect' listener
console.log('connected to server!');
client.write('world!\r\n');
});
client.on('data', (data) => {
console.log(data.toString());
client.end();
});
client.on('end', () => {
console.log('disconnected from server');
});
</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: '/tmp/echo.sock'});
</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>'connect'</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>'localhost'</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>'connect'</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>'connection'</code></a> event.</p>
<p><code>options</code> is an object with the following defaults:</p>
<pre><code class="lang-js">{
allowHalfOpen: false,
pauseOnConnect: false
}
</code></pre>
<p>If <code>allowHalfOpen</code> is <code>true</code>, then the socket won'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>'end'</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('net');
const server = net.createServer((c) => {
// 'connection' listener
console.log('client connected');
c.on('end', () => {
console.log('client disconnected');
});
c.write('hello\r\n');
c.pipe(c);
});
server.on('error', (err) => {
throw err;
});
server.listen(8124, () => {
console.log('server bound');
});
</code></pre>
<p>Test this by using <code>telnet</code>:</p>
<pre><code>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('/tmp/echo.sock', () => {
console.log('server bound');
});
</code></pre>
<p>Use <code>nc</code> to connect to a UNIX domain socket server:</p>
<pre><code class="lang-js">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>
|