pub struct TcpListener { /* private fields */ }Expand description
Async TCP listening socket.
A listener accepts inbound TcpStream connections from a bound local
address. Use TcpListener::accept for one connection at a time or
TcpListener::incoming for a Stream adapter.
Implementations§
Source§impl TcpListener
impl TcpListener
Sourcepub fn from_std(listener: TcpListener) -> Result<Self>
Available on Unix only.
pub fn from_std(listener: TcpListener) -> Result<Self>
Adopts a blocking std::net::TcpListener, returning an async
TcpListener.
The socket is switched to non-blocking mode. Ownership of the descriptor transfers to the returned listener.
Source§impl TcpListener
impl TcpListener
Sourcepub async fn bind<A>(addr: A) -> Result<Self>where
A: ToSocketAddrs + Send + 'static,
pub async fn bind<A>(addr: A) -> Result<Self>where
A: ToSocketAddrs + Send + 'static,
Binds a TCP listener to the first resolved address that succeeds.
Binding to port 0 asks the OS to assign an available port, which can be
retrieved with local_addr.
Like std::net::TcpListener::bind, this does not set SO_REUSEADDR.
To reuse a recently-closed address (or bind while a previous socket is in
TIME_WAIT), configure a TcpSocket and enable it before binding:
runite::spawn(async {
let socket = runite::net::TcpSocket::new_v4().unwrap();
socket.set_reuseaddr(true).unwrap();
socket.bind("127.0.0.1:0".parse().unwrap()).unwrap();
let listener = socket.listen(1024).unwrap();
assert!(listener.local_addr().unwrap().port() != 0);
});
runite::run();§Examples
runite::spawn(async {
let listener = runite::net::TcpListener::bind("127.0.0.1:0")
.await
.unwrap();
assert!(listener.local_addr().unwrap().port() != 0);
});
runite::run();Sourcepub async fn accept(&self) -> Result<(TcpStream, SocketAddr)>
pub async fn accept(&self) -> Result<(TcpStream, SocketAddr)>
Accepts an incoming connection.
The returned address is the peer address reported by the operating system for the accepted stream.
Sourcepub fn incoming(&self) -> Incoming
pub fn incoming(&self) -> Incoming
Returns a Stream that yields inbound connections as they arrive.
The stream is infinite: it never yields None. Each item is the result
of an accept, so transient errors surface as Some(Err(_)) without
ending iteration.
Sourcepub fn local_addr(&self) -> Result<SocketAddr>
pub fn local_addr(&self) -> Result<SocketAddr>
Returns the local socket address of this listener.
Trait Implementations§
Source§impl AsFd for TcpListener
Available on Unix only.
impl AsFd for TcpListener
Source§fn as_fd(&self) -> BorrowedFd<'_>
fn as_fd(&self) -> BorrowedFd<'_>
Source§impl AsRawFd for TcpListener
Available on Unix only.
impl AsRawFd for TcpListener
Source§impl Debug for TcpListener
impl Debug for TcpListener
Source§impl From<OwnedFd> for TcpListener
Available on Unix only.
impl From<OwnedFd> for TcpListener
Source§fn from(fd: OwnedFd) -> Self
fn from(fd: OwnedFd) -> Self
Adopts an already-listening TCP socket. Use TcpListener::from_std to
adopt a std::net::TcpListener and set the expected mode.