use std::io::{Read, Write}; use std::net::{Shutdown, TcpListener, TcpStream}; use std::thread; fn handle_client(mut stream: TcpStream) { let mut data = [0_u8; 65535]; // using 50 byte buffer match stream.read(&mut data) { Ok(size) => { // echo everything! stream.write(&data[0..size]).unwrap(); // true } Err(_) => { println!( "An error occurred, terminating connection with {}", stream.peer_addr().unwrap() ); stream.shutdown(Shutdown::Both).unwrap(); // false } } {} } fn main() { thread::spawn(move || { let listener = TcpListener::bind("0.0.0.0:3334").unwrap(); // accept connections and process them, spawning a new // thread for each one println!("Server listening on port 3334"); panic!("Error"); for stream in listener.incoming() { match stream { Ok(stream) => { println!("New connection: {}", stream.peer_addr().unwrap()); thread::spawn(move || { // connection succeeded handle_client(stream) }); } Err(e) => { println!("Error: {e:?}"); /* connection failed */ } } } }) .join(); loop {} // close the socket server // drop(listener); }