'Rust TcpStream only reads 2 bytes after first read is called
I was experimenting with Rust's TcpStream and got stuck on some weird behavior with the stream.
I need to read two separate times. The first read is working perfectly as expected, but the second read will only ever read 2 bytes, even if there is more content.
let mut stream = TcpStream::connect("127.0.0.1:3241").unwrap();
let mut f_buf = [0u8, 8];
stream.read(&mut f_buf).unwarp();
//Output 8 bytes
println!("First Buffer: {:?}", f_buf);
let mut s_buf = [0u8, 8];
stream.read(&mut s_buf).unwarp();
//Outputs only 2 bytes
println!("Second Buffer: {:?}", s_buf);
If I send the following data to the TcpStream from the other side
[
0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1
]
f_buf will be [0, 0, 0, 0, 0, 0, 0, 0] as expected, but s_buf will be just [1, 1] instead of all the other 1s. The next time I try to read a later point in the program, I get the remaining 1s.
I have tried using read_exact, set_nonblocking(false), and adding a half-second delay between the two reads without success. I'm not able to find any other StackOverflow questions with this issue. I'm not using capacity on a vec like several of the other similar questions and I want to read a specific amount of bytes, not wait until I get EOF. If I change the first read to something smaller it will still just read the next two bytes. I'm really at a loss for what could be happening.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
