net.createServer([options][, connectionListener])


  • options <Object>

    • allowHalfOpen <boolean> 如果设置为 false,则当可读端结束时,套接字将自动结束可写端。 默认值: false
    • pauseOnConnect <boolean> 指示是否应在传入连接上暂停套接字。 默认值: false
    • noDelay <boolean> 如果设置为 true,则它会在收到新的传入连接后立即禁用 Nagle 算法。 默认值: false
    • keepAlive <boolean> 如果设置为 true,则它会在接收到新的传入连接后立即在套接字上启用保持活动功能,类似于在 socket.setKeepAlive([enable][, initialDelay]) 中所做的。 默认值: false
    • keepAliveInitialDelay <number> 如果设置为正数,则它会设置在空闲套接字上发送第一个保持活跃探测之前的初始延迟。默认值: 0
  • connectionListener <Function> 自动设置为 'connection' 事件的监听器。

  • 返回: <net.Server>

创建新的 TCP 或 IPC 服务器。

如果 allowHalfOpen 设置为 true,则当套接字的另一端发出传输结束信号时,服务器仅在显式调用 socket.end() 时才发回传输结束。 例如,在 TCP 上下文中,当接收到 FIN 数据包时,只有在显式调用 socket.end() 时才会发送回 FIN 数据包。 在此之前,连接是半关闭的(不可读但仍可写)。 有关更多信息,请参阅 'end' 事件和 RFC 1122(第 4.2.2.13 章节)。

如果 pauseOnConnect 设置为 true,则与每个传入连接关联的套接字将被暂停,并且不会从其句柄读取数据。 这允许在进程之间传递连接,而原始进程不会读取任何数据。 要开始从暂停的套接字读取数据,则调用 socket.resume()

服务器可以是 TCP 服务器或 IPC 服务器,这取决于其 listen() 什么。

这是一个 TCP 回显服务器的示例,它监听端口 8124 上的连接:

const net = require('node:net');
const server = net.createServer((c) => {
  // 'connection' 监听器。
  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');
});

使用 telnet 对此进行测试:

$ telnet localhost 8124

要监听套接字 /tmp/echo.sock

server.listen('/tmp/echo.sock', () => {
  console.log('server bound');
});

使用 nc 连接到 Unix 域套接字服务器:

$ nc -U /tmp/echo.sock
  • options <Object>

    • allowHalfOpen <boolean> If set to false, then the socket will automatically end the writable side when the readable side ends. Default: false.
    • pauseOnConnect <boolean> Indicates whether the socket should be paused on incoming connections. Default: false.
    • noDelay <boolean> If set to true, it disables the use of Nagle's algorithm immediately after a new incoming connection is received. Default: false.
    • keepAlive <boolean> If set to true, it enables keep-alive functionality on the socket immediately after a new incoming connection is received, similarly on what is done in socket.setKeepAlive([enable][, initialDelay]). Default: false.
    • keepAliveInitialDelay <number> If set to a positive number, it sets the initial delay before the first keepalive probe is sent on an idle socket.Default: 0.
  • connectionListener <Function> Automatically set as a listener for the 'connection' event.

  • Returns: <net.Server>

Creates a new TCP or IPC server.

If allowHalfOpen is set to true, when the other end of the socket signals the end of transmission, the server will only send back the end of transmission when socket.end() is explicitly called. For example, in the context of TCP, when a FIN packed is received, a FIN packed is sent back only when socket.end() is explicitly called. Until then the connection is half-closed (non-readable but still writable). See 'end' event and RFC 1122 (section 4.2.2.13) for more information.

If pauseOnConnect is set to true, 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 socket.resume().

The server can be a TCP server or an IPC server, depending on what it listen() to.

Here is an example of a TCP echo server which listens for connections on port 8124:

const net = require('node: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');
});

Test this by using telnet:

$ telnet localhost 8124

To listen on the socket /tmp/echo.sock:

server.listen('/tmp/echo.sock', () => {
  console.log('server bound');
});

Use nc to connect to a Unix domain socket server:

$ nc -U /tmp/echo.sock