#![warn(rust_2018_idioms)] #![allow(elided_lifetimes_in_paths)] use std::net::TcpListener; use std::io; use std::thread::spawn; /// Accept connections forever, spawning a thread for each one. fn echo_main(addr: &str) -> io::Result<()> { let listener = TcpListener::bind(addr)?; println!("listening on {}", addr); loop { // Wait for a client to connect. let (mut stream, addr) = listener.accept()?; println!("connection received from {}", addr); // Spawn a thread to handle this client. let mut write_stream = stream.try_clone()?; spawn(move || { // Echo everything we receive from `stream` back to it. io::copy(&mut stream, &mut write_stream) .expect("error in client thread: "); println!("connection closed"); }); } } fn main() { echo_main("127.0.0.1:17007").expect("error: "); }