Guys, I'm kinda stuck with a problem here. I'm trying to understand the the basics of TCP server programming at a relatively low level, and I don't know whether my Rust code is wrong or curl doesn't do what I'm expectiong it to do.
On the curl side it's all simple:
curl --data "parm=value" localhost:33000
I tried all possible combinations and methods of reading from a TcpStream, including BufReader.
Both sides are blocked. When I CTRL+C curl, only then I get my buf ready.
Ok so read_to_end
says it reads until EOF. I suspect curl doesn't send EOF with POST request?
let mut buf= vec![];
stream.read_to_end(&mut buf); //blocks
In this case read successfully get called several times, but the last call is blocked and never returns.
let mut read_buf = [0u8; 32];
let mut data = vec![];
loop {
match reader.read(&mut read_buf) {
Ok(0) => break, // this arm never called
Ok(n) => data.extend_from_slice(&read_buf[..n]),
Err(e) => eprintln!("{}", e),
}
}
Same as in previous case, read_line blocks right before the end.
let mut reader = BufReader::new(stream);
let mut s = String::new();
loop {
match reader.read_line(&mut s) {
Ok(0) => {
println!("Read zero bytes"); // arm never reached
break;
}
Ok(n) => {
println!("Read {}", n);
}
_ => ()
}
}
Finally, this works and I don't know why and I don't feel this is a right solution
First, read doesn't guarantied to read all the data in one call, second the buf might not be large enough. But why doesn't this block?. With large enough buffer it somehow knows when to stop reading!
let mut buf = [0u8; 1024];
socket.read(&mut buf)
How do I properly read the request (not parse, just read the bytes)?