Skip to content
volt
GitHub

std::net

The network: names (resolve), TCP (listen, connect, streams) and UDP, with timeouts. One file for every system: the socket calls, constants, struct layouts and error numbers that differ are picked with @cfg("os") (Linux, macOS, FreeBSD and Windows, 64-bit), so a program builds unchanged on each.

Source: std/net.volt

Types

error std::net::net_error

error net_error

why a network call failed

Variant
NOT_FOUNDthe name doesn't resolve (or isn't a name at all)
REFUSEDnothing listens there
TIMED_OUTthe timeout passed first
IN_USEthe address is taken
RESETthe other side closed the connection
UNREACHABLEno route to the network or the host
IOanything else

struct std::net::address

struct address

an IP address and a port: ip holds 4 bytes for IPv4 (the rest zero), 16 for IPv6

Field
ip: u8[16]
v6: bool
port: u16

Methods

<A: std::mem::allocator = std::mem::default_allocator> attach fn text(this: address&, allocator: A = {}) -> std::string<A>

(inside the namespace: a top-level text would hide the std::text namespace) "1.2.3.4:80", or "[2001:db8:0:0:0:0:0:1]:443" for IPv6

struct std::net::tcp_stream

struct tcp_stream

A TCP connection: read and write bytes; closed when it's deleted. It's a writer, so std::write formats straight into it (a failed write shows as failed).

Field
fd: i64
failed: bool = falsea write through write_str failed

Methods

attach fn read(this: std::net::tcp_stream&, buf: u8[..]) -> std::net::net_error!usize

read up to buf.len bytes: how many (0 when the other side has finished sending)

<A: std::mem::allocator = std::mem::default_allocator> attach fn read_all(this: std::net::tcp_stream&, allocator: A = {}) -> std::net::net_error!std::string<A>

everything until the other side finishes sending

attach fn write(this: std::net::tcp_stream&, data: str) -> std::net::net_error!void

send all of data

attach fn write_str(this: std::net::tcp_stream&, s: str) -> void

send s: this makes a stream a writer, for std::write. A failure sets failed

attach fn shutdown_write(this: std::net::tcp_stream&) -> void

stop sending: the other side's reads see the end; this side can still read

attach fn set_timeout(this: std::net::tcp_stream&, read: std::time::duration?, write: std::time::duration?) -> void

how long a read or a write may wait before it fails with TIMED_OUT (null: as long as it takes)

attach fn local(this: std::net::tcp_stream&) -> std::net::address

the address of this end

attach fn peer(this: std::net::tcp_stream&) -> std::net::address

the address of the other end

attach fn delete(this: std::net::tcp_stream&) -> void

struct std::net::tcp_listener

struct tcp_listener

Accepts TCP connections on an address; closed when it's deleted.

Field
fd: i64

Methods

attach fn accept(this: std::net::tcp_listener&) -> std::net::net_error!std::net::tcp_stream

the next connection, waiting for one

attach fn local(this: std::net::tcp_listener&) -> std::net::address

the address it listens on (with the port the system picked for port 0)

attach fn delete(this: std::net::tcp_listener&) -> void

struct std::net::udp_socket

struct udp_socket

A UDP socket: sends and receives datagrams; closed when it's deleted.

Field
fd: i64

Methods

attach fn send_to(this: std::net::udp_socket&, data: str, to: std::net::address) -> std::net::net_error!void

send data as one datagram to to

attach fn recv_from(this: std::net::udp_socket&, buf: u8[..]) -> std::net::net_error!(usize, std::net::address)

the next datagram, into buf (cut to buf.len): its length and where it came from

attach fn set_timeout(this: std::net::udp_socket&, read: std::time::duration?) -> void

how long recv_from may wait before it fails with TIMED_OUT (null: as long as it takes)

attach fn local(this: std::net::udp_socket&) -> std::net::address

the address it's bound to (with the port the system picked for port 0)

attach fn delete(this: std::net::udp_socket&) -> void

Functions

std::net::resolve

<A: std::mem::allocator = std::mem::default_allocator> fn resolve(host: str, port: u16, allocator: A = {}) -> net_error!std::vec<address, A>

the addresses host (a name or a numeric address) resolves to, with port, from the system's resolver (getaddrinfo: the hosts file, DNS, ...)

std::net::connect_to

fn connect_to(a: address, timeout: std::time::duration? = null) -> net_error!tcp_stream

connect to a's port, giving up after timeout (null: wait as long as the system does)

std::net::connect

fn connect(host: str, port: u16, timeout: std::time::duration? = null) -> net_error!tcp_stream

connect to host (a name or a numeric address) on port: each address it resolves to in turn, giving up on each after timeout

std::net::listen

fn listen(host: str, port: u16, backlog: i32 = 128) -> net_error!tcp_listener

listen for connections on host's port (port 0: any free one; local() tells which)

std::net::bind_udp

fn bind_udp(host: str, port: u16) -> net_error!udp_socket

a UDP socket on host's port (port 0: any free one)