socket.connect(options[, connectListener])


在给定的套接字上启动连接。 通常不需要此方法,应该使用 net.createConnection() 来创建和打开套接字。 仅在实现自定义的套接字时才使用此方法。

对于 TCP 连接,可用的 options 有:

  • port <number> 必须。套接字要连接的端口。
  • host <string> 套接字要连接的主机。默认值: 'localhost'
  • localAddress <string> 套接字要连接的本地地址。
  • localPort <number> 套接字要连接的本地端口。
  • family <number> IP 栈的版本。必须为 4600 值表示允许 IPv4 和 IPv6 地址。默认值: 0
  • hints <number> 可选的 dns.lookup() 提示
  • lookup <Function> 自定义的查找函数。默认值: dns.lookup()

对于 IPC 连接,可用的 options 有:

对于这两种类型,可用 options 包括:

  • onread <Object> 如果指定,则传入的数据会存储在单个 buffer 中,并在数据到达套接字时传给提供的 callback。 这将会导致流式的功能不会提供任何数据。 该套接字将会正常触发 'error''end''close' 之类的事件。 诸如 pause()resume() 之类的方法也将会按预期运行。
    • buffer <Buffer> | <Uint8Array> | <Function> 用于存储传入数据的可复用的内存块、或返回此类数据的函数。
    • callback <Function> 为每个传入的数据块调用此函数。有两个参数传给它:写入 buffer 的字节数和对 buffer 的引用。从此函数返回 false 可以隐式地 pause() 套接字。该函数将会在全局的上下文中执行。

以下是使用 onread 选项的客户端的示例:

const net = require('net');
net.connect({
  port: 80,
  onread: {
    // 为套接字的每次读取复用 4KiB 的 Buffer。
    buffer: Buffer.alloc(4 * 1024),
    callback: function(nread, buf) {
      // 收到的数据在 `buf` 中可用,从 0 到 'nread`。
      console.log(buf.toString('utf8', 0, nread));
    }
  }
});

Initiate a connection on a given socket. Normally this method is not needed, the socket should be created and opened with net.createConnection(). Use this only when implementing a custom Socket.

For TCP connections, available options are:

  • port <number> Required. Port the socket should connect to.
  • host <string> Host the socket should connect to. Default: 'localhost'.
  • localAddress <string> Local address the socket should connect from.
  • localPort <number> Local port the socket should connect from.
  • family <number>: Version of IP stack. Must be 4, 6, or 0. The value 0 indicates that both IPv4 and IPv6 addresses are allowed. Default: 0.
  • hints <number> Optional dns.lookup() hints.
  • lookup <Function> Custom lookup function. Default: dns.lookup().

For IPC connections, available options are:

For both types, available options include:

  • onread <Object> If specified, incoming data is stored in a single buffer and passed to the supplied callback when data arrives on the socket. This will cause the streaming functionality to not provide any data. The socket will emit events like 'error', 'end', and 'close' as usual. Methods like pause() and resume() will also behave as expected.
    • buffer <Buffer> | <Uint8Array> | <Function> Either a reusable chunk of memory to use for storing incoming data or a function that returns such.
    • callback <Function> This function is called for every chunk of incoming data. Two arguments are passed to it: the number of bytes written to buffer and a reference to buffer. Return false from this function to implicitly pause() the socket. This function will be executed in the global context.

Following is an example of a client using the onread option:

const net = require('net');
net.connect({
  port: 80,
  onread: {
    // Reuses a 4KiB Buffer for every read from the socket.
    buffer: Buffer.alloc(4 * 1024),
    callback: function(nread, buf) {
      // Received data is available in `buf` from 0 to `nread`.
      console.log(buf.toString('utf8', 0, nread));
    }
  }
});