pub struct UdpSocket(/* private fields */);Expand description
A UDP Socket.
UDP is “connectionless”, unlike TCP.
Meaning, regardless of what address you’ve bound to, a UdpSocket is free to communicate with many different remotes.
Implementations§
Source§impl UdpSocket
impl UdpSocket
Sourcepub async fn bind(addr: SocketAddr) -> Result<UdpSocket>
pub async fn bind(addr: SocketAddr) -> Result<UdpSocket>
Creates a new UDP socket from the given address.
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 a single datagram messages on the socket.
On success, returns the number of bytes read and the source address.
Sourcepub async fn peek_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)>
pub async fn peek_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)>
Receives a single datagram message on the socket, without removing it from the queue.
On success, returns the number of bytes read and the source address.
Sourcepub async fn send_to(&self, buf: &[u8], target: SocketAddr) -> Result<usize>
pub async fn send_to(&self, buf: &[u8], target: SocketAddr) -> Result<usize>
Sends data on the socket to the given address.
On Success, returns the number of bytes written.
Note that the operating system may refuse buffers larger than 65507 bytes.
Sourcepub fn peer_addr(&self) -> Result<SocketAddr>
pub fn peer_addr(&self) -> Result<SocketAddr>
Returns the socket address of the remote peer this socket was connected to.
Sourcepub fn local_addr(&self) -> Result<SocketAddr>
pub fn local_addr(&self) -> Result<SocketAddr>
Returns the socket address of the local endpoint this socket is bound to.
Sourcepub fn try_clone(&self) -> Result<Self>
pub fn try_clone(&self) -> Result<Self>
Creates a new independently owned handle to the same socket.
It doesn’t work with Tokio’s UdpSocket because it doesn’t support cloning.
Sourcepub fn set_read_timeout(&self, timeout: Option<Duration>) -> Result<()>
pub fn set_read_timeout(&self, timeout: Option<Duration>) -> Result<()>
Sets the read timeout for the socket.
It doesn’t work with Tokio’s UdpSocket because it doesn’t support setting timeouts.
Sourcepub fn set_write_timeout(&self, timeout: Option<Duration>) -> Result<()>
pub fn set_write_timeout(&self, timeout: Option<Duration>) -> Result<()>
Sets the write timeout for the socket.
It doesn’t work with Tokio’s UdpSocket because it doesn’t support setting timeouts.
Sourcepub fn read_timeout(&self) -> Result<Option<Duration>>
pub fn read_timeout(&self) -> Result<Option<Duration>>
Returns the read and write timeouts for the socket.
It doesn’t work with Tokio’s UdpSocket because it doesn’t support timeouts.
Sourcepub fn write_timeout(&self) -> Result<Option<Duration>>
pub fn write_timeout(&self) -> Result<Option<Duration>>
Returns the read and write timeouts for the socket.
It doesn’t work with Tokio’s UdpSocket because it doesn’t support timeouts.
Sourcepub fn set_broadcast(&self, broadcast: bool) -> Result<()>
pub fn set_broadcast(&self, broadcast: bool) -> Result<()>
Sets the value for the SO_BROADCAST option on the socket.
Sourcepub fn broadcast(&self) -> Result<bool>
pub fn broadcast(&self) -> Result<bool>
Gets the value for the SO_BROADCAST option on the socket.
Sourcepub fn set_multicast_loop_v4(&self, loop_v4: bool) -> Result<()>
pub fn set_multicast_loop_v4(&self, loop_v4: bool) -> Result<()>
Sets the value of the IP_MULTICAST_LOOP option on the socket.
Sourcepub fn multicast_loop_v4(&self) -> Result<bool>
pub fn multicast_loop_v4(&self) -> Result<bool>
Gets the value of the IP_MULTICAST_LOOP option on the socket.
Sourcepub fn set_multicast_ttl_v4(&self, ttl: u32) -> Result<()>
pub fn set_multicast_ttl_v4(&self, ttl: u32) -> Result<()>
Sets the value of the IP_MULTICAST_TTL option on the socket.
Sourcepub fn multicast_ttl_v4(&self) -> Result<u32>
pub fn multicast_ttl_v4(&self) -> Result<u32>
Gets the value of the IP_MULTICAST_TTL option on the socket.
Sourcepub fn set_multicast_loop_v6(&self, loop_v6: bool) -> Result<()>
pub fn set_multicast_loop_v6(&self, loop_v6: bool) -> Result<()>
Sets the value of the IPV6_MULTICAST_LOOP option on the socket.
Sourcepub fn multicast_loop_v6(&self) -> Result<bool>
pub fn multicast_loop_v6(&self) -> Result<bool>
Gets the value of the IPV6_MULTICAST_LOOP option on the socket.
Sourcepub fn set_ttl(&self, ttl: u32) -> Result<()>
pub fn set_ttl(&self, ttl: u32) -> Result<()>
Sets the value of the IP_TTL option on the socket.
Sourcepub fn join_multicast_v4(
&self,
multiaddr: &Ipv4Addr,
interface: &Ipv4Addr,
) -> Result<()>
pub fn join_multicast_v4( &self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr, ) -> Result<()>
Executes an operation of the IP_ADD_MEMBERSHIP type
Sourcepub fn join_multicast_v6(
&self,
multiaddr: &Ipv6Addr,
interface: u32,
) -> Result<()>
pub fn join_multicast_v6( &self, multiaddr: &Ipv6Addr, interface: u32, ) -> Result<()>
Executes an operation of the IPV6_ADD_MEMBERSHIP type
pub fn leave_multicast_v4( &self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr, ) -> Result<()>
pub fn leave_multicast_v6( &self, multiaddr: &Ipv6Addr, interface: u32, ) -> Result<()>
Sourcepub fn take_error(&self) -> Result<Option<Error>>
pub fn take_error(&self) -> Result<Option<Error>>
Gets the value of the SO_ERROR option on the socket.
Sourcepub async fn connect(&self, addr: SocketAddr) -> Result<()>
pub async fn connect(&self, addr: SocketAddr) -> Result<()>
Connects this UDP socket to a remote address, allowing the send and recv syscalls to be used to send data and also applies filters to only receive data from the specified address.
Sourcepub async fn send(&self, buf: &[u8]) -> Result<usize>
pub async fn send(&self, buf: &[u8]) -> Result<usize>
Sendss data on the socket to the remote address this socket is connected to.
On Success, returns the number of bytes written.
Sourcepub async fn recv(&self, buf: &mut [u8]) -> Result<usize>
pub async fn recv(&self, buf: &mut [u8]) -> Result<usize>
Receives a single datagram message on the socket.
On success, returns the number of bytes read.
Sourcepub async fn peek(&self, buf: &mut [u8]) -> Result<usize>
pub async fn peek(&self, buf: &mut [u8]) -> Result<usize>
Receives a single datagram message on the socket, without removing it from the queue.
On success, returns the number of bytes read.
Sourcepub fn set_nonblocking(&self, nonblocking: bool) -> Result<()>
pub fn set_nonblocking(&self, nonblocking: bool) -> Result<()>
Moves this UDP socket into or out of non-blocking mode.
It doesn’t work with Tokio’s UdpSocket because it doesn’t support non-blocking mode.