1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| use std::net::{TcpListener,TcpStream}; use std::thread; use std::io::{Read,Write,Error};
fn handle_client(mut stream:TcpStream)->Result<(),Error>{ println!("Incomming connection from :{}",stream.peer_addr()?); let mut buf=[0;512]; loop { let bytes_read = stream.read(&mut buf)?; if bytes_read == 0 { return Ok(()); } stream.write(&buf[..bytes_read])?; } } fn main() { let listtener = TcpListener::bind("0.0.0.0:8888").expect("could not bind"); for stream in listtener.incoming(){ match stream { Err(e)=>{eprintln!("failed:{}",e)} Ok(stream)=>{ thread::spawn(move||{ handle_client(stream).unwrap_or_else(|error|eprintln!("{:?}",error)); }); } } }
}
|