Reading bytes time
This commit is contained in:
parent
8f3897e097
commit
7ae9903d95
@ -1,9 +1,22 @@
|
|||||||
use std::env;
|
use std::env;
|
||||||
|
use std::io::Read;
|
||||||
use std::net::{TcpListener, TcpStream};
|
use std::net::{TcpListener, TcpStream};
|
||||||
|
|
||||||
fn handle_client(stream: TcpStream) {
|
fn handle_client(stream: &mut TcpStream) -> std::io::Result<()> {
|
||||||
println!("---> handling stream {:?}", stream);
|
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<()> {
|
fn main() -> std::io::Result<()> {
|
||||||
@ -16,7 +29,9 @@ fn main() -> std::io::Result<()> {
|
|||||||
let listener = TcpListener::bind(addr)?;
|
let listener = TcpListener::bind(addr)?;
|
||||||
|
|
||||||
for stream in listener.incoming() {
|
for stream in listener.incoming() {
|
||||||
handle_client(stream?);
|
if let Ok(_) = handle_client(&mut stream?) {
|
||||||
|
println!("---> looks like that went well")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user