In my understanding, TCPStream
doesn't know when a complete message arrived from a client but the information arrives at a stream of bytes.
However, when I do the standard "Hello World" TCPStream
example in Rust, I am reading complete HTTP messages off the stream. When I send two or more messages, they will get separated accordingly.
How is this possible?
use std::io::prelude::*;
use std::net::TcpListener;
use std::net::TcpStream;
fn main() {
let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
for stream in listener.incoming() {
let stream = stream.unwrap();
handle_connection(stream);
}
}
fn handle_connection(mut stream: TcpStream) {
let mut buffer = [0; 512];
stream.read(&mut buffer).unwrap();
println!("{}", String::from_utf8_lossy(&buffer[..]));
}
When I reduce the buffer size, the HTTP messages are getting cut and newer messages start from the beginning. I would somehow assume that I have to manage ending and starting a new HTTP message myself?