This question already has answers here:
Reading from TcpStream results in empty buffer
(1 个回答)
UdpSocket.recv_from fails with “end of file” but I can see the incoming package in Wireshark
(1 个回答)
去年关闭。
我在网上找到了很多例子,其中
然而,这似乎对我不起作用。以下代码的输出是:
这是代码:
当我使用
这是 Rust 中的错误吗?我正在使用 1.41。
(1 个回答)
UdpSocket.recv_from fails with “end of file” but I can see the incoming package in Wireshark
(1 个回答)
去年关闭。
我在网上找到了很多例子,其中
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 中的错误吗?我正在使用 1.41。
最佳答案
Vec::new
和 Vec::with_capacity
返回一个包含 0 个元素的 Vec
,因此从它们借用 &mut buffer
将产生一个包含 0 个元素的切片。
recv
的参数必须有足够的容量来容纳字节,但由于 &mut buffer
是一个超过 0 字节的切片,因此该切片中没有足够的空间,因此多余的字节将被丢弃。
您可能希望在将 buffer.resize(32, 0)
传递给 recv
以反射(reflect)您使用数组 ( [u8; 32]
) 显示的情况之前。
10-08 02:36