本文簡要介紹rust語言中 std::net::UdpSocket.recv
的用法。
用法
pub fn recv(&self, buf: &mut [u8]) -> Result<usize>
在套接字上從它所連接的遠程地址接收單個數據報消息。成功時,返回讀取的字節數。
必須使用具有足夠大小的有效字節數組buf
來調用該函數以保存消息字節。如果消息太長而無法放入提供的緩衝區,則可能會丟棄多餘的字節。
UdpSocket::connect
將此套接字連接到遠程地址。如果套接字未連接,此方法將失敗。
例子
use std::net::UdpSocket;
let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
socket.connect("127.0.0.1:8080").expect("connect function failed");
let mut buf = [0; 10];
match socket.recv(&mut buf) {
Ok(received) => println!("received {} bytes {:?}", received, &buf[..received]),
Err(e) => println!("recv function failed: {:?}", e),
}
相關用法
- Rust UdpSocket.recv_from用法及代碼示例
- Rust UdpSocket.read_timeout用法及代碼示例
- Rust UdpSocket.set_multicast_loop_v6用法及代碼示例
- Rust UdpSocket.send用法及代碼示例
- Rust UdpSocket.ttl用法及代碼示例
- Rust UdpSocket.set_write_timeout用法及代碼示例
- Rust UdpSocket.peek用法及代碼示例
- Rust UdpSocket.set_ttl用法及代碼示例
- Rust UdpSocket.broadcast用法及代碼示例
- Rust UdpSocket.peer_addr用法及代碼示例
- Rust UdpSocket.try_clone用法及代碼示例
- Rust UdpSocket.set_broadcast用法及代碼示例
- Rust UdpSocket.send_to用法及代碼示例
- Rust UdpSocket.bind用法及代碼示例
- Rust UdpSocket.set_multicast_loop_v4用法及代碼示例
- Rust UdpSocket.write_timeout用法及代碼示例
- Rust UdpSocket.set_read_timeout用法及代碼示例
- Rust UdpSocket.set_nonblocking用法及代碼示例
- Rust UdpSocket.connect用法及代碼示例
- Rust UdpSocket.multicast_loop_v6用法及代碼示例
- Rust UdpSocket.multicast_loop_v4用法及代碼示例
- Rust UdpSocket.peek_from用法及代碼示例
- Rust UdpSocket.multicast_ttl_v4用法及代碼示例
- Rust UdpSocket.set_multicast_ttl_v4用法及代碼示例
- Rust UdpSocket.local_addr用法及代碼示例
注:本文由純淨天空篩選整理自rust-lang.org大神的英文原創作品 std::net::UdpSocket.recv。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。