|
|
|
@ -1,9 +1,22 @@
|
|
|
|
|
use std::env;
|
|
|
|
|
use std::io::Read;
|
|
|
|
|
use std::net::{TcpListener, TcpStream};
|
|
|
|
|
|
|
|
|
|
fn handle_client(stream: TcpStream) {
|
|
|
|
|
fn handle_client(stream: &mut TcpStream) -> std::io::Result<()> {
|
|
|
|
|
println!("---> handling stream {:?}", stream);
|
|
|
|
|
// ...
|
|
|
|
|
let mut buf = [0; 1024];
|
|
|
|
|
let n = stream.read(&mut buf[..])?;
|
|
|
|
|
let req = std::str::from_utf8(&buf[..n]);
|
|
|
|
|
match req {
|
|
|
|
|
Ok(s) => {
|
|
|
|
|
println!("---> first {} bytes: {:?}", n, s);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
println!("---> oh no {:?}", e);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main() -> std::io::Result<()> {
|
|
|
|
@ -16,7 +29,9 @@ fn main() -> std::io::Result<()> {
|
|
|
|
|
let listener = TcpListener::bind(addr)?;
|
|
|
|
|
|
|
|
|
|
for stream in listener.incoming() {
|
|
|
|
|
handle_client(stream?);
|
|
|
|
|
if let Ok(_) = handle_client(&mut stream?) {
|
|
|
|
|
println!("---> looks like that went well")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|