host.tcp
Connect and listen for TCP connections on a client.
The TCP interface creates outbound connections and listeners on the selected client, with byte streams for reading and writing.
Here, host is a selected RemoteHost; see host discovery to obtain one. The module overview covers permissions and shared errors.
TCP requires tcp.enabled. A TcpAddress has read-only hostname: string and port: number fields.
host.tcp.connect(options)
Function: establishes a TCP connection from the client.
Parameters: required options, containing optional hostname: string (default 127.0.0.1) and required port: number (integer 1–65535).
Return value: Promise<TcpConnection>.
host.tcp.listen(options?)
Function: listens for TCP connections on the client.
Parameters: optional { hostname?: string; port?: number }, default 127.0.0.1:0. Port is an integer 0–65535; 0 allocates a free port.
Return value: Promise<TcpListener>.
TcpConnection
| Property | Type | Description |
|---|---|---|
localAddr, remoteAddr | Readonly<TcpAddress> | Local and remote addresses |
readable | ReadableStream<Uint8Array> | Reads bytes; cancel closes the whole connection |
writable | WritableStream<Uint8Array> | Writes bytes; close sends write EOF only, abort closes the whole connection |
closed | Promise<void> | Resolves when the connection ends, rejects on remote failure |
connection.close() takes no arguments and returns Promise<void>. It closes the whole connection and is safe to repeat.
TcpListener
Read-only addr: Readonly<TcpAddress> is the actual listening address. finished: Promise<void> represents listener completion.
| Method | Function | Parameters | Return value |
|---|---|---|---|
listener.accept() | Wait for one connection; only one pending call is allowed | None | Promise<TcpConnection | null>; null after explicit close, rejects on listener failure |
listener.close() | Stop accepting; existing connections remain independent | None | Promise<void> |
listener[Symbol.asyncIterator]() | Iterate connections with for await | None | AsyncIterator<TcpConnection>; leaving iteration closes the listener |
See remote.d.ts for type declarations.
