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_errorwhy a network call failed
| Variant | |
|---|---|
NOT_FOUND | the name doesn't resolve (or isn't a name at all) |
REFUSED | nothing listens there |
TIMED_OUT | the timeout passed first |
IN_USE | the address is taken |
RESET | the other side closed the connection |
UNREACHABLE | no route to the network or the host |
IO | anything else |
struct std::net::address
struct addressan 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_streamA 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 = false | a write through write_str failed |
Methods
attach fn read(this: std::net::tcp_stream&, buf: u8[..]) -> std::net::net_error!usizeread 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!voidsend all of data
attach fn write_str(this: std::net::tcp_stream&, s: str) -> voidsend s: this makes a stream a writer, for std::write. A failure sets failed
attach fn shutdown_write(this: std::net::tcp_stream&) -> voidstop 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?) -> voidhow 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::addressthe address of this end
attach fn peer(this: std::net::tcp_stream&) -> std::net::addressthe address of the other end
attach fn delete(this: std::net::tcp_stream&) -> voidstruct std::net::tcp_listener
struct tcp_listenerAccepts 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_streamthe next connection, waiting for one
attach fn local(this: std::net::tcp_listener&) -> std::net::addressthe address it listens on (with the port the system picked for port 0)
attach fn delete(this: std::net::tcp_listener&) -> voidstruct std::net::udp_socket
struct udp_socketA 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!voidsend 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?) -> voidhow 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::addressthe address it's bound to (with the port the system picked for port 0)
attach fn delete(this: std::net::udp_socket&) -> voidFunctions
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_streamconnect 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_streamconnect 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_listenerlisten 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_socketa UDP socket on host's port (port 0: any free one)