Update
The idea that Content-Length maybe required by the client could have some merit.
Conversely, you could add a Connection: close
header, which would prevent the browser from keeping it open (hence 'EOF' would count as end of response).
The code looks fine and it works without sleep on my box (Ubuntu/Opera).
The only things that strikes me as... interesting is that
you are sending the terminating NUL
character as part of the response:
$ netcat localhost 12345 | xxd
0000000: 4854 5450 2f31 2e30 2032 3030 204f 4b0d HTTP/1.0 200 OK.
0000010: 0a0d 0a3c 6874 6d6c 3e3c 626f 6479 3e3c ...<html><body><
0000020: 693e 4865 6c6c 6f2c 2077 6f72 6c64 3c2f i>Hello, world</
0000030: 693e 3c2f 626f 6479 3e3c 2f68 746d 6c3e i></body></html>
0000040: 00 .
you are not receiving the request; depending on the client this might confuse the client a little (?). I don't think this can be the problem here because there's precious little request information to be split up into packages
Update Actually, the/a problem might occur if your browser does other requests (like GET /favicon.ico
, e.g. in parallel. Your server handles connections synchronously and they might arrive simultaneously. Make sure that the listening socket allows for at least 1 (or maybe, say, 10) connections in the "backlog"
This old answer ('08) says backlog
used to be 5 in Asio, by default.