[src]

Struct std::io::net::tcp::TcpListener

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.

Example

use std::io::net::tcp::TcpListener;
use std::io::net::ip::{Ipv4Addr, SocketAddr};
use std::io::{Acceptor, Listener};

let addr = SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 80 };
let listener = TcpListener::bind(addr);

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

// accept connections and process them
for stream in acceptor.incoming() {
    spawn(proc() {
        handle_client(stream);
    });
}

// close the socket server
drop(acceptor);

Methods

impl TcpListener

fn bind(addr: SocketAddr) -> IoResult<TcpListener>

Creates a new TcpListener which will be bound to the specified local socket 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.

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>

Spin up the listener and start queuing incoming connections

Error

Returns Err if this listener could not be bound to listen for connections. In all cases, this listener is consumed.