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
impl UdpSocket
Sourcepub fn bind(addr: &str) -> BindUdpFuture ⓘ
pub fn bind(addr: &str) -> BindUdpFuture ⓘ
Sourcepub fn recv_from<'a, 'b>(
&'a mut self,
buf: &'b mut [u8],
) -> RecvFromFuture<'a, 'b> ⓘ
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. 返回接收的字节数和对端地址。
Sourcepub fn send_to<'a, 'b>(
&'a mut self,
buf: &'b [u8],
addr: SocketAddr,
) -> SendToFuture<'a, 'b> ⓘ
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. 返回发送的字节数。
Sourcepub fn connect(&mut self, addr: SocketAddr) -> ConnectUdpFuture ⓘ
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§
Auto Trait Implementations§
impl Freeze for UdpSocket
impl RefUnwindSafe for UdpSocket
impl Send for UdpSocket
impl Sync for UdpSocket
impl Unpin for UdpSocket
impl UnsafeUnpin for UdpSocket
impl UnwindSafe for UdpSocket
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more