github.com/SupersunnySea/draft@v0.16.0/examples/example-rust/src/bin.rs (about) 1 use std::net::{TcpStream, TcpListener}; 2 use std::io::{Write, Error}; 3 use std::thread; 4 5 6 fn reply(mut stream: TcpStream) -> Result<(), Error> { 7 let response = b"HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n<html><body>Hello from Rust & Draft!</body></html>\r\n"; 8 stream.write(response)?; 9 Ok(()) 10 } 11 12 fn main() { 13 let listener = TcpListener::bind("127.0.0.1:8080").unwrap(); 14 println!("👌 Listening for connections on port {}", 8080); 15 for stream in listener.incoming() { 16 17 match stream { 18 19 Ok(stream) => { 20 stream.set_nonblocking(true).expect("set_nonblocking call failed"); 21 stream.set_write_timeout(None).expect("set_write_timeout call failed"); 22 stream.set_nodelay(true).expect("set_nodelay call failed"); 23 thread::spawn(|| { 24 match reply(stream){ 25 Ok(_) =>{}, 26 Err(e) => println!("IO error: {}", e) 27 } 28 }); 29 30 31 } 32 Err(e) => { 33 println!("Unable to connect: {}", e); 34 } 35 } 36 } 37 }