How to accept a client socket and then poll for the TcpStream in mio?

1.4k views Asked by At

I cannot figure out how to accept a client socket then start polling for events on the returned TcpStream with mio 0.6.

I tried this code, but an event for Token(1) never arrives. Although the accept part is printed out, the client is instantly disconnected.

loop {
    poll.poll(&mut events, None).unwrap();

    for event in events.iter() {
        match event.token() {
            Token(0) => {
                match server.accept() {
                    Ok((stream, addr)) => {
                        println!("{}: accept {}", stream.as_raw_fd(), addr);

                        stream.register(&poll, Token(1), Ready::readable(), PollOpt::edge() | PollOpt::oneshot()).unwrap();
                    }
                    Err(e) => {
                        println!("listener.accept() errored: {}", e);
                        return;
                    }
                }
            },
            Token(1) => {
                println!("event from 1")
            }
            _ => {
                unreachable!()
            }
        }
    }
}

I have no clue how to do this properly.

0

There are 0 answers