tcpListen
tcpListen binds a TCP endpoint and returns a TcpListener handle inside Ok; recoverable bind failures return Err(NetError).
Use accept to receive connections. The optional timeout applies to the bind operation and is expressed in milliseconds.
Signature
tcpListen(address: string, timeoutMs?: u64): Promise<Result<TcpListener, NetError>>tyrus
import { NetError, TcpListener, tcpListen } from "tyrus:net";
async function listen(): Promise<Result<void, NetError>> {
const listener: TcpListener = (await tcpListen("127.0.0.1:0", 1000))?;
print(listener.localAddress()); // "127.0.0.1:random-port"
listener.close();
return Ok();
}Error Handling
error
tyrus
import { NetError, TcpListener, tcpListen } from "tyrus:net";
async function listen(): Promise<void> {
const result: Result<TcpListener, NetError> = await tcpListen("", 1000);
match (result) {
Ok(listener) => { listener.close(); }
Err(error) => { printError(error.code); }
}
}Binding failures fulfill with Err(NetError) using ERR_NET_INVALID_ADDRESS, ERR_NET_PERMISSION, or another dedicated ERR_NET_* code.
