let listener = TcpListener::bind("localhost:1234").unwrap();
for stream in listener.incoming() {
let s = stream.unwrap();
handle_stream(s);
}
fn handle_stream(mut stream: TcpStream) -> () {
let mut buf = [0];
loop {
let _ = match stream.read(&mut buf) {
Err(e) => panic!("Got an error: {}", e),
Ok(m) => {
println!("Received {:?}, {:?}", m, buf);
if m == 0 {
// doesn't reach here.
break;
}
m
},
};
}
}
I then connect to the server by running curl http://localhost:1234
.
I expect Ok(0)
would be returned, but it doesn't reach that statement and it hangs instead. If that's an EOF, how can I treat in this case?
You want to use
TcpStream::set_read_timeout
and then check for that specific type of error: