Struct std::io::net::tcp::TcpAcceptor[src]

pub struct TcpAcceptor {
    // some fields omitted
}

The accepting half of a TCP socket server. This structure is created through a TcpListener's listen method, and this object can be used to accept new TcpStream instances.

Methods

impl TcpAcceptor

fn set_timeout(&mut self, ms: Option<u64>)

Prevents blocking on all future accepts after ms milliseconds have elapsed.

This function is used to set a deadline after which this acceptor will time out accepting any connections. The argument is the relative distance, in milliseconds, to a point in the future after which all accepts will fail.

If the argument specified is None, then any previously registered timeout is cleared.

A timeout of 0 can be used to "poll" this acceptor to see if it has any pending connections. All pending connections will be accepted, regardless of whether the timeout has expired or not (the accept will not block in this case).

Example

fn main() { #![allow(experimental)] use std::io::TcpListener; use std::io::{Listener, Acceptor, TimedOut}; let mut a = TcpListener::bind("127.0.0.1", 8482).listen().unwrap(); // After 100ms have passed, all accepts will fail a.set_timeout(Some(100)); match a.accept() { Ok(..) => println!("accepted a socket"), Err(ref e) if e.kind == TimedOut => { println!("timed out!"); } Err(e) => println!("err: {}", e), } // Reset the timeout and try again a.set_timeout(Some(100)); let socket = a.accept(); // Clear the timeout and block indefinitely waiting for a connection a.set_timeout(None); let socket = a.accept(); }
use std::io::TcpListener;
use std::io::{Listener, Acceptor, TimedOut};

let mut a = TcpListener::bind("127.0.0.1", 8482).listen().unwrap();

// After 100ms have passed, all accepts will fail
a.set_timeout(Some(100));

match a.accept() {
    Ok(..) => println!("accepted a socket"),
    Err(ref e) if e.kind == TimedOut => { println!("timed out!"); }
    Err(e) => println!("err: {}", e),
}

// Reset the timeout and try again
a.set_timeout(Some(100));
let socket = a.accept();

// Clear the timeout and block indefinitely waiting for a connection
a.set_timeout(None);
let socket = a.accept();

Trait Implementations

impl Acceptor<TcpStream> for TcpAcceptor

fn accept(&mut self) -> IoResult<TcpStream>

fn incoming<'r>(&'r mut self) -> IncomingConnections<'r, Self>