pub struct UnixDatagram { /* private fields */ }
Expand description
A Unix datagram socket.
After creating a UnixDatagram
by bind
ing it to a path, data can be sent to and
received from any other socket address.
This type is an async version of std::os::unix::net::UnixDatagram
.
Examples
use async_std::os::unix::net::UnixDatagram;
let socket = UnixDatagram::bind("/tmp/socket1").await?;
socket.send_to(b"hello world", "/tmp/socket2").await?;
let mut buf = vec![0u8; 1024];
let (n, peer) = socket.recv_from(&mut buf).await?;
Implementations
sourceimpl UnixDatagram
impl UnixDatagram
sourcepub async fn bind<P: AsRef<Path>>(path: P) -> Result<UnixDatagram>
pub async fn bind<P: AsRef<Path>>(path: P) -> Result<UnixDatagram>
Creates a Unix datagram socket bound to the given path.
Examples
use async_std::os::unix::net::UnixDatagram;
let socket = UnixDatagram::bind("/tmp/socket").await?;
sourcepub fn unbound() -> Result<UnixDatagram>
pub fn unbound() -> Result<UnixDatagram>
Creates a Unix datagram which is not bound to any address.
Examples
use async_std::os::unix::net::UnixDatagram;
let socket = UnixDatagram::unbound()?;
sourcepub fn pair() -> Result<(UnixDatagram, UnixDatagram)>
pub fn pair() -> Result<(UnixDatagram, UnixDatagram)>
Creates an unnamed pair of connected sockets.
Returns two sockets which are connected to each other.
Examples
use async_std::os::unix::net::UnixDatagram;
let (socket1, socket2) = UnixDatagram::pair()?;
sourcepub async fn connect<P: AsRef<Path>>(&self, path: P) -> Result<()>
pub async fn connect<P: AsRef<Path>>(&self, path: P) -> Result<()>
Connects the socket to the specified address.
The send
method may be used to send data to the specified address. recv
and
recv_from
will only receive data from that address.
Examples
use async_std::os::unix::net::UnixDatagram;
let socket = UnixDatagram::unbound()?;
socket.connect("/tmp/socket").await?;
sourcepub fn local_addr(&self) -> Result<SocketAddr>
pub fn local_addr(&self) -> Result<SocketAddr>
Returns the address of this socket.
Examples
use async_std::os::unix::net::UnixDatagram;
let socket = UnixDatagram::bind("/tmp/socket").await?;
let addr = socket.local_addr()?;
sourcepub fn peer_addr(&self) -> Result<SocketAddr>
pub fn peer_addr(&self) -> Result<SocketAddr>
sourcepub async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)>
pub async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)>
Receives data from the socket.
On success, returns the number of bytes read and the address from where the data came.
Examples
use async_std::os::unix::net::UnixDatagram;
let socket = UnixDatagram::unbound()?;
let mut buf = vec![0; 1024];
let (n, peer) = socket.recv_from(&mut buf).await?;
sourcepub async fn recv(&self, buf: &mut [u8]) -> Result<usize>
pub async fn recv(&self, buf: &mut [u8]) -> Result<usize>
Receives data from the socket.
On success, returns the number of bytes read.
Examples
use async_std::os::unix::net::UnixDatagram;
let socket = UnixDatagram::bind("/tmp/socket").await?;
let mut buf = vec![0; 1024];
let n = socket.recv(&mut buf).await?;
sourcepub async fn send_to<P: AsRef<Path>>(
&self,
buf: &[u8],
path: P
) -> Result<usize>
pub async fn send_to<P: AsRef<Path>>(
&self,
buf: &[u8],
path: P
) -> Result<usize>
Sends data on the socket to the specified address.
On success, returns the number of bytes written.
Examples
use async_std::os::unix::net::UnixDatagram;
let socket = UnixDatagram::unbound()?;
socket.send_to(b"hello world", "/tmp/socket").await?;
sourcepub async fn send(&self, buf: &[u8]) -> Result<usize>
pub async fn send(&self, buf: &[u8]) -> Result<usize>
Sends data on the socket to the socket’s peer.
On success, returns the number of bytes written.
Examples
use async_std::os::unix::net::UnixDatagram;
let socket = UnixDatagram::unbound()?;
socket.connect("/tmp/socket").await?;
socket.send(b"hello world").await?;
sourcepub fn shutdown(&self, how: Shutdown) -> Result<()>
pub fn shutdown(&self, how: Shutdown) -> Result<()>
Shut down the read, write, or both halves of this connection.
This function will cause all pending and future I/O calls on the specified portions to
immediately return with an appropriate value (see the documentation of Shutdown
).
Examples
use async_std::os::unix::net::UnixDatagram;
use std::net::Shutdown;
let socket = UnixDatagram::unbound()?;
socket.shutdown(Shutdown::Both)?;
Trait Implementations
sourceimpl AsRawFd for UnixDatagram
impl AsRawFd for UnixDatagram
sourceimpl Debug for UnixDatagram
impl Debug for UnixDatagram
sourceimpl From<UnixDatagram> for UnixDatagram
impl From<UnixDatagram> for UnixDatagram
sourcefn from(datagram: StdUnixDatagram) -> UnixDatagram
fn from(datagram: StdUnixDatagram) -> UnixDatagram
Converts a std::os::unix::net::UnixDatagram
into its asynchronous equivalent.
sourceimpl FromRawFd for UnixDatagram
impl FromRawFd for UnixDatagram
sourceunsafe fn from_raw_fd(fd: RawFd) -> UnixDatagram
unsafe fn from_raw_fd(fd: RawFd) -> UnixDatagram
Constructs a new instance of Self
from the given raw file
descriptor. Read more
sourceimpl IntoRawFd for UnixDatagram
impl IntoRawFd for UnixDatagram
sourcefn into_raw_fd(self) -> RawFd
fn into_raw_fd(self) -> RawFd
Consumes this object, returning the raw underlying file descriptor. Read more
Auto Trait Implementations
impl RefUnwindSafe for UnixDatagram
impl Send for UnixDatagram
impl Sync for UnixDatagram
impl Unpin for UnixDatagram
impl UnwindSafe for UnixDatagram
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