Rust network programming materials
1. Sockets and async (start here)
std::net(TcpListener,TcpStream) for blocking basics.- Tokio for real work. Its official tutorial (tokio.rs/tokio/tutorial) builds a mini-Redis server and client, which covers framing, shared state, and graceful shutdown.
tokio-utilcodecs (Framed,LengthDelimitedCodec) for protocol framing, and thebytescrate for buffers.- socket2 when you need options
stddoesn’t expose (SO_REUSEPORT, keepalive tuning, buffer sizes,TCP_NODELAYvariants). mio if you want the epoll layer under Tokio.
2. Packets and raw access
pnetfor raw sockets and packet parsing and building.etherparsefor fast, zero-copy header parsing.- The
pcapcrate (libpcap bindings) orpcap-filefor capture and replay. tun-tapor thetuncrate for virtual interfaces. On Linux, use these withip tuntapfor safe experiments.
3. Implementing TCP/IP yourself
- smoltcp is a standalone,
no_std-friendly TCP/IP stack and the best code to read. It’s small enough to follow. - Jon Gjengset’s “Implementing TCP in Rust” live-stream series on YouTube builds TCP over a TUN device from scratch. It’s long but excellent.
- Rust in Action (McNamara) has a networking chapter that builds a stack over a TAP device.
4. Protocol theory (language-agnostic)
- TCP/IP Illustrated, Vol. 1 (Stevens/Fall) is still the reference for how TCP actually behaves.
- RFCs 9293 (TCP), 791 (IP), and 793 for history.
- Beej’s Guide to Network Programming. It’s written for C, but the socket concepts map directly.
Practical tips
- Start with an echo server, then a chat server, then a protocol with real framing.
- Test with
ncat,tcpdump, and Wireshark so you see what’s on the wire.tc netemsimulates loss and delay. - Newer stacks like QUIC (
quinn) are worth a look once TCP makes sense.
Crate APIs shift between versions, so check docs.rs for current signatures. Tell me which direction you want (a server, packet tooling, or your own stack) and I can sketch a starter project.