Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Standard Library Modules

The std::net module

Reference for Rust std::net: TcpListener, TcpStream, UdpSocket, IpAddr, Ipv4Addr, Ipv6Addr and SocketAddr, with loopback examples and common pitfalls.

std::net is the standard library's blocking networking toolbox. It has two halves: value types that describe addresses (IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr) and socket types that move bytes (TcpListener, TcpStream, UdpSocket). Everything is synchronous: accept, read and connect block the calling thread until they finish, so servers usually give each connection its own thread or move to an async runtime.

The address types are plain data. They are Copy, comparable, hashable, and they parse from and print to the usual text forms. The socket types wrap an operating-system socket and implement Read and Write (the TCP ones), so all of std::io works on them, including BufReader and read_line.

Rust
use std::net::{Ipv4Addr, SocketAddr, TcpListener};

let ip = Ipv4Addr::new(127, 0, 0, 1);
let addr = SocketAddr::from((ip, 0));
let listener = TcpListener::bind(addr).unwrap();

Try it

Rust Address types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust A TCP echo on the loopback interface

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust UDP datagrams

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Syntax

Item Description
Ipv4Addr, Ipv6Addr One address each. Constants LOCALHOST, UNSPECIFIED, BROADCAST (v4). Predicates such as is_loopback, is_private, is_multicast.
IpAddr Enum with variants V4 and V6. Parses either form.
SocketAddrV4, SocketAddrV6, SocketAddr An IP address plus a port (v6 also carries a flow info and scope id).
ToSocketAddrs Trait for anything that can resolve to socket addresses: &str, (&str, u16), (IpAddr, u16), SocketAddr. May do DNS lookups.
TcpListener bind, accept, incoming, local_addr, set_nonblocking.
TcpStream connect, connect_timeout, try_clone, shutdown, set_read_timeout, set_nodelay, peer_addr.
UdpSocket bind, send_to, recv_from, connect, send, recv, set_broadcast.
Shutdown Read, Write or Both, for TcpStream::shutdown.

Errors are io::Error for sockets and AddrParseError for parsing text addresses.

Usage notes

  • Port 0 means "pick any free port"; read it back with local_addr(). This is how tests avoid port clashes.
  • TCP is a stream, not messages. One write does not equal one read. Frame your data (line endings, length prefixes) as the echo example does with a newline.
  • Blocking by default. Use set_read_timeout to avoid hanging forever, or set_nonblocking(true) and handle ErrorKind::WouldBlock.
  • incoming() yields io::Result<TcpStream> forever; a for loop over it is the classic one-thread-per-connection server skeleton.
  • try_clone gives a second handle to the same socket, which is how you read with a BufReader and still write on the original.
  • Binding to 0.0.0.0 listens on every interface; bind 127.0.0.1 for local-only services.
  • Dropping closes. A socket closes when its last handle is dropped, and the peer then sees end of file (a read returning 0 bytes).
  • No TLS, HTTP or async. For those use crates; the standard library stops at raw sockets.
  • Hostnames in ToSocketAddrs trigger a blocking DNS lookup, and one name may return several addresses.