Skip to content

std.net.socket

raw OS socket handle; options shared by tcp and udp

pub Socket :: struct {
fd: usize,
}

raw Winsock SOCKET; INVALID_SOCKET = none; make with tcp()/udp() or from_fd

pub is_valid :: fn(self: *Self) -> bool

false for INVALID_SOCKET

pub close :: fn(self: *Self)

safe on an invalid handle

pub shutdown :: fn(self: *Self, how: i32) -> void ! NetError

how: os::SHUT_RECV, SHUT_SEND or SHUT_BOTH

pub set_opt_int :: fn(self: *Self, level: i32, opt: i32, value: i32) -> void ! NetError

int-valued option: timeouts, BOOL flags

pub set_reuse_addr :: fn(self: *Self, on: bool) -> void ! NetError

allow binding an address in TIME_WAIT; servers set it before bind

pub set_broadcast :: fn(self: *Self, on: bool) -> void ! NetError

allow sending to broadcast addresses (UDP)

pub set_recv_timeout :: fn(self: *Self, ms: i32) -> void ! NetError

ms, 0 = block forever; a blocking recv then fails instead of hanging

pub set_send_timeout :: fn(self: *Self, ms: i32) -> void ! NetError

ms, 0 = block forever

pub set_nonblocking :: fn(self: *Self, on: bool) -> void ! NetError

non-blocking calls fail with would_block instead of waiting (os::is_would_block)

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

servers, or a fixed UDP port

pub local_addr :: fn(self: *Self) -> addr::SocketAddr ! NetError

bound address; resolves the ephemeral port after binding port 0

pub from_fd :: fn(fd: usize) -> Socket

wraps an open OS socket, e.g. from accept

pub tcp :: fn() -> Socket ! NetError

IPv4 TCP stream socket

pub udp :: fn() -> Socket ! NetError

IPv4 UDP datagram socket