Is it possible to register multiple sources with a single MIO Poll?

use mio::{Events, Poll, Interest, Token};
use mio::net::TcpStream;
use std::net::SocketAddr;
use std::time::{Duration, Instant};

let mut poll = Poll::new()?;

let address: SocketAddr = "".parse()?;
let listener = net::TcpListener::bind(address)?;
let mut socket = TcpStream::connect(listener.local_addr()?)?;

let address2: SocketAddr = "".parse()?;
let listener2 = net::TcpListener::bind(address2)?;
let mut socket2 = TcpStream::connect(listener2.local_addr()?)?;

// Register the first socket with `poll`
    &mut socket,
    Token(0), // zeroth socket
    Interest::READABLE | Interest::WRITABLE)?;

// Register the second socket with `poll`
    &mut socket2,
    Token(1), // first socket
    Interest::READABLE | Interest::WRITABLE)?;

let mut events = Events::with_capacity(1024);
let start = Instant::now();
let timeout = Duration::from_millis(500);

loop {
    let elapsed = start.elapsed();

    if elapsed >= timeout {
        // Connection timed out
        return Ok(());

    let remaining = timeout - elapsed;
    poll.poll(&mut events, Some(remaining))?;

    for event in &events {
        if event.token() == Token(0) {
            // Event received on socket 0
        } else if event.token() == Token(1) {
            // Event received on socket 1

I want to establish an event loop that polls multiple sockets. I need to know if this is possible using MIO. Ideally, MIO would poll the Poll, and thereafter, return events from each.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.