Struct async_net::TcpListener

source ·
pub struct TcpListener { /* private fields */ }
Expand description

A TCP server, listening for connections.

After creating a TcpListener by binding it to an address, it listens for incoming TCP connections. These can be accepted by calling accept() or by awaiting items from the stream of incoming connections.

Cloning a TcpListener creates another handle to the same socket. The socket will be closed when all handles to it are dropped.

The Transmission Control Protocol is specified in IETF RFC 793.

Examples

use async_net::TcpListener;
use futures_lite::prelude::*;

let listener = TcpListener::bind("127.0.0.1:8080").await?;
let mut incoming = listener.incoming();

while let Some(stream) = incoming.next().await {
    let mut stream = stream?;
    stream.write_all(b"hello").await?;
}

Implementations§

source§

impl TcpListener

source

pub async fn bind<A: AsyncToSocketAddrs>(addr: A) -> Result<TcpListener>

Creates a new TcpListener bound to the given address.

Binding with a port number of 0 will request that the operating system assigns an available port to this listener. The assigned port can be queried via the local_addr() method.

If addr yields multiple addresses, binding will be attempted with each of the addresses until one succeeds and returns the listener. If none of the addresses succeed in creating a listener, the error from the last attempt is returned.

Examples

Create a TCP listener bound to 127.0.0.1:80:

use async_net::TcpListener;

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

Create a TCP listener bound to 127.0.0.1:80. If that address is unavailable, then try binding to 127.0.0.1:443:

use async_net::{SocketAddr, TcpListener};

let addrs = [
    SocketAddr::from(([127, 0, 0, 1], 80)),
    SocketAddr::from(([127, 0, 0, 1], 443)),
];
let listener = TcpListener::bind(&addrs[..]).await.unwrap();
source

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

Returns the local address this listener is bound to.

Examples

Bind to port 0 and then see which port was assigned by the operating system:

use async_net::{SocketAddr, TcpListener};

let listener = TcpListener::bind("127.0.0.1:0").await?;
println!("Listening on {}", listener.local_addr()?);
source

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

Accepts a new incoming connection.

Returns a TCP stream and the address it is connected to.

Examples
use async_net::TcpListener;

let listener = TcpListener::bind("127.0.0.1:8080").await?;
let (stream, addr) = listener.accept().await?;
source

pub fn incoming(&self) -> Incoming<'_>

Returns a stream of incoming connections.

Iterating over this stream is equivalent to calling accept() in a loop. The stream of connections is infinite, i.e awaiting the next connection will never result in None.

Examples
use async_net::TcpListener;
use futures_lite::prelude::*;

let listener = TcpListener::bind("127.0.0.1:0").await?;
let mut incoming = listener.incoming();

while let Some(stream) = incoming.next().await {
    let mut stream = stream?;
    stream.write_all(b"hello").await?;
}
source

pub fn ttl(&self) -> Result<u32>

Gets the value of the IP_TTL option for this socket.

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

Examples
use async_net::TcpListener;

let listener = TcpListener::bind("127.0.0.1:80").await?;
listener.set_ttl(100)?;
assert_eq!(listener.ttl()?, 100);
source

pub fn set_ttl(&self, ttl: u32) -> Result<()>

Sets the value of the IP_TTL option for this socket.

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

Examples
use async_net::TcpListener;

let listener = TcpListener::bind("127.0.0.1:80").await?;
listener.set_ttl(100)?;

Trait Implementations§

source§

impl AsRawSocket for TcpListener

source§

fn as_raw_socket(&self) -> RawSocket

Extracts the raw socket. Read more
source§

impl AsSocket for TcpListener

source§

fn as_socket(&self) -> BorrowedSocket<'_>

Borrows the socket.
source§

impl Clone for TcpListener

source§

fn clone(&self) -> TcpListener

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for TcpListener

source§

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

Formats the value using the given formatter. Read more
source§

impl From<Async<TcpListener>> for TcpListener

source§

fn from(listener: Async<TcpListener>) -> TcpListener

Converts to this type from the input type.
source§

impl From<TcpListener> for Arc<Async<TcpListener>>

source§

fn from(val: TcpListener) -> Self

Converts to this type from the input type.
source§

impl TryFrom<OwnedSocket> for TcpListener

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(value: OwnedSocket) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<TcpListener> for TcpListener

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(listener: TcpListener) -> Result<TcpListener>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> AsFd for Twhere T: AsSocket,

§

fn as_fd(&self) -> BorrowedSocket<'_>

An as_fd function for Winsock2, where a Fd is a Socket.
§

impl<T> AsRawFd for Twhere T: AsRawSocket,

§

fn as_raw_fd(&self) -> u32

A version of as_raw_fd for use with Winsock2 API.
§

impl<T> AsSource for Twhere T: AsSocket,

§

fn source(&self) -> BorrowedSocket<'_>

Returns the borrowed socket.
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more