TcpListener
use tokio::net::{TcpListener, TcpStream};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let listener = TcpListener::bind("0.0.0.0:8080").await?;
loop {
let (stream, addr) = listener.accept().await?;
tokio::spawn(async move {
handle_connection(stream, addr).await;
});
}
}
Each accepted connection is spawned as an independent task. Thousands of connections run concurrently on a handful of threads.