Skip to content

tcpListen ​

tcpListen vincula un endpoint TCP y devuelve un handle TcpListener dentro de Ok; los fallos recuperables de bind devuelven Err(NetError).

Usa accept para recibir conexiones. El timeout opcional se aplica al bind y se expresa en milisegundos.

FirmatcpListen(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();
}

Manejo de errores ​

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

Los fallos de bind cumplen con Err(NetError) usando ERR_NET_INVALID_ADDRESS, ERR_NET_PERMISSION u otro código ERR_NET_* dedicado.