You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
964 B

use std::env;
use std::io::Read;
use std::net::{TcpListener, 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<()> {
let mut addr = String::from("127.0.0.1:19721");
if let Ok(v) = env::var("ADDR") {
addr = String::from(v.as_str());
}
println!("listening at {}", addr);
let listener = TcpListener::bind(addr)?;
for stream in listener.incoming() {
if let Ok(_) = handle_client(&mut stream?) {
println!("---> looks like that went well")
}
}
Ok(())
}