1

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.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Midiparse
  • 4,701
  • 7
  • 28
  • 48
  • What's wrong with [the example posted in the documentation](https://docs.rs/mio/0.6.4/mio/), which appears to do the thing you want to do? – Shepmaster Jan 30 '17 at 18:22
  • It does basically the same thing just that it creates a client as well. But it says in the comment, that the example server drops the socket immediately, which I think is the same thing happening here, and I don't know who to leave it open. I am trying to create a "simple" concurrent TCP echo server. – Midiparse Jan 30 '17 at 20:39
  • I found an implementation [here](https://github.com/tigranbs/libuv-vs-rustmio/blob/master/tcp_mio/src/main.rs). – Midiparse Jan 30 '17 at 21:22
  • 1
    *I am trying to create a "simple" concurrent TCP echo server* — You probably want to look at [Tokio](https://tokio.rs/) instead, as that's the current forerunner in the Rust world. – Shepmaster Jan 30 '17 at 21:25

0 Answers0