Struct async_std::os::unix::net::UnixListener

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

A Unix domain socket server, listening for connections.

After creating a UnixListener by binding it to a socket address, it listens for incoming 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.

This type is an async version of std::os::unix::net::UnixListener.

Examples

use async_std::os::unix::net::UnixListener;
use async_std::prelude::*;

let listener = UnixListener::bind("/tmp/socket").await?;
let mut incoming = listener.incoming();

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

Implementations§

source§

impl UnixListener

source

pub async fn bind<P: AsRef<Path>>(path: P) -> Result<UnixListener>

Creates a Unix datagram listener bound to the given path.

Examples
use async_std::os::unix::net::UnixListener;

let listener = UnixListener::bind("/tmp/socket").await?;
source

pub async fn accept(&self) -> Result<(UnixStream, 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::os::unix::net::UnixListener;

let listener = UnixListener::bind("/tmp/socket").await?;
let (socket, 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::os::unix::net::UnixListener;
use async_std::prelude::*;

let listener = UnixListener::bind("/tmp/socket").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 socket address of this listener.

Examples
use async_std::os::unix::net::UnixListener;

let listener = UnixListener::bind("/tmp/socket").await?;
let addr = listener.local_addr()?;

Trait Implementations§

source§

impl AsRawFd for UnixListener

source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
source§

impl Debug for UnixListener

source§

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

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

impl From<UnixListener> for UnixListener

source§

fn from(listener: StdUnixListener) -> UnixListener

Converts a std::os::unix::net::UnixListener into its asynchronous equivalent.

source§

impl FromRawFd for UnixListener

source§

unsafe fn from_raw_fd(fd: RawFd) -> UnixListener

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

impl IntoRawFd for UnixListener

source§

fn into_raw_fd(self) -> RawFd

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

impl TryFrom<UnixListener> for UnixListener

source§

fn try_from(listener: UnixListener) -> Result<StdUnixListener>

Converts a UnixListener 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 T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where 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.

source§

impl<T, U> Into<U> for T
where 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, U> TryFrom<U> for T
where 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 T
where 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.