Struct async_std::net::TcpListener

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

A TCP socket server, listening for connections.

After creating a TcpListener by binding it to a socket address, it listens for incoming TCP connections. These can be accepted by awaiting elements from the async stream of incoming connections.

The socket will be closed when the value is dropped.

The Transmission Control Protocol is specified in IETF RFC 793.

This type is an async version of std::net::TcpListener.

Examples

use async_std::io;
use async_std::net::TcpListener;
use async_std::prelude::*;

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

while let Some(stream) = incoming.next().await {
    let stream = stream?;
    let (reader, writer) = &mut (&stream, &stream);
    io::copy(reader, writer).await?;
}

Implementations§

source§

impl TcpListener

source

pub async fn bind<A: ToSocketAddrs>(addrs: A) -> Result<TcpListener>

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.

Examples

Create a TCP listener bound to 127.0.0.1:0:

use async_std::net::TcpListener;

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

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

Accepts a new incoming connection to this listener.

When a connection is established, the corresponding stream and address will be returned.

Examples
use async_std::net::TcpListener;

let listener = TcpListener::bind("127.0.0.1:0").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_std::net::TcpListener;
use async_std::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 world").await?;
}
source

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

Returns the local address that this listener is bound to.

This can be useful, for example, to identify when binding to port 0 which port was assigned by the OS.

Examples
use async_std::net::TcpListener;

let listener = TcpListener::bind("127.0.0.1:8080").await?;
let addr = listener.local_addr()?;

Trait Implementations§

source§

impl AsRawFd for TcpListener

source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. 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<TcpListener> for TcpListener

source§

fn from(listener: TcpListener) -> TcpListener

Converts a std::net::TcpListener into its asynchronous equivalent.

source§

impl FromRawFd for TcpListener

source§

unsafe fn from_raw_fd(fd: RawFd) -> TcpListener

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

impl IntoRawFd for TcpListener

source§

fn into_raw_fd(self) -> RawFd

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

impl TryFrom<TcpListener> for TcpListener

source§

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

Converts a TcpListener into its synchronous equivalent.

§

type Error = Error

The type returned in the event of a conversion error.

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> AsRawFilelike for Twhere T: AsRawFd,

§

fn as_raw_filelike(&self) -> i32

Returns the raw value.
§

impl<T> AsRawSocketlike for Twhere T: AsRawFd,

§

fn as_raw_socketlike(&self) -> i32

Returns the raw value.
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> FromRawFilelike for Twhere T: FromRawFd,

§

unsafe fn from_raw_filelike(raw: i32) -> T

Constructs Self from the raw value. Read more
§

impl<T> FromRawSocketlike for Twhere T: FromRawFd,

§

unsafe fn from_raw_socketlike(raw: i32) -> T

Constructs Self from the raw value. Read more
source§

impl<T> Instrument for T

source§

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

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

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.

§

impl<T> IntoRawFilelike for Twhere T: IntoRawFd,

§

fn into_raw_filelike(self) -> i32

Returns the raw value.
§

impl<T> IntoRawSocketlike for Twhere T: IntoRawFd,

§

fn into_raw_socketlike(self) -> i32

Returns the raw value.
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.
source§

impl<T> WithSubscriber for T

source§

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
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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