Skip to main content

UdpSocket

Struct UdpSocket 

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

UDP socket type / UDP套接字类型

Provides async UDP send and receive operations. 提供异步UDP发送和接收操作。

§Example / 示例

use hiver_runtime::io::UdpSocket;

async fn echo_server() -> std::io::Result<()> {
    let socket = UdpSocket::bind("127.0.0.1:8080").await?;
    println!("UDP server listening on 127.0.0.1:8080");

    let mut buf = [0u8; 1024];
    loop {
        let (n, peer) = socket.recv_from(&mut buf).await?;
        socket.send_to(&buf[..n], &peer).await?;
    }
}

Implementations§

Source§

impl UdpSocket

Source

pub fn bind(addr: &str) -> BindUdpFuture

Bind a new UDP socket to the specified address 将新的UDP套接字绑定到指定地址

§Example / 示例
use hiver_runtime::io::UdpSocket;

async fn bind_server() -> std::io::Result<()> {
    let socket = UdpSocket::bind("127.0.0.1:8080").await?;
    Ok(())
}
Source

pub fn recv_from<'a, 'b>( &'a mut self, buf: &'b mut [u8], ) -> RecvFromFuture<'a, 'b>

Receive data from the socket 从套接字接收数据

Returns the number of bytes received and the peer address. 返回接收的字节数和对端地址。

Source

pub fn send_to<'a, 'b>( &'a mut self, buf: &'b [u8], addr: SocketAddr, ) -> SendToFuture<'a, 'b>

Send data to the specified address 向指定地址发送数据

Returns the number of bytes sent. 返回发送的字节数。

Source

pub fn connect(&mut self, addr: SocketAddr) -> ConnectUdpFuture

Connect the socket to a remote address 将套接字连接到远程地址

This filters incoming datagrams to only receive from this address. 这会过滤传入的数据报,只接收来自此地址的数据。

Trait Implementations§

Source§

impl AsRawFd for UdpSocket

Source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. 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> 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.