Skip to content

std.net.udp

datagram sockets; send_to/recv_from, or connect once then send/recv

pub Datagram :: struct {
len: usize,
from: addr::SocketAddr,
}

recv_from result: byte count + sender

pub UdpSocket :: struct {
sock: socket::Socket,
}
pub send_to :: fn(self: *Self, data: []const u8, to: addr::SocketAddr) -> usize ! NetError

one datagram to to; bytes sent

pub recv_from :: fn(self: *Self, buf: []u8) -> Datagram ! NetError

one datagram into buf; truncated if longer than buf

pub connect :: fn(self: *Self, to: addr::SocketAddr) -> void ! NetError

default peer for send/recv; drops datagrams from others

pub send :: fn(self: *Self, data: []const u8) -> usize ! NetError

to the connected peer; must call connect first

pub recv :: fn(self: *Self, buf: []u8) -> usize ! NetError

from the connected peer; bytes read

pub set_broadcast :: fn(self: *Self, on: bool) -> void ! NetError
pub set_read_timeout :: fn(self: *Self, ms: i32) -> void ! NetError
pub local_addr :: fn(self: *Self) -> addr::SocketAddr ! NetError
pub close :: fn(self: *Self)
pub bind :: fn(sa: addr::SocketAddr) -> UdpSocket ! NetError

udp socket bound to sa; sets SO_REUSEADDR

pub unbound :: fn() -> UdpSocket ! NetError

unbound socket; the os picks a port on first send