std::net
On this page
- Modules
- Structs
Ipv6Addr3Socket42acceptaccept_asyncbindbroadcastcloseconnectconnect_asyncconnect_timeoutlingerlistenlocal_addrnewnodelayonly_v6peer_addrread_timeoutrecv_buffer_sizerecv_fromrecv_from_asyncreuse_addressreuse_portsend_buffer_sizesend_tosend_to_asyncset_broadcastset_keepaliveset_lingerset_nodelayset_nonblockingset_only_v6set_read_timeoutset_recv_buffer_sizeset_reuse_addressset_reuse_portset_send_buffer_sizeset_ttlset_write_timeoutshutdownshutdown_asynctry_clonettlwrite_timeout
ReadHalf4WriteHalf3UdpSocket29bindbroadcastcloseconnectconnect_asyncjoin_multicast_v4leave_multicast_v4local_addrpeer_addrread_timeoutrecvrecv_asyncrecv_fromrecv_from_asyncsendsend_asyncsend_tosend_to_asyncset_broadcastset_multicast_loop_v4set_multicast_ttl_v4set_nonblockingset_read_timeoutset_segment_sizeset_ttlset_write_timeouttry_clonettlwrite_timeout
- Enums
IpAddr3Domain1- Traits
ToSocketAddrs1- Functions
TCP / UDP sockets, addresses, DNS. Unix
sockets are os::unix::net, the Linux kernel copies
os::linux::net::TcpStreamExt.
Three layers: Socket (domain, type, protocol and options, each call
one system call), the typed TcpStream::connect / TcpListener::bind /
UdpSocket::bind over ToSocketAddrs, and the free tcp_connect /
tcp_listen / udp_bind beside them.
Sockets are non-blocking underneath. Blocking calls wait in poll, so
set_*_timeout bounds that wait (async uses task::timeout), and
set_nonblocking(true) makes them throw WouldBlock. *_async calls
suspend until the I/O driver sees the fd ready.
A plain call never suspends: inside a task it blocks the worker, because a sync fn cannot suspend a stackless coroutine.
read(buf) fills buf[0..n] of a SliceMut<u8>; writes take
any AsBytes. No IPv6 multicast. TLS 1.3 is tls.
Modules
tlsTLS 1.3, implemented here in Nabi oncore::crypto: no OS or external TLS library.
Structs
Ipv4AddrSource
pub struct Ipv4AddrAn IPv4 address.
The loopback address.
The unspecified (any) address.
The address
a.b.c.d.The four bytes, most significant first.
The address as a big-endian number (
a.b.c.d=a << 24 | ..).truefor127.0.0.0/8.truefor0.0.0.0.truefor224.0.0.0/4.
Ipv6AddrSource
pub struct Ipv6AddrAn IPv6 address.
Sourcepub const LOCALHOST: Ipv6Addr = Ipv6Addr { segments: [0u16, 0u16, 0u16, 0u16, 0u16, 0u16, 0u16, 1u16] }§The loopback address.
Sourcepub const UNSPECIFIED: Ipv6Addr = Ipv6Addr { segments: [0u16, 0u16, 0u16, 0u16, 0u16, 0u16, 0u16, 0u16] }§The unspecified (any) address.
The address
a:b:c:d:e:f:g:h, one 16-bit segment each.The eight 16-bit groups, most significant first.
truefor::1.
SocketAddrV4Source
pub struct SocketAddrV4An IPv4 address and a port.
ip:port.The IP address.
The port.
Replace the IP address.
Replace the port.
Trait implementations 3
impl Display for SocketAddrV4impl Debug for SocketAddrV4impl ToSocketAddrs for SocketAddrV4
SocketAddrV6Source
pub struct SocketAddrV6An IPv6 address, a port, the flow label and the scope (interface) id.
Sourcepub const fn new(§ip: Ipv6Addr, port: u16, flow_info: u32 = 0u32, scope_id: u32 = 0u32) -> SocketAddrV6 [ip]:port, with an optional flow label and scope id.The IP address.
The port.
The flow label (
sin6_flowinfo).The scope id (
sin6_scope_id), an interface index.Replace the IP address.
Replace the port.
Replace the flow label.
Replace the scope id.
Trait implementations 3
impl Display for SocketAddrV6impl Debug for SocketAddrV6impl ToSocketAddrs for SocketAddrV6
ProtocolSource
pub struct ProtocolAn IP protocol number (the IANA registry).
ICMP for IPv4.
TCP.
UDP.
ICMP for IPv6.
The protocol numbered
number(IANA).The IANA protocol number.
SocketSource
pub struct SocketA socket as the OS has it: any domain, type and protocol, its
options, and each call one system call (bind does not set
SO_REUSEADDR, connect does not resolve). TcpStream::from(s),
TcpListener::from(s) and UdpSocket::from(s) take it over once it
is set up, and Socket::from goes back. It has one owner: let b = a
moves it.
Sourcepub fn new(§domain: Domain, ty: SocketType, protocol: Protocol | none = none) throws (IoError) -> Socket A new socket (non-blocking underneath, not inherited by child processes);
protocolnone is the default forty.
Sourcepub fn bind(§self, addr: SocketAddr) throws (IoError) Bind to
addr.Listen for connections, queueing at most
backlogpending ones.
Sourcepub fn connect(§self, addr: SocketAddr) throws (IoError) Connect, blocking until the handshake is done (in non-blocking mode,
WouldBlockwhile it is in progress).
Sourcepub fn connect_timeout(§self, addr: SocketAddr, dur: Duration) throws (IoError) connectthat throwsTimedOutafterdur.
Sourcepub async fn connect_async(§ref self, addr: SocketAddr) throws (IoError) connectthat suspends the task during the handshake.
Sourcepub fn accept(§self) throws (IoError) -> (Socket, SocketAddr) The next connection and its peer, blocking until one arrives.
Sourcepub async fn accept_async(§ref self) throws (IoError) -> (Socket, SocketAddr) acceptthat suspends the task until a connection arrives.
Sourcepub fn local_addr(§self) throws (IoError) -> SocketAddr The address this socket is bound to.
Sourcepub fn peer_addr(§self) throws (IoError) -> SocketAddr The address of the connected peer.
Shut down reading, writing or both.
As
shutdown, which never waits.
Sourcepub fn send_to<B: AsBytes>(§self, buf: B, addr: SocketAddr) throws (IoError) -> usize Send the datagram
buftoaddr; bytes sent.
Sourcepub async fn send_to_async<B: AsBytes>(§ref self, buf: B, addr: SocketAddr) throws (IoError) -> usize send_tothat suspends the task while the socket is full.
Sourcepub fn recv_from(§self, buf: SliceMut<u8>) throws (IoError) -> (usize, SocketAddr) The next datagram into
buf[0..n](truncated tobuf.len()) and its sender.
Sourcepub async fn recv_from_async(§ref self, buf: SliceMut<u8>) throws (IoError) -> (usize, SocketAddr) recv_fromthat suspends the task until a datagram arrives.Make the blocking calls throw
WouldBlockinstead of waiting.Limit blocking reads to
dur, thenTimedOut;nonewaits forever.Limit blocking writes to
dur, thenTimedOut;nonewaits forever.The read timeout;
nonefor none.The write timeout;
nonefor none.SO_REUSEADDR.SO_REUSEADDR.SO_REUSEPORT;Unsupportedoff Unix.SO_REUSEPORT.TCP_NODELAY.TCP_NODELAY.Keepalive probes after
aliveidle (whole seconds);none= off.SO_BROADCAST.SO_BROADCAST.SO_RCVBUF(the OS may round it, and Linux doubles it).SO_RCVBUF, as the OS reports it.SO_SNDBUF.SO_SNDBUF, as the OS reports it.IP_TTLof an IPv4 socket.IP_TTL.IPV6_V6ONLY: an IPv6 socket that takes no IPv4 traffic.IPV6_V6ONLY.SO_LINGER(whole seconds):closewaits that long for unsent data;none= off.SO_LINGERin whole seconds;nonewhen off.A second handle to the same socket.
Close now, throwing the OS's error; later calls fail with
InvalidInput.
Trait implementations 8
impl Debug for Socketimpl Read for Socketimpl Write for Socketimpl AsyncRead for Socketimpl AsyncWrite for Socketimpl Close for Socketimpl AsyncClose for Socketimpl Drop for Socket
ReadHalfSource
pub struct ReadHalf<T>The read half of a split connection (TcpStream::split,
UnixStream::split, tls::Client::split, tls::Server::split):
it owns the reading direction. Send it to another task or thread
while the WriteHalf writes; close it, or rejoin the two with
unsplit. The socket closes when both halves are closed.
Close the read half; the socket closes once the write half is closed too. Throws the OS's error of that close.
Shut down reading, writing or both of the socket the halves share (TLS: close_notify first for writing).
Bothwakes a write blocked on the other half too.shutdown, awaiting close_notify on TLS instead of blocking.The connection again, from this half and the
WriteHalfthatsplitgave with it. Panics on a write half of another connection.
TcpListenerSource
pub struct TcpListenerA listening TCP socket.
Sourcepub fn bind<A: ToSocketAddrs>(§addr: A) throws (IoError) -> TcpListener Bind and listen on the first address of
addrthat works (SO_REUSEADDRoff Windows, backlog 128). Port 0 picks one: seelocal_addr.The listening sockets the host handed this program, in order: WASI's pre-opened sockets (wasmtime
-S tcplisten=ADDR, preview 1:wasm32-wasip1-threads), systemd socket activation (LISTEN_FDS) on Unix; none on Windows. Where a program cannot bind sockets itself, these are how it serves.
Sourcepub fn accept(§self) throws (IoError) -> (TcpStream, SocketAddr) The next connection and its peer, blocking until one arrives.
Sourcepub async fn accept_async(§ref self) throws (IoError) -> (TcpStream, SocketAddr) acceptthat suspends the task until a connection arrives.
Sourcepub fn local_addr(§self) throws (IoError) -> SocketAddr The bound address (the port picked when bound to port 0).
Set
IP_TTL.IP_TTL.A second descriptor on the same socket (
dup).Make the blocking calls throw
WouldBlockinstead of waiting.try for s in l.incoming() { .. }: connections forever; a failedacceptends the loop with its error. A view of the listener: it stays in the listener's scope, and its owner still closes it.incomingowning the listener: it may leave the scope or move to a task, and closes the listener with itself.Close now, throwing the OS's error.
TcpStreamSource
pub struct TcpStreamA TCP connection. It has one owner (let b = a moves it); close it
with try s.close() or using.
Sourcepub fn connect<A: ToSocketAddrs>(§addr: A) throws (IoError) -> TcpStream Connect to the first address of
addrthat accepts, blocking until the handshake is done.
Sourcepub fn connect_timeout(§addr: SocketAddr, dur: Duration) throws (IoError) -> TcpStream Connect to
addr, giving up afterdur.
Sourcepub async fn connect_async<A: ToSocketAddrs>(§addr: A) throws (IoError) -> TcpStream connectthat suspends the task during each handshake.
Sourcepub fn peer_addr(§self) throws (IoError) -> SocketAddr The remote end's address.
Sourcepub fn local_addr(§self) throws (IoError) -> SocketAddr This end's address.
Shut down the read half, the write half or both; the peer reads end of stream after
Write.As
shutdown, which never waits.Turn Nagle's algorithm off (
TCP_NODELAY) or back on.TCP_NODELAY.Keepalive probes after
aliveidle (whole seconds);none= off.Set
IP_TTL.IP_TTL.Make a blocking read throw
TimedOutafterdur;nonewaits forever.Make a blocking write throw
TimedOutafterdur;nonewaits forever.The read timeout;
nonewhen reads wait forever.The write timeout;
nonewhen writes wait forever.Make the blocking calls throw
WouldBlockinstead of waiting.A second descriptor on the same socket (
dup), with this one's timeouts and blocking mode.Split into a read half and a write half that two tasks or threads can use at once. Each half owns its direction and must be closed;
ReadHalf::unsplitjoins them again.Close now, throwing the OS's error; later calls fail with
InvalidInput.
Trait implementations 9
impl Debug for TcpStreamimpl From<Socket> for TcpStreamimpl Read for TcpStreamimpl Write for TcpStreamimpl AsyncRead for TcpStreamimpl AsyncWrite for TcpStreamimpl Close for TcpStreamimpl AsyncClose for TcpStreamimpl Drop for TcpStream
WriteHalfSource
pub struct WriteHalf<T>The write half of a split connection: it owns the writing
direction. Closing it ends the stream for the peer (shutdown(Write),
after close_notify on TLS) while the read half reads on.
End the stream for the peer, or close the socket when the read half is closed already. Throws the OS's error.
Shut down reading, writing or both of the socket the halves share (TLS: close_notify first for writing).
Bothwakes a read blocked on the other half, asshutdown(SHUT_RDWR)does.shutdown, awaiting close_notify on TLS instead of blocking.
Trait implementations 7
unsafe impl<T> Send for WriteHalf<T>impl<T> Debug for WriteHalf<T>impl<T> Write for WriteHalf<T>impl<T> AsyncWrite for WriteHalf<T>impl<T> Close for WriteHalf<T>impl<T> AsyncClose for WriteHalf<T>impl<T> Drop for WriteHalf<T>
UdpSocketSource
pub struct UdpSocketA UDP socket. It has one owner: let b = a moves it.
Sourcepub fn bind<A: ToSocketAddrs>(§addr: A) throws (IoError) -> UdpSocket Bind the first address of
addrthat works.
Sourcepub fn send_to<B: AsBytes, A: ToSocketAddrs>(§self, buf: B, addr: A) throws (IoError) -> usize Send the datagram
bufto the first address ofaddr; bytes sent.
Sourcepub async fn send_to_async<B: AsBytes, A: ToSocketAddrs>(§ref self, buf: B, addr: A) throws (IoError) -> usize send_to, suspending the task instead of blocking.
Sourcepub fn recv_from(§self, buf: SliceMut<u8>) throws (IoError) -> (usize, SocketAddr) The next datagram into
buf[0..n](truncated tobuf.len()) and its sender.
Sourcepub async fn recv_from_async(§ref self, buf: SliceMut<u8>) throws (IoError) -> (usize, SocketAddr) recv_fromthat suspends the task until a datagram arrives.
Sourcepub fn connect<A: ToSocketAddrs>(§self, addr: A) throws (IoError) Fix the peer to the first address of
addrthat takes it:send/recvthen talk to it only.
Sourcepub async fn connect_async<A: ToSocketAddrs>(§ref self, addr: A) throws (IoError) As
connect: a UDP connect only records the peer, so it never waits (a name inaddris resolved asconnectdoes).Send
bufas one datagram to the peer set byconnect.send, suspending the task instead of blocking.The next datagram from the
connectpeer intobuf(truncated tobuf.len()); its length.recv, suspending the task instead of blocking.
Sourcepub fn local_addr(§self) throws (IoError) -> SocketAddr The bound address.
Sourcepub fn peer_addr(§self) throws (IoError) -> SocketAddr The peer set by
connect; throws without one.Allow sending to broadcast addresses (
SO_BROADCAST).SO_BROADCAST.Set
IP_TTL.IP_TTL.Join the multicast group
addron the interface whose address isinterface(0.0.0.0: the default).Leave the multicast group
addroninterface.Whether this host receives its own multicast datagrams.
Hops a multicast datagram may take (
IP_MULTICAST_TTL).UDP_SEGMENT(GSO);Unsupportedoff Linux.Make a blocking read throw
TimedOutafterdur;nonewaits forever.Make a blocking write throw
TimedOutafterdur;nonewaits forever.The read timeout;
nonewaits forever.The write timeout;
nonewaits forever.Make the blocking calls throw
WouldBlockinstead of waiting.A second descriptor on the same socket (
dup).Close now, throwing the OS's error.
IncomingSource
pub struct IncomingTcpListener::incoming: a view of the listener.
Trait implementations 4
impl !Send for Incomingimpl !Sync for Incomingimpl Debug for Incomingimpl TryIterator for Incoming
Enums
IpAddrSource
pub enum IpAddrAn IPv4 or IPv6 address.
truefor an IPv4 address.truefor an IPv6 address.truefor a loopback address (127.0.0.0/8,::1).
SocketAddrSource
pub enum SocketAddrAn IPv4 or IPv6 socket address.
ipandport; an IPv6 one has flow info and scope id 0.The IP address.
The port.
An address of the other family replaces the whole address (flow info and scope id 0).
Replace the port.
truefor an IPv4 address.truefor an IPv6 address.
Trait implementations 6
impl From<SocketAddrV4> for SocketAddrimpl From<SocketAddrV6> for SocketAddrimpl Display for SocketAddrimpl Debug for SocketAddrimpl FromStr for SocketAddrimpl ToSocketAddrs for SocketAddr
DomainSource
pub enum DomainAn address family.
AF_INET.AF_INET6.
Sourcepub const fn for_address(§addr: SocketAddr) -> Domain The family of
addr.
ShutdownSource
pub enum ShutdownWhich half of a connection shutdown closes.
SocketTypeSource
pub enum SocketTypeA socket type.
SOCK_STREAM: TCP over IP.SOCK_DGRAM: UDP over IP.SOCK_RAW: IP packets of one protocol (privileged; not on WASI).
Traits
ToSocketAddrsSource
pub trait ToSocketAddrsWhat names the socket addresses to try, in order: a SocketAddr,
"host:port" text (resolved, net::resolve), an (IpAddr, u16)
pair or a list of addresses.
Sourcefn to_socket_addrs(§self) throws (IoError) -> List<SocketAddr> The addresses, in the order to try them.
Functions
resolveSource
pub fn resolve(host: str) throws (IoError) -> List<SocketAddr> Addresses of host: "name:port", "[v6]:port", a socket address,
or a bare name / IP (port 0). The addresses come in the system
resolver's order (its preferred address first), at most 32; a name
with none throws NotFound.
tcp_connectSource
pub fn tcp_connect<A: ToSocketAddrs>(addr: A) throws (IoError) -> TcpStream TcpStream::connect(addr).
tcp_connect_asyncSource
pub async fn tcp_connect_async<A: ToSocketAddrs>(addr: A) throws (IoError) -> TcpStream TcpStream::connect_async(addr).
tcp_listenSource
pub fn tcp_listen<A: ToSocketAddrs>(addr: A) throws (IoError) -> TcpListener TcpListener::bind(addr).
udp_bindSource
pub fn udp_bind<A: ToSocketAddrs>(addr: A) throws (IoError) -> UdpSocket UdpSocket::bind(addr).