Skip to content

UdpSocket

1 min read

Import: import system.net;

Read the usage guide for examples and common tasks. This reference lists the public declarations for this part of the library. See Reading API signatures for parameter and result notation.

UdpSocket

An owned UDP socket with explicit message boundaries and bounded receive buffers.

public class UdpSocket

bind

Binds an endpoint; port zero selects an ephemeral port.

public static async fun bind(
    host: string,
    port: int,
    token: Option<CancellationToken> = Option.None
): Result<UdpSocket, IoError>

local_host

Numeric bound interface address.

public get local_host(): string

local_port

Actual bound UDP port.

public get local_port(): int

send

Sends one datagram of at most 65507 bytes with optional deadline and cancellation.

public async fun send(
    host: string,
    port: int,
    data: byte[],
    timeout_ms: int = 0,
    token: Option<CancellationToken> = Option.None
): Result<int, IoError>

receive

Receives one message, reporting truncation explicitly; a concurrent receive returns EBUSY.

public async fun receive(
    max_bytes: int = 65535,
    timeout_ms: int = 0,
    token: Option<CancellationToken> = Option.None
): Result<UdpDatagram, IoError>

set_broadcast

Enables or disables sending to broadcast endpoints.

public fun set_broadcast(enabled: bool): Result<Unit, IoError>

set_ttl

Sets outgoing IP time-to-live to a value between 1 and 255.

public fun set_ttl(ttl: int): Result<Unit, IoError>

close

Closes the owned socket and wakes pending receives; repeated closure succeeds.

public fun close(): Result<Unit, IoError>