pub struct UnixListener { /* private fields */ }
Expand description
A Unix server, listening for connections.
After creating a UnixListener
by bind
ing it to an address, it
listens for incoming connections. These can be accepted by calling
accept()
or by awaiting items from the async stream of
incoming
connections.
Cloning a UnixListener
creates another handle to the same socket. The socket will be closed
when all handles to it are dropped.
Examples
use async_net::unix::UnixListener;
use futures_lite::prelude::*;
let listener = UnixListener::bind("/tmp/socket")?;
let mut incoming = listener.incoming();
while let Some(stream) = incoming.next().await {
let mut stream = stream?;
stream.write_all(b"hello").await?;
}
Implementations
sourceimpl UnixListener
impl UnixListener
sourcepub fn bind<P>(path: P) -> Result<UnixListener, Error> where
P: AsRef<Path>,
pub fn bind<P>(path: P) -> Result<UnixListener, Error> where
P: AsRef<Path>,
Creates a new UnixListener
bound to the given path.
Examples
use async_net::unix::UnixListener;
use futures_lite::prelude::*;
let listener = UnixListener::bind("/tmp/socket")?;
let mut incoming = listener.incoming();
while let Some(stream) = incoming.next().await {
let mut stream = stream?;
stream.write_all(b"hello").await?;
}
sourcepub async fn accept(&'_ self) -> Result<(UnixStream, SocketAddr), Error>
pub async fn accept(&'_ self) -> Result<(UnixStream, SocketAddr), Error>
Accepts a new incoming connection.
Returns a TCP stream and the address it is connected to.
Examples
use async_net::unix::UnixListener;
let listener = UnixListener::bind("/tmp/socket")?;
let (stream, addr) = listener.accept().await?;
sourcepub fn incoming(&self) -> Incoming<'_>
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::unix::UnixListener;
use futures_lite::prelude::*;
let listener = UnixListener::bind("/tmp/socket")?;
let mut incoming = listener.incoming();
while let Some(stream) = incoming.next().await {
let mut stream = stream?;
stream.write_all(b"hello").await?;
}
sourcepub fn local_addr(&self) -> Result<SocketAddr, Error>
pub fn local_addr(&self) -> Result<SocketAddr, Error>
Returns the local address this listener is bound to.
Examples
use async_net::unix::UnixListener;
let listener = UnixListener::bind("/tmp/socket")?;
println!("Local address is {:?}", listener.local_addr()?);
Trait Implementations
sourceimpl AsRawFd for UnixListener
impl AsRawFd for UnixListener
sourceimpl Clone for UnixListener
impl Clone for UnixListener
sourcefn clone(&self) -> UnixListener
fn clone(&self) -> UnixListener
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for UnixListener
impl Debug for UnixListener
sourceimpl From<Async<UnixListener>> for UnixListener
impl From<Async<UnixListener>> for UnixListener
sourcefn from(listener: Async<UnixListener>) -> UnixListener
fn from(listener: Async<UnixListener>) -> UnixListener
Performs the conversion.
sourceimpl TryFrom<UnixListener> for UnixListener
impl TryFrom<UnixListener> for UnixListener
sourcefn try_from(listener: UnixListener) -> Result<UnixListener, Error>
fn try_from(listener: UnixListener) -> Result<UnixListener, Error>
Performs the conversion.
Auto Trait Implementations
impl RefUnwindSafe for UnixListener
impl Send for UnixListener
impl Sync for UnixListener
impl Unpin for UnixListener
impl UnwindSafe for UnixListener
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more