Struct async_net::unix::UnixDatagram
source · [−]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.
Cloning a UnixDatagram
creates another handle to the same socket. The socket will be closed
when all handles to it are dropped. The reading and writing portions of the socket can also be
shut down individually with the shutdown()
method.
Examples
use async_net::unix::UnixDatagram;
let socket = UnixDatagram::bind("/tmp/socket1")?;
socket.send_to(b"hello", "/tmp/socket2").await?;
let mut buf = vec![0u8; 1024];
let (n, addr) = socket.recv_from(&mut buf).await?;
Implementations
sourceimpl UnixDatagram
impl UnixDatagram
sourcepub fn bind<P: AsRef<Path>>(path: P) -> Result<UnixDatagram>
pub fn bind<P: AsRef<Path>>(path: P) -> Result<UnixDatagram>
Creates a new UnixDatagram
bound to the given address.
Examples
use async_net::unix::UnixDatagram;
let socket = UnixDatagram::bind("/tmp/socket")?;
sourcepub fn unbound() -> Result<UnixDatagram>
pub fn unbound() -> Result<UnixDatagram>
Creates a Unix datagram socket not bound to any address.
Examples
use async_net::unix::UnixDatagram;
let socket = UnixDatagram::unbound()?;
sourcepub fn pair() -> Result<(UnixDatagram, UnixDatagram)>
pub fn pair() -> Result<(UnixDatagram, UnixDatagram)>
Creates a pair of connected Unix datagram sockets.
Examples
use async_net::unix::UnixDatagram;
let (socket1, socket2) = UnixDatagram::pair()?;
sourcepub fn connect<P: AsRef<Path>>(&self, path: P) -> Result<()>
pub fn connect<P: AsRef<Path>>(&self, path: P) -> Result<()>
Connects the Unix datagram socket to the given address.
When connected, methods send()
and
recv()
will use the specified address for sending and receiving
messages. Additionally, a filter will be applied to
recv_from()
so that it only receives messages from that
same address.
Examples
use async_net::unix::UnixDatagram;
let socket = UnixDatagram::unbound()?;
socket.connect("/tmp/socket")?;
sourcepub fn local_addr(&self) -> Result<SocketAddr>
pub fn local_addr(&self) -> Result<SocketAddr>
Returns the local address this socket is bound to.
Examples
use async_net::unix::UnixDatagram;
let socket = UnixDatagram::bind("/tmp/socket")?;
println!("Bound to {:?}", socket.local_addr()?);
sourcepub fn peer_addr(&self) -> Result<SocketAddr>
pub fn peer_addr(&self) -> Result<SocketAddr>
Returns the remote address this socket is connected to.
Examples
use async_net::unix::UnixDatagram;
let socket = UnixDatagram::unbound()?;
socket.connect("/tmp/socket")?;
println!("Connected to {:?}", socket.peer_addr()?);
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 an address.
On success, returns the number of bytes received and the address data came from.
Examples
use async_net::unix::UnixDatagram;
let socket = UnixDatagram::bind("/tmp/socket")?;
let mut buf = vec![0; 1024];
let (n, addr) = socket.recv_from(&mut buf).await?;
println!("Received {} bytes from {:?}", n, addr);
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 to the given address.
On success, returns the number of bytes sent.
Examples
use async_net::unix::UnixDatagram;
let socket = UnixDatagram::unbound()?;
socket.send_to(b"hello", "/tmp/socket").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 connected address.
On success, returns the number of bytes received.
Examples
use async_net::unix::UnixDatagram;
let socket = UnixDatagram::unbound()?;
socket.connect("/tmp/socket")?;
let mut buf = vec![0; 1024];
let n = socket.recv(&mut buf).await?;
sourcepub async fn send(&self, buf: &[u8]) -> Result<usize>
pub async fn send(&self, buf: &[u8]) -> Result<usize>
Sends data to the connected address.
On success, returns the number of bytes sent.
Examples
use async_net::unix::UnixDatagram;
let socket = UnixDatagram::unbound()?;
socket.connect("/tmp/socket")?;
socket.send(b"hello").await?;
sourcepub fn shutdown(&self, how: Shutdown) -> Result<()>
pub fn shutdown(&self, how: Shutdown) -> Result<()>
Shuts down the read half, write half, or both halves of this socket.
This method will cause all pending and future I/O in the given directions to return
immediately with an appropriate value (see the documentation of Shutdown
).
Examples
use async_net::{Shutdown, unix::UnixDatagram};
let socket = UnixDatagram::unbound()?;
socket.shutdown(Shutdown::Both)?;
Trait Implementations
sourceimpl AsRawFd for UnixDatagram
impl AsRawFd for UnixDatagram
sourceimpl Clone for UnixDatagram
impl Clone for UnixDatagram
sourcefn clone(&self) -> UnixDatagram
fn clone(&self) -> UnixDatagram
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 UnixDatagram
impl Debug for UnixDatagram
sourceimpl From<Async<UnixDatagram>> for UnixDatagram
impl From<Async<UnixDatagram>> for UnixDatagram
sourcefn from(socket: Async<UnixDatagram>) -> UnixDatagram
fn from(socket: Async<UnixDatagram>) -> UnixDatagram
Performs the conversion.
sourceimpl From<UnixDatagram> for Arc<Async<UnixDatagram>>
impl From<UnixDatagram> for Arc<Async<UnixDatagram>>
sourcefn from(val: UnixDatagram) -> Self
fn from(val: UnixDatagram) -> Self
Performs the conversion.
sourceimpl TryFrom<UnixDatagram> for UnixDatagram
impl TryFrom<UnixDatagram> for UnixDatagram
sourcefn try_from(socket: UnixDatagram) -> Result<UnixDatagram>
fn try_from(socket: UnixDatagram) -> Result<UnixDatagram>
Performs the conversion.
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
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