Pick the right conversation.
Separate byte streams, request/response messages, and persistent sessions.
01 Understand the contract
TCP provides an ordered byte stream, not application message boundaries. HTTP defines request and response semantics over a transport. WebSocket begins with an HTTP upgrade and then supports bidirectional framed messages over a persistent connection. A single read may contain only part of an application message, so define framing or use a protocol library. The cookbook uses Axum for HTTP and WebSocket framing rather than inventing an HTTP parser. Bind local practice servers to loopback so their audience matches the exercise.
02 Make it concrete
This is a complete standard-library example. Use Rust stable with the 2024 edition. Run it locally using the Rust workbench.
use std::net::SocketAddr;
fn main() {
let address: SocketAddr = "127.0.0.1:3000".parse().unwrap();
println!(
"loopback={}, port={}",
address.ip().is_loopback(),
address.port()
);
let payload = "hello";
println!("payload bytes={}", payload.len());
}loopback=true, port=3000 payload bytes=5
- 1
SocketAddr separates address and port.
- 2
Loopback restricts access to the local machine.
- 3
A payload length is a byte count, not a TCP packet boundary.
cargo run -p lesson_networking03 Mind the boundary
04 Your next build
Parse an invalid address without panicking and test a valid loopback endpoint.
Reveal one possible solution +
use std::net::SocketAddr;
fn main() {
assert!("invalid".parse::<SocketAddr>().is_err());
let a: SocketAddr = "127.0.0.1:9000".parse().unwrap();
assert!(a.ip().is_loopback());
}05 Check your reasoning
Does TCP preserve application message boundaries?
Understand it.
Then make it yours.
Run the example, try your own input, and explain the boundary. Log the lesson when the idea makes sense.