UdpSocket

Struct UdpSocket 

Source
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

Source

pub async fn bind(addr: SocketAddr) -> Result<UdpSocket>

Creates a new UDP socket from the given address.

Source

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.

Source

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.

Source

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.

Source

pub fn peer_addr(&self) -> Result<SocketAddr>

Returns the socket address of the remote peer this socket was connected to.

Source

pub fn local_addr(&self) -> Result<SocketAddr>

Returns the socket address of the local endpoint this socket is bound to.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn set_broadcast(&self, broadcast: bool) -> Result<()>

Sets the value for the SO_BROADCAST option on the socket.

Source

pub fn broadcast(&self) -> Result<bool>

Gets the value for the SO_BROADCAST option on the socket.

Source

pub fn set_multicast_loop_v4(&self, loop_v4: bool) -> Result<()>

Sets the value of the IP_MULTICAST_LOOP option on the socket.

Source

pub fn multicast_loop_v4(&self) -> Result<bool>

Gets the value of the IP_MULTICAST_LOOP option on the socket.

Source

pub fn set_multicast_ttl_v4(&self, ttl: u32) -> Result<()>

Sets the value of the IP_MULTICAST_TTL option on the socket.

Source

pub fn multicast_ttl_v4(&self) -> Result<u32>

Gets the value of the IP_MULTICAST_TTL option on the socket.

Source

pub fn set_multicast_loop_v6(&self, loop_v6: bool) -> Result<()>

Sets the value of the IPV6_MULTICAST_LOOP option on the socket.

Source

pub fn multicast_loop_v6(&self) -> Result<bool>

Gets the value of the IPV6_MULTICAST_LOOP option on the socket.

Source

pub fn set_ttl(&self, ttl: u32) -> Result<()>

Sets the value of the IP_TTL option on the socket.

Source

pub fn ttl(&self) -> Result<u32>

Gets the value of the IP_TTL option on the socket.

Source

pub fn join_multicast_v4( &self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr, ) -> Result<()>

Executes an operation of the IP_ADD_MEMBERSHIP type

Source

pub fn join_multicast_v6( &self, multiaddr: &Ipv6Addr, interface: u32, ) -> Result<()>

Executes an operation of the IPV6_ADD_MEMBERSHIP type

Source

pub fn leave_multicast_v4( &self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr, ) -> Result<()>

Source

pub fn leave_multicast_v6( &self, multiaddr: &Ipv6Addr, interface: u32, ) -> Result<()>

Source

pub fn take_error(&self) -> Result<Option<Error>>

Gets the value of the SO_ERROR option on the socket.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl AsFd for UdpSocket

Source§

fn as_fd(&self) -> BorrowedFd<'_>

Borrows the file descriptor. Read more
Source§

impl AsRawFd for UdpSocket

Source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
Source§

impl Debug for UdpSocket

Source§

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

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

impl From<UdpSocket> for UdpSocket

Source§

fn from(socket: UdpSocket) -> Self

Converts to this type from the input type.
Source§

impl From<UdpSocket> for UdpSocket

Available on crate feature tokio-net only.
Source§

fn from(socket: UdpSocket) -> Self

Converts to this type from the input type.
Source§

impl Unwrap for UdpSocket

Source§

type StdImpl = UdpSocket

Source§

type TokioImpl = UdpSocket

Source§

fn unwrap_std(self) -> Self::StdImpl

Unwraps the std underlying implementation of the MaybeFut type.
Source§

fn unwrap_tokio(self) -> Self::TokioImpl

Unwraps the tokio underlying implementation of the MaybeFut type.
Source§

fn unwrap_std_ref(&self) -> &Self::StdImpl

Unwraps the std underlying implementation of the MaybeFut type as a reference.
Source§

fn unwrap_tokio_ref(&self) -> &Self::TokioImpl

Unwraps the tokio underlying implementation of the MaybeFut type as a reference.
Source§

fn unwrap_std_mut(&mut self) -> &mut Self::StdImpl

Unwraps the std underlying implementation of the MaybeFut type as a mutable reference.
Source§

fn unwrap_tokio_mut(&mut self) -> &mut Self::TokioImpl

Unwraps the tokio underlying implementation of the MaybeFut type as a mutable reference.
Source§

fn get_std(self) -> Option<Self::StdImpl>

Safely unwraps the std underlying implementation of the MaybeFut type.
Source§

fn get_tokio(self) -> Option<Self::TokioImpl>

Safely unwraps the tokio underlying implementation of the MaybeFut type.
Source§

fn get_std_ref(&self) -> Option<&Self::StdImpl>

Safely unwraps the std underlying implementation of the MaybeFut type as a reference.
Source§

fn get_tokio_ref(&self) -> Option<&Self::TokioImpl>

Safely unwraps the tokio underlying implementation of the MaybeFut type as a reference.
Source§

fn get_std_mut(&mut self) -> Option<&mut Self::StdImpl>

Safely unwraps the std underlying implementation of the MaybeFut type as a mutable reference.
Source§

fn get_tokio_mut(&mut self) -> Option<&mut Self::TokioImpl>

Safely unwraps the tokio underlying implementation of the MaybeFut type as a mutable reference.

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>,

Source§

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>,

Source§

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.