# Struct bitflags::__core::net::TcpListener [−] [src]

    pub struct TcpListener(_);

1.0.0

A structure representing a socket server.

# Examples

    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>(addr: A) -> Result<TcpListener, Error> where A: ToSocketAddrs`

Creates a new `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 `local_addr` method.

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

#### `fn local_addr(&self) -> Result<SocketAddr, Error>`

Returns the local socket address of this listener.

#### `fn try_clone(&self) -> Result<TcpListener, Error>`

Creates a new independently owned handle to the underlying socket.

The returned `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), Error>`

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` 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` and will also not yield the peer's `SocketAddr` structure.

#### `fn set_ttl(&self, ttl: u32) -> Result<(), Error>`1.9.0

Sets the value for the `IP_TTL` option on this socket.

This value sets the time-to-live field that is used in every packet sent from this socket.

#### `fn ttl(&self) -> Result<u32, Error>`1.9.0

Gets the value of the `IP_TTL` option for this socket.

For more information about this option, see `set_ttl`.

#### `fn set_only_v6(&self, only_v6: bool) -> Result<(), Error>`1.9.0

Sets the value for the `IPV6_V6ONLY` option on this socket.

If this is set to `true` then the socket is restricted to sending and receiving IPv6 packets only. In this case two IPv4 and IPv6 applications can bind the same port at the same time.

If this is set to `false` then the socket can be used to send and receive packets from an IPv4-mapped IPv6 address.

#### `fn only_v6(&self) -> Result<bool, Error>`1.9.0

Gets the value of the `IPV6_V6ONLY` option for this socket.

For more information about this option, see `set_only_v6`.

#### `fn take_error(&self) -> Result<Option<Error>, Error>`1.9.0

Get the value of the `SO_ERROR` option on this socket.

This will retrieve the stored error in the underlying socket, clearing the field in the process. This can be useful for checking errors between calls.

#### `fn set_nonblocking(&self, nonblocking: bool) -> Result<(), Error>`1.9.0

Moves this TCP stream into or out of nonblocking mode.

On Unix this corresponds to calling fcntl, and on Windows this corresponds to calling ioctlsocket.

## Trait Implementations

### `impl Debug for TcpListener`

#### `fn fmt(&self, f: &mut Formatter) -> Result<(), Error>`

### `impl AsRawFd for TcpListener`

#### `fn as_raw_fd(&self) -> i32`

### `impl FromRawFd for TcpListener` 1.1.0

#### `unsafe fn from_raw_fd(fd: i32) -> TcpListener`

### `impl IntoRawFd for TcpListener` 1.4.0

#### `fn into_raw_fd(self) -> i32`

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)