Struct std::net::TcpListener [] [src]

pub struct TcpListener(_);

A structure representing a socket server.

Examples

fn main() { use std::net::{TcpListener, TcpStream}; use std::thread; let listener = TcpListener::bind("127.0.0.1:80").unwrap(); fn handle_client(stream: TcpStream) { // ... } // accept connections and process them, spawning a new thread for each one for stream in listener.incoming() { match stream { Ok(stream) => { thread::spawn(move|| { // connection succeeded handle_client(stream) }); } Err(e) => { /* connection failed */ } } } // close the socket server drop(listener); }
use std::net::{TcpListener, TcpStream};
use std::thread;

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

fn handle_client(stream: TcpStream) {
    // ...
}

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

// close the socket server
drop(listener);

Methods

impl TcpListener

fn bind<A: ToSocketAddrs>(addr: A) -> Result<TcpListener>

Creates a new TcpListener`TcpListener` which will be bound to the specified address.

The returned listener is ready for accepting connections.

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_addr`socket_addr` function.

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

fn local_addr(&self) -> Result<SocketAddr>

Returns the local socket address of this listener.

fn try_clone(&self) -> Result<TcpListener>

Creates a new independently owned handle to the underlying socket.

The returned TcpListener`TcpListener` is a reference to the same socket that this object references. Both handles can be used to accept incoming connections and options set on one listener will affect the other.

fn accept(&self) -> Result<(TcpStream, SocketAddr)>

Accept a new incoming connection from this listener.

This function will block the calling thread until a new TCP connection is established. When established, the corresponding TcpStream`TcpStream` and the remote peer's address will be returned.

fn incoming(&self) -> Incoming

Returns an iterator over the connections being received on this listener.

The returned iterator will never return None`Noneand will also not yield the peer's` and will also not yield the peer's SocketAddr`SocketAddr` structure.

Trait Implementations

impl Debug for TcpListener

fn fmt(&self, f: &mut Formatter) -> Result

impl AsRawFd for TcpListener

fn as_raw_fd(&self) -> RawFd

impl FromRawFd for TcpListener

unsafe fn from_raw_fd(fd: RawFd) -> TcpListener

impl AsRawFd for TcpListener

fn as_raw_fd(&self) -> RawFd

impl FromRawFd for TcpListener

unsafe fn from_raw_fd(fd: RawFd) -> TcpListener