Struct std::old_io::net::tcp::TcpListenerUnstable [-] [+] [src]

pub struct TcpListener {
    // some fields omitted
}

A structure representing a socket server. This listener is used to create a TcpAcceptor which can be used to accept sockets on a local port.

Examples

fn main() { fn foo() { use std::old_io::{TcpListener, TcpStream}; use std::old_io::{Acceptor, Listener}; use std::thread; let listener = TcpListener::bind("127.0.0.1:80").unwrap(); // bind the listener to the specified address let mut acceptor = listener.listen().unwrap(); fn handle_client(mut stream: TcpStream) { // ... &mut stream; // silence unused mutability/variable warning } // accept connections and process them, spawning a new tasks for each one for stream in acceptor.incoming() { match stream { Err(e) => { /* connection failed */ } Ok(stream) => { thread::spawn(move|| { // connection succeeded handle_client(stream) }); } } } // close the socket server drop(acceptor); } }
use std::old_io::{TcpListener, TcpStream};
use std::old_io::{Acceptor, Listener};
use std::thread;

let listener = TcpListener::bind("127.0.0.1:80").unwrap();

// bind the listener to the specified address
let mut acceptor = listener.listen().unwrap();

fn handle_client(mut stream: TcpStream) {
    // ...
}
// accept connections and process them, spawning a new tasks for each one
for stream in acceptor.incoming() {
    match stream {
        Err(e) => { /* connection failed */ }
        Ok(stream) => {
            thread::spawn(move|| {
                // connection succeeded
                handle_client(stream)
            });
        }
    }
}

// close the socket server
drop(acceptor);

Methods

impl TcpListener

fn bind<A: ToSocketAddr>(addr: A) -> IoResult<TcpListener>

Creates a new TcpListener which will be bound to the specified address. This listener is not ready for accepting connections, listen must be called on it before that's possible.

Binding with a port number of 0 will request that the OS assigns a port to this listener. The port allocated can be queried via the socket_name function.

The address type can be any implementer of ToSocketAddr trait. See its documentation for concrete examples.

fn socket_name(&mut self) -> IoResult<SocketAddr>

Returns the local socket address of this listener.

Trait Implementations

impl Listener<TcpStream, TcpAcceptor> for TcpListener

fn listen(self) -> IoResult<TcpAcceptor>

impl AsRawFd for TcpListener

fn as_raw_fd(&self) -> Fd

impl AsRawFd for TcpListener

fn as_raw_fd(&self) -> Fd