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


  • options <Object>

    • allowHalfOpen <boolean> 如果设置为 false,则当可读端结束时,套接字将自动结束可写端。默认值:false

      ¥allowHalfOpen <boolean> If set to false, then the socket will automatically end the writable side when the readable side ends. Default: false.

    • highWaterMark <number> 可选择覆盖所有 net.Socket' readableHighWaterMarkwritableHighWaterMark。默认值:参见 stream.getDefaultHighWaterMark()

      ¥highWaterMark <number> Optionally overrides all net.Sockets' readableHighWaterMark and writableHighWaterMark. Default: See stream.getDefaultHighWaterMark().

    • keepAlive <boolean> 如果设置为 true,则它会在接收到新的传入连接后立即在套接字上启用保持活动功能,类似于在 socket.setKeepAlive() 中所做的。默认值: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(). Default: false.

    • keepAliveInitialDelay <number> 如果设置为正数,则它会设置在空闲套接字上发送第一个保持活跃探测之前的初始延迟。默认值:0

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

    • noDelay <boolean> 如果设置为 true,则它会在收到新的传入连接后立即禁用 Nagle 算法。默认值: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.

    • pauseOnConnect <boolean> 指示是否应在传入连接上暂停套接字。默认值:false

      ¥pauseOnConnect <boolean> Indicates whether the socket should be paused on incoming connections. Default: false.

    • blockList <net.BlockList> blockList 可用于禁用对特定 IP 地址、IP 范围或 IP 子网的入站访问。如果服务器位于反向代理、NAT 等后面,则这不起作用,因为针对阻止列表检查的地址是代理的地址,或 NAT 指定的地址。

      ¥blockList <net.BlockList> blockList can be used for disabling inbound access to specific IP addresses, IP ranges, or IP subnets. This does not work if the server is behind a reverse proxy, NAT, etc. because the address checked against the block list is the address of the proxy, or the one specified by the NAT.

  • connectionListener <Function> 自动设置为 'connection' 事件的监听器。

    ¥connectionListener <Function> Automatically set as a listener for the 'connection' event.

  • 返回:<net.Server>

    ¥Returns: <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