Accepting TCP
Before we write any TCP code, we must acknowledge that our code is at risk of becoming bloated due to all the code being in one file. To prevent the src/main.rs
file from becoming bloated, we must copy all the code apart from the main function from the src/main.rs
file into a file called src/actors.rs
. Now, we can wipe our src/main.rs
file completely and fill it with the following outline:
use tokio::net::TcpListener; use std::{thread, time}; #[tokio::main] async fn main() { let addr = "127.0.0.1:8080".to_string(); let mut socket = TcpListener::bind(&addr).await.unwrap(); println!("Listening on: {}", addr); while let Ok((mut stream, peer)) = socket.accept().await { println!("Incoming connection from: {}", &...