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


  • options <Object>
    • allowHalfOpen <boolean> 如果设置为 false,当可读端结束时,套接字将自动关闭可写端。默认值: false
    • highWaterMark <number> 可选择覆盖所有 net.SocketreadableHighWaterMarkwritableHighWaterMark默认值:stream.getDefaultHighWaterMark()
    • keepAlive <boolean> 如果设置为 true,在接收到新的传入连接后,会立即在套接字上启用保持活动功能,这与 socket.setKeepAlive() 中的处理类似。默认值: false
    • keepAliveInitialDelay <number> 如果设置为正数,它会设置在空闲套接字上发送第一个保活探测之前的初始延迟。 默认值: 0
    • noDelay <boolean> 如果设置为 true,在收到新的传入连接后会立即禁用 Nagle 算法。默认值: false
    • pauseOnConnect <boolean> 表示在接收到连接时套接字是否应暂停。默认值:false
    • blockList <net.BlockList> blockList 可用于禁止特定 IP 地址、IP 范围或 IP 子网的入站访问。如果服务器位于反向代理、NAT 等之后,该功能将无法生效,因为与阻止列表进行检查的是代理的地址,或 NAT 指定的地址。
  • connectionListener <Function> 会自动设置为 'connection' 事件的监听器。
  • 返回值: <net.Server>

创建一个新的 TCP 或 IPC 服务器。

🌐 Creates a new TCP or IPC server.

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

🌐 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.

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

🌐 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().

服务器可以是 TCP 服务器或 IPC 服务器,取决于它所 listen() 的内容。

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

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

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

import net from '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');
});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');
});

使用 telnet 测试此操作:

🌐 Test this by using telnet:

telnet localhost 8124 

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

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

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

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

🌐 Use nc to connect to a Unix domain socket server:

nc -U /tmp/echo.sock