I'm experiencing some problems with reading TCP packets.
I'm trying to read a JSON response, that's 5000
bytes in size, but looking at the packets in the Wireshark, they're separated into three different packets, first and second being 1448
bytes, and third being 2530
bytes in size.
When I try to read them with Tokio-rs
, I only receive the first one, so I don't receive whole JSON data.
For reading, I'm using the following code:
pub async fn read(stream: &mut TcpStream) -> Result<Bytes, std::io::Error>{
let mut buf = BytesMut::with_capacity(8128);
let mut resp = [0u8; 8128];
let buf_len = stream.read(&mut resp).await?;
buf.extend_from_slice(&resp);
buf.truncate(buf_len);
println!("{}", buf.len());
Ok(buf.freeze())
}
And buf.len()
returns 1448
which is exactly the size of the first and second packet, but buf
contains data from the first packet.
Now I'm wondering if I missed something and the TcpStream
closes with the first received packet or am I missing buffer size somewhere.
read_to_end()
didn't work for me, as it would stop reading when the socket closed, but mine doesn't. – MG lolenstine