Struct std::net::UnixDatagram
A Unix domain datagram socket
Unix datagram sockets provide connectionless, unreliable message delivery between processes on the same machine.
Example
use std::net::{UnixDatagram, UnixAddr};
use std::fs::Path;
let addr = UnixAddr::from_path(Path::new("/tmp/server.sock")).unwrap();
let socket = UnixDatagram::bind(&UnixAddr::unnamed()).unwrap();
defer socket.close();
socket.send_to("Hello!", &addr).unwrap();
Run this example
Fields
-
socket: Socket
Methods
impl UnixDatagram { ... }
-
fn from_socket(socket: Socket) -> UnixDatagram
Creates a Unix datagram socket from a raw socket.
-
fn unbound() -> Result<UnixDatagram>
Creates an unbound Unix datagram socket.
The socket can be used to send datagrams with send_to without binding.
Example
use std::net::UnixDatagram; let socket = UnixDatagram::unbound().unwrap(); defer socket.close();
Run this example -
fn bind(addr: &UnixAddr) -> Result<UnixDatagram>
Creates a Unix datagram socket bound to the specified address.
Example
use std::net::{UnixDatagram, UnixAddr}; use std::fs::Path; let addr = UnixAddr::from_path(Path::new("/tmp/my.sock")).unwrap(); let socket = UnixDatagram::bind(&addr).unwrap(); defer socket.close();
Run this example -
fn pair() -> Result<(UnixDatagram, UnixDatagram)>
Creates a pair of connected Unix datagram sockets.
Example
use std::net::UnixDatagram; let (sock1, sock2) = UnixDatagram::pair().unwrap(); defer sock1.close(); defer sock2.close(); sock1.send("Hello!").unwrap(); let buf: [u8; 128]; let n = sock2.recv(&buf).unwrap(); assert_eq!(buf[..n] as &[u8], "Hello!");
Run this example -
fn connect(self: &UnixDatagram, addr: &UnixAddr) -> Result<()>
Connects this socket to a remote address.
After connecting, send and recv can be used to send and receive datagrams to/from the specified address.
Example
use std::net::{UnixDatagram, UnixAddr}; use std::fs::Path; let addr = UnixAddr::from_path(Path::new("/tmp/server.sock")).unwrap(); let socket = UnixDatagram::unbound().unwrap(); defer socket.close(); socket.connect(&addr).unwrap(); socket.send("Hello!").unwrap();
Run this example -
fn recv_from(self: &UnixDatagram, buf: &mut [u8]) -> Result<(usize, UnixAddr)>
Receives data from the socket.
Returns the number of bytes read and the address from which the data was received.
Example
use std::net::{UnixDatagram, UnixAddr}; use std::fs::Path; let addr = UnixAddr::from_path(Path::new("/tmp/my.sock")).unwrap(); let socket = UnixDatagram::bind(&addr).unwrap(); defer socket.close(); let buf: [u8; 1024]; let (size, peer) = socket.recv_from(&buf).unwrap(); println!("Received {} bytes from {}", size, peer);
Run this example -
fn send_to(self: &UnixDatagram, buf: &[u8], addr: &UnixAddr) -> Result<usize>
Sends data to a particular address.
Example
use std::net::{UnixDatagram, UnixAddr}; use std::fs::Path; let addr = UnixAddr::from_path(Path::new("/tmp/server.sock")).unwrap(); let socket = UnixDatagram::unbound().unwrap(); defer socket.close(); socket.send_to("Hello!", &addr).unwrap();
Run this example -
fn recv(self: &UnixDatagram, buf: &mut [u8]) -> Result<usize>
Receives data from the socket (requires prior connect).
Example
use std::net::{UnixDatagram, UnixAddr}; use std::fs::Path; let addr = UnixAddr::from_path(Path::new("/tmp/server.sock")).unwrap(); let socket = UnixDatagram::unbound().unwrap(); defer socket.close(); socket.connect(&addr).unwrap(); let buf: [u8; 1024]; let n = socket.recv(&buf).unwrap();
Run this example -
fn send(self: &UnixDatagram, buf: &[u8]) -> Result<usize>
Sends data to the socket (requires prior connect).
Example
use std::net::{UnixDatagram, UnixAddr}; use std::fs::Path; let addr = UnixAddr::from_path(Path::new("/tmp/server.sock")).unwrap(); let socket = UnixDatagram::unbound().unwrap(); defer socket.close(); socket.connect(&addr).unwrap(); socket.send("Hello!").unwrap();
Run this example -
fn socket_addr(self: &UnixDatagram) -> Result<SocketAddr>
Returns the local socket address of this datagram socket.
Example
use std::net::{UnixDatagram, UnixAddr}; use std::fs::Path; let addr = UnixAddr::from_path(Path::new("/tmp/my.sock")).unwrap(); let socket = UnixDatagram::bind(&addr).unwrap(); defer socket.close(); let local = socket.socket_addr().unwrap(); println!("Bound to {}", local);
Run this example -
fn peer_addr(self: &UnixDatagram) -> Result<SocketAddr>
Returns the remote socket address this socket is connected to.
Example
use std::net::{UnixDatagram, UnixAddr}; use std::fs::Path; let addr = UnixAddr::from_path(Path::new("/tmp/server.sock")).unwrap(); let socket = UnixDatagram::unbound().unwrap(); defer socket.close(); socket.connect(&addr).unwrap(); let peer = socket.peer_addr().unwrap(); println!("Connected to {}", peer);
Run this example -
fn shutdown(self: &UnixDatagram, how: Shutdown) -> Result<()>
Shuts down the reading or writing end of the socket or both.
Example
use std::net::{UnixDatagram, UnixAddr, Shutdown}; use std::fs::Path; let addr = UnixAddr::from_path(Path::new("/tmp/server.sock")).unwrap(); let socket = UnixDatagram::unbound().unwrap(); defer socket.close(); socket.connect(&addr).unwrap(); socket.send("Hello!").unwrap(); socket.shutdown(Shutdown::Write).unwrap();
Run this example -
fn set_read_timeout(self: &UnixDatagram, duration: Option<Duration>) -> Result<()>
Sets the read timeout for the socket.
If the provided duration is
Option::none()
, then read operations will block indefinitely. Otherwise, read operations will time out after the specified duration.Example
use std::net::{UnixDatagram, UnixAddr}; use std::fs::Path; use std::time::Duration; let addr = UnixAddr::from_path(Path::new("/tmp/my.sock")).unwrap(); let socket = UnixDatagram::bind(&addr).unwrap(); defer socket.close(); socket.set_read_timeout(Option::some(Duration::from_secs(5))).unwrap();
Run this example -
fn read_timeout(self: &UnixDatagram) -> Result<Option<Duration>>
Gets the read timeout for the socket.
Returns
Option::none()
if reads will block indefinitely.Example
use std::net::{UnixDatagram, UnixAddr}; use std::fs::Path; let addr = UnixAddr::from_path(Path::new("/tmp/my.sock")).unwrap(); let socket = UnixDatagram::bind(&addr).unwrap(); defer socket.close(); let timeout = socket.read_timeout().unwrap(); if timeout.is_some() { println!("Read timeout: {} seconds", timeout.unwrap().total_secs()); }
Run this example -
fn set_write_timeout(self: &UnixDatagram, duration: Option<Duration>) -> Result<()>
Sets the write timeout for the socket.
If the provided duration is
Option::none()
, then write operations will block indefinitely. Otherwise, write operations will time out after the specified duration.Example
use std::net::{UnixDatagram, UnixAddr}; use std::fs::Path; use std::time::Duration; let addr = UnixAddr::from_path(Path::new("/tmp/my.sock")).unwrap(); let socket = UnixDatagram::bind(&addr).unwrap(); defer socket.close(); socket.set_write_timeout(Option::some(Duration::from_secs(5))).unwrap();
Run this example -
fn write_timeout(self: &UnixDatagram) -> Result<Option<Duration>>
Gets the write timeout for the socket.
Returns
Option::none()
if writes will block indefinitely.Example
use std::net::{UnixDatagram, UnixAddr}; use std::fs::Path; let addr = UnixAddr::from_path(Path::new("/tmp/my.sock")).unwrap(); let socket = UnixDatagram::bind(&addr).unwrap(); defer socket.close(); let timeout = socket.write_timeout().unwrap(); if timeout.is_some() { println!("Write timeout: {} seconds", timeout.unwrap().total_secs()); }
Run this example -
fn set_nonblocking(self: &mut UnixDatagram, nonblocking: bool) -> Result<()>
Sets the socket to non-blocking or blocking mode.
In non-blocking mode, read and write operations will return immediately with an error if they would block.
Example
use std::net::{UnixDatagram, UnixAddr}; use std::fs::Path; let addr = UnixAddr::from_path(Path::new("/tmp/my.sock")).unwrap(); let socket = UnixDatagram::bind(&addr).unwrap(); defer socket.close(); socket.set_nonblocking(true).unwrap();
Run this example -
fn close(self: &mut UnixDatagram) -> Result<()>
Closes the socket.
-
fn as_fd(self: &UnixDatagram) -> FileDescriptor
-
fn move(self: &mut UnixDatagram) -> UnixDatagram