# [−][src]Struct mio::net::TcpListener

    pub struct TcpListener { /* fields omitted */ }

A structure representing a socket server

# Examples

    use mio::{Events, Ready, Poll, PollOpt, Token};
    use mio::net::TcpListener;
    use std::time::Duration;

    let listener = TcpListener::bind(&"127.0.0.1:34255".parse()?)?;

    let poll = Poll::new()?;
    let mut events = Events::with_capacity(128);

    // Register the socket with `Poll`
    poll.register(&listener, Token(0), Ready::readable(),
                  PollOpt::edge())?;

    poll.poll(&mut events, Some(Duration::from_millis(100)))?;

    // There may be a socket ready to be accepted

## Methods

### `impl TcpListener`[src]

#### `pub fn bind(addr: &SocketAddr) -> Result<TcpListener>`[src]

Convenience method to bind a new TCP listener to the specified address to receive new connections.

This function will take the following steps:

1. Create a new TCP socket.
2. Set the `SO_REUSEADDR` option on the socket.
3. Bind the socket to the specified address.
4. Call `listen` on the socket to prepare it to receive new connections.

If fine-grained control over the binding and listening process for a socket is desired then the `net2::TcpBuilder` methods can be used in combination with the `TcpListener::from_listener` method to transfer ownership into mio.

#### `pub fn from_std(listener: TcpListener) -> Result<TcpListener>`[src]

Creates a new `TcpListener` from an instance of a `std::net::TcpListener` type.

This function will set the `listener` provided into nonblocking mode on Unix, and otherwise the stream will just be wrapped up in an mio stream ready to accept new connections and become associated with an event loop.

The address provided must be the address that the listener is bound to.

#### `pub fn accept(&self) -> Result<(TcpStream, SocketAddr)>`[src]

Accepts a new `TcpStream`.

This may return an `Err(e)` where `e.kind()` is `io::ErrorKind::WouldBlock`. This means a stream may be ready at a later point and one should wait for a notification before calling `accept` again.

If an accepted stream is returned, the remote address of the peer is returned along with it.

#### `pub fn accept_std(&self) -> Result<(TcpStream, SocketAddr)>`[src]

Accepts a new `std::net::TcpStream`.

This method is the same as `accept`, except that it returns a TCP socket in blocking mode which isn't bound to `mio`. This can be later then converted to a `mio` type, if necessary.

#### `pub fn local_addr(&self) -> Result<SocketAddr>`[src]

Returns the local socket address of this listener.

#### `pub fn try_clone(&self) -> Result<TcpListener>`[src]

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.

#### `pub fn set_ttl(&self, ttl: u32) -> Result<()>`[src]

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.

#### `pub fn ttl(&self) -> Result<u32>`[src]

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

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

#### `pub fn set_only_v6(&self, only_v6: bool) -> Result<()>`[src]

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.

#### `pub fn only_v6(&self) -> Result<bool>`[src]

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

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

#### `pub fn take_error(&self) -> Result<Option<Error>>`[src]

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.

## Trait Implementations

### `impl Evented for TcpListener`[src]

#### `fn register(
&self,
poll: &Poll,
token: Token,
interest: Ready,
opts: PollOpt
) -> Result<()>`[src]

Register `self` with the given `Poll` instance. Read more

#### `fn reregister(
&self,
poll: &Poll,
token: Token,
interest: Ready,
opts: PollOpt
) -> Result<()>`[src]

Re-register `self` with the given `Poll` instance. Read more

#### `fn deregister(&self, poll: &Poll) -> Result<()>`[src]

Deregister `self` from the given `Poll` instance Read more

### `impl Debug for TcpListener`[src]

#### `fn fmt(&self, f: &mut Formatter) -> Result`[src]

Formats the value using the given formatter. Read more

### `impl IntoRawFd for TcpListener`[src]

#### `fn into_raw_fd(self) -> RawFd`[src]

Consumes this object, returning the raw underlying file descriptor. Read more

### `impl AsRawFd for TcpListener`[src]

#### `fn as_raw_fd(&self) -> RawFd`[src]

Extracts the raw file descriptor. Read more

### `impl FromRawFd for TcpListener`[src]

#### `unsafe fn from_raw_fd(fd: RawFd) -> TcpListener`[src]

Constructs a new instance of `Self` from the given raw file descriptor. Read more

## Auto Trait Implementations

### `impl Send for TcpListener`

### `impl Unpin for TcpListener`

### `impl Sync for TcpListener`

### `impl UnwindSafe for TcpListener`

### `impl RefUnwindSafe for TcpListener`

## Blanket Implementations

### `impl<T, U> Into<U> for T where
U: From<T>,` [src]

#### `fn into(self) -> U`[src]

Performs the conversion.

### `impl<T> From<T> for T`[src]

#### `fn from(t: T) -> T`[src]

Performs the conversion.

### `impl<T, U> TryFrom<U> for T where
U: Into<T>,` [src]

#### `type Error = Infallible`

The type returned in the event of a conversion error.

#### `fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>`[src]

Performs the conversion.

### `impl<T, U> TryInto<U> for T where
U: TryFrom<T>,` [src]

#### `type Error = <U as TryFrom<T>>::Error`

The type returned in the event of a conversion error.

#### `fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>`[src]

Performs the conversion.

### `impl<T> BorrowMut<T> for T where
T: ?Sized,` [src]

####
ⓘImportant traits for &'_ mut W

### Important traits for &'_ mut W

`impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized,`
`fn borrow_mut(&mut self) -> &mut T`[src]

Mutably borrows from an owned value. Read more

### `impl<T> Borrow<T> for T where
T: ?Sized,` [src]

####
ⓘImportant traits for &'_ mut W

### Important traits for &'_ mut W

`impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized,`
`fn borrow(&self) -> &T`[src]

Immutably borrows from an owned value. Read more

### `impl<T> Any for T where
T: 'static + ?Sized,` [src]

#### `fn type_id(&self) -> TypeId`[src]

Gets the `TypeId` of `self`. Read more

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## 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`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)