为什么 [u8] 可以用作 UdpSocket 的缓冲区,而 Vec 则不能?



我在网上发现了许多例子,其中Vec<u8>被用作UdpSocket.recv()的缓冲区(例如1、2、3(。

然而,这似乎对我不起作用

[SEND] Wrote 4 bytes to the network: [1, 0, 0, 0]
[RECV] received 0 bytes: []
[SEND] Wrote 4 bytes to the network: [2, 0, 0, 0]
[RECV] received 0 bytes: []

这是代码:

use std::net::{SocketAddr, UdpSocket};
use std::{thread, time};
fn receiver(socket: UdpSocket, _remote: SocketAddr) {
// This works:
//   let mut buffer: [u8; 32] = [0; 32];
// These don't:
//   let mut buffer: Vec<u8> = Vec::with_capacity(32);
let mut buffer: Vec<u8> = Vec::new();
loop {
match socket.recv(&mut buffer) {
Ok(bytes) => {
println!("[RECV] received {} bytes: {:?}", bytes, buffer);
}
Err(error) => {
unimplemented!("Handle me: {:?}", error);
}
}
}
}
fn sender(socket: UdpSocket, remote: SocketAddr) {
thread::sleep(time::Duration::from_secs(3));
let a = bincode::serialize(&1).unwrap();
let b = bincode::serialize(&2).unwrap();
match socket.send_to(&a, remote) {
Ok(bytes) => {
println!("[SEND] Wrote {} bytes to the network: {:?}", bytes, a);
}
Err(error) => {
println!("{:?}", error);
}
}
thread::sleep(time::Duration::from_secs(1));
match socket.send_to(&b, remote) {
Ok(bytes) => {
println!("[SEND] Wrote {} bytes to the network: {:?}", bytes, b);
}
Err(error) => {
println!("{:?}", error);
}
}
}
fn main() {
use std::net::{IpAddr, Ipv4Addr};
let send_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 3333);
let recv_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 4444);
let send_sock = UdpSocket::bind(send_addr).unwrap();
let recv_sock = UdpSocket::bind(recv_addr).unwrap();
let send_handle = thread::spawn(move || sender(send_sock, recv_addr));
let recv_handle = thread::spawn(move || receiver(recv_sock, send_addr));
let _ = send_handle.join();
let _ = recv_handle.join();
}

当我使用[u8; 32]作为缓冲区时,它工作得很好:

[SEND] Wrote 4 bytes to the network: [1, 0, 0, 0]
[RECV] received 4 bytes: [1, 0, 0, 0, 0, 0, 0, 0, ...]
[SEND] Wrote 4 bytes to the network: [2, 0, 0, 0]
[RECV] received 4 bytes: [2, 0, 0, 0, 0, 0, 0, 0, ...]

这是Rust中的一个bug吗?我用的是1.41。

Vec::newVec::with_capacity返回一个具有0个元素的Vec,因此从它们借用&mut buffer将生成一个具有零个元素的切片。

recv的参数必须有足够的容量来容纳字节,但由于&mut buffer是一个超过0字节的切片,因此该切片中没有足够的空间,因此多余的字节将被丢弃。

您可能希望先buffer.resize(32, 0),然后再将其传递给recv,以反映您用数组([u8; 32](显示的情况。

最新更新