Skip to content

udpBind ​

udpBind binds a local UDP socket and returns an explicit UdpSocket handle.

The returned socket can send and receive datagrams asynchronously. The handle must be closed when it is no longer needed.

SignatureudpBind(address: string): Promise<Result<UdpSocket, NetError>>
tyrus
import { NetError, UdpSocket, udpBind } from "tyrus:net";

async function openSocket(): Promise<Result<void, NetError>> {
    const socket: UdpSocket = (await udpBind("127.0.0.1:0"))?;
    print(socket.localAddress()); // "127.0.0.1:random-port"
    socket.close();
    return Ok();
}

Error Handling ​

error

tyrus
import { NetError, udpBind } from "tyrus:net";

async function openSocket(): Promise<void> {
    const result: Result<UdpSocket, NetError> = await udpBind("");
    match (result) {
        Ok(socket) => { socket.close(); }
        Err(error) => { print(error.code); // "ERR_NET_INVALID_ADDRESS"
        }
    }
}

Binding failures return Err(NetError) with ERR_NET_INVALID_ADDRESS, ERR_NET_PERMISSION, or another dedicated ERR_NET_* code.