net.createConnection(options[, connectListener])


有关可用的选项,请参阅 new net.Socket([options])socket.connect(options[, connectListener])

其他选项:

以下是 net.createServer() 部分中描述的回显服务器的客户端示例:

const net = require('net');
const client = net.createConnection({ port: 8124 }, () => {
  // 'connect' 监听器。
  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');
});

连接套接字 /tmp/echo.sock

const client = net.createConnection({ path: '/tmp/echo.sock' });

For available options, see new net.Socket([options]) and socket.connect(options[, connectListener]).

Additional options:

Following is an example of a client of the echo server described in the net.createServer() section:

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

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

const client = net.createConnection({ path: '/tmp/echo.sock' });