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()
?
The default when calling
register
on mio v0.3.5 is to register only for thereadable
Interest, so that is the only hint you will get.If you want to be warned for hup as well, you need to use the function register_opt and give your
Interest
andPollOpt
as parameters, so your code becomes:I think the default for the convenience function
register
has changed in v0.4.0 to beInterest::all()
so this shouldn't be a problem in the future.