When using MIO (0.3.5) how do I detect the termination of a connection?
I tried the following:
extern crate mio;
use mio::{EventLoop,Token,ReadHint};
use std::io::Read;
fn main(){
let listener = mio::tcp::TcpListener::bind("localhost:1234").unwrap();
let (stream,_) : (mio::tcp::TcpStream, _) = listener.accept().unwrap();
let mut event_loop = EventLoop::new().unwrap();
event_loop.register(&stream,Token(0)).unwrap();
println!("run...");
event_loop.run(&mut H{stream:stream}).unwrap();
}
struct H{stream : mio::tcp::TcpStream}
impl mio::Handler for H{
type Timeout = ();
type Message = ();
fn readable(&mut self, _ : &mut EventLoop<Self>, _ : Token, hint: ReadHint){
let mut buf: [u8; 500] = [0; 500];
println!("{} {}",(hint==ReadHint::data()),self.stream.read(&mut buf).unwrap());
std::thread::sleep_ms(1000);
}
}
Run this. Connect to it using something like nc localhost 1234
. Terminate connection by using Ctrl-C. My code will think there is new data available (hint==ReadHint::data()
). A try to read it will result in zero bytes available.
Shouldn't the hint be something like ReadHint::hup()
?