UdpSocket

Struct UdpSocket 

Source
pub struct UdpSocket { /* private fields */ }
Expand description

A UDP socket, using non-blocking I/O.

Intended to be used within a Tokio runtime, this offers a more ergonomic interface compared to the standard tokio::net::UdpSocket.

Bind to a socket with the bind method. Receive datagrams as a Stream with incoming, and send datagrams with send_to.

§Examples

let socket = UdpSocket::bind("127.0.0.1:34254")?;

tokio::spawn(
    socket.incoming()
        .for_each(|datagram| { println!("{:?}", datagram); Ok(()) })
        .map_err(|_| ())
);

tokio::spawn(
    socket.send_to(&[0xde, 0xad, 0xbe, 0xef], "127.0.0.1:34254")?
        .map_err(|_| ())
);

Implementations§

Source§

impl UdpSocket

Source

pub fn bind<Addrs: ToSocketAddrs>(addrs: Addrs) -> Result<UdpSocket, Error>

Creates a UDP socket from the given address.

The address parameter, which must implement ToSocketAddrs, is treated the same as for std::net::UdpSocket::bind. See the documentation there for details.

Returns an Err in the case that address parsing fails.

§Examples
let socket = UdpSocket::bind("127.0.0.1:34254")?;
Source

pub fn incoming(&self) -> Incoming

Returns a stream of datagrams received on this socket.

Each datagram in the stream is a UdpDatagram struct with the body of the datagram and the peer address it was received from.

§Examples
tokio::spawn(
    socket.incoming()
        .for_each(|datagram| { println!("{:?}", datagram); Ok(()) })
        .map_err(|_| ())
);
Source

pub fn send_to<'a, Addr: ToSocketAddrs>( &self, buffer: &'a [u8], addr: Addr, ) -> Result<SendTo<'a>, Error>

Sends data to the given address via the socket.

If the address text parses correctly, returns Ok of a future which resolves when the datagram has been written. Returns Err if an address can’t be parsed.

Though addr might resolve to multiple socket addresses, this will only attempt to send to the first resolved address, to be consistent with std::net::UdpSocket::send_to.

If you’ll be sending more than one datagram, it’s better to use send.

§Examples
tokio::spawn(
    socket.send_to(&[0xde, 0xad, 0xbe, 0xef], "127.0.0.1:34254")?
        .map_err(|_| ())
);
Source

pub fn send(&self, datagram: UdpDatagram) -> Send

Sends a datagram to the given address via the socket.

Returns a future which resolves when the datagram has been written. Use this method instead of send_to to repeatedly send to a peer without the address parsing overhead or risk of errors.

§Examples
tokio::spawn(
    socket.incoming()
        .map_err(|_| ())
        .for_each(move |datagram| {
            // echo!
            socket.send(datagram)
                .map_err(|_| ())
        })
);

Trait Implementations§

Source§

impl Clone for UdpSocket

Source§

fn clone(&self) -> UdpSocket

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for UdpSocket

Source§

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

Formats the value using the given formatter. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.