我在网上发现了许多示例,其中Vec<u8>
被用作UdpSocket.recv()
的缓冲区(例如1,2,3)。 / p>
但是,这似乎对我不起作用。以下代码的输出是:
[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中的错误吗?我正在使用1.41。
答案 0 :(得分:2)
Vec::new
和Vec::with_capacity
返回具有0个元素的Vec
,因此从它们中借用&mut buffer
将产生一个具有0个元素的切片。
recv
的参数必须具有足够的容量来容纳字节,但是由于&mut buffer
是一个0字节以上的片,因此该片中没有足够的空间,因此多余的字节将被丢弃。
您可能希望先将buffer.resize(32, 0)
传递给recv
,以反映您使用数组([u8; 32]
)显示的情况。