TCP servers
Build a lightweight TCP service that accepts connections and exchanges stream data.
Node.js · Module
Node.js’s node:net module provides asynchronous, stream-based TCP and IPC networking. Use it to create servers with net.createServer() and clients with net.createConnection() or net.connect().
The node:net module supplies asynchronous stream-based networking for TCP and IPC. Servers are created with net.createServer(), while clients use net.createConnection() or its alias net.connect(); sockets provide the connection’s readable and writable stream interface.
import net from 'node:net';
net.connect(options[, connectListener]);
net.connect(path[, connectListener]);
net.connect(port[, host][, connectListener]);| Name | Type | Description |
|---|---|---|
options | object | Connection options, including TCP or IPC addressing settings. |
path | string | IPC endpoint path. |
port | number | TCP destination port. |
host | string | TCP destination host; optional in the port overload. |
connectListener | function | Function invoked after the connection is established. |
A new net.Socket that starts the connection.
node:net specifier; CommonJS access is also documented.net.connect() is an alias for net.createConnection().path as a TCP host or port; it identifies an IPC endpoint for these APIs.import net from 'node:net';
const server = net.createServer((socket) => {
socket.on('data', (chunk) => socket.write(chunk));
});
server.listen(0, '127.0.0.1', () => {
const address = server.address();
if (!address || typeof address === 'string') return;
const client = net.createConnection(address.port, address.address, () => {
client.write('hello');
});
client.on('data', (data) => {
console.log(data.toString());
client.end();
server.close();
});
});Creates a local TCP server that echoes client data, then connects to it and prints the response.
Build a lightweight TCP service that accepts connections and exchanges stream data.
Connect to another TCP service from a Node.js process.
Communicate locally through operating-system IPC endpoints.
node:net module provide?The module provides asynchronous networking for stream-based TCP or IPC servers and clients. net.createServer() creates servers, and net.createConnection() creates clients.
Follow-up: How can a client connect without explicitly calling socket.connect()? Use net.connect() or net.createConnection(), which creates a socket and immediately initiates the connection.
net.connect()?net.connect() is an alias for net.createConnection(). It accepts options, an IPC path, or a TCP port with an optional host and connection listener.
Follow-up: Which argument identifies an IPC endpoint? The path argument does, and it is accepted by connection, listening, and socket-connection APIs.
A server is started with server.listen(), and a client socket is established with socket.connect() or the connection factory functions. The resulting socket participates in stream-based I/O.
Follow-up: What does the factory return? It returns a new net.Socket that has begun connecting.
net.createServer()net.createConnection()net.Servernet.Socketserver.listen()socket.connect()