Build a bounded echo server
Quick answer
An echo server returns received data. A bounded design restricts the amount of work, memory and waiting time spent on clients.
Build a one-request local demonstration
Use a loopback listener on an OS-selected port and a two-byte request in this tiny fixed-length protocol. The demonstration handles exactly one connection, not a production service. Both endpoints set read and write timeouts. A production listener also needs an accept deadline or cancellation, an overall per-client deadline, a concurrency limit and graceful shutdown. Avoid opening this teaching service to a public interface.
use std::{io::{Read, Write}, net::{TcpListener, TcpStream}, time::Duration};fn main() -> std::io::Result<()> { let l = TcpListener::bind("127.0.0.1:0")?; let mut c = TcpStream::connect_timeout(&l.local_addr()?, Duration::from_secs(2))?; let (mut s, _) = l.accept()?; for x in [&c, &s] { x.set_read_timeout(Some(Duration::from_secs(2)))?; x.set_write_timeout(Some(Duration::from_secs(2)))?; } c.write_all(b"ok")?; let mut b = [0; 2]; s.read_exact(&mut b)?; s.write_all(&b)?; c.read_exact(&mut b)?; println!("{}", String::from_utf8_lossy(&b)); Ok(())}Manual local Rust/Cargo candidate; not executed. Expected behavior:
ok if local I/O succeedsThis sequential demonstration avoids an unbounded worker pool.
fn main() { let sizes = [8, 1024, 1025]; for n in sizes { println!("{}", n <= 1024); }}Expected stdout (not executed):
truetruefalseReject before allocating the body in a variable-length implementation.
Common mistake
A per-read timeout alone is not an overall connection deadline.
Try it yourself
Extend the in-memory framing fixture to echo one message, then design a two-client admission limit.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Build a bounded echo server — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A loopback echo demo handles one connection. Does this test demonstrate production load handling?
Build a bounded echo server — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::{io::{Read, Write}, net::{TcpListener, TcpStream}, time::Duration}; fn main() -> std::io::Result<()> { let l = TcpListener::bind("127.0.0.1:0")?; let mut c = TcpStream::connect_timeout(&l.local_addr()?, Duration::from_secs(2))?; let (mut s, _) = l.accept()?; for x in [&c, &s] { x.set_read_timeout(Some(Duration::from_secs(2)))?; x.set_write_timeout(Some(Duration::from_secs(2)))?; } c.write_all(b"ok")?; let mut b = [0; 2]; s.read_exact(&mut b)?; s.write_all(&b)?; c.read_exact(&mut b)?; println!("{}", String::from_utf8_lossy(&b)); Ok(()) } ``` Which outcome or interpretation best matches this example?
Build a bounded echo server — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn main() { let sizes = [8, 1024, 1025]; for n in sizes { println!("{}", n <= 1024); } } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.