Struct async_std::net::TcpListener [−][src]
A TCP socket server, listening for connections.
After creating a TcpListener
by bind
ing 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
impl TcpListener
[src]
pub async fn bind<A: ToSocketAddrs>(addrs: A) -> Result<TcpListener>
[src]
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?;
pub async fn accept(&self) -> Result<(TcpStream, SocketAddr)>
[src]
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?;
pub fn incoming(&self) -> Incoming<'_>
[src]
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?; }
pub fn local_addr(&self) -> Result<SocketAddr>
[src]
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
impl AsRawFd for TcpListener
[src]
impl Debug for TcpListener
[src]
impl From<TcpListener> for TcpListener
[src]
fn from(listener: TcpListener) -> TcpListener
[src]
Converts a std::net::TcpListener
into its asynchronous equivalent.
impl FromRawFd for TcpListener
[src]
unsafe fn from_raw_fd(fd: RawFd) -> TcpListener
[src]
impl IntoRawFd for TcpListener
[src]
fn into_raw_fd(self) -> RawFd
[src]
Auto Trait Implementations
impl RefUnwindSafe for TcpListener
impl Send for TcpListener
impl Sync for TcpListener
impl Unpin for TcpListener
impl UnwindSafe for TcpListener
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut Tⓘ
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,