In article , Glyn Davies wrote: :For simplicity of implementation, I'd recommend implementing a message :layer over the TCP socket - something as simple as a two byte binary header :to indicate the length of the message to follow.
:This means you can then read a whole message - text based or otherwise, :and the process it accordingly. You don't need to mess about with parsing :lines and stuff like that.
You still need to "mess about" if your data might not fit in a single packet. Recall that when you read() from a socket you are not certain to get all of the data unless the data length is no greater than your system's atomicity limits [PIPE_BUF]. The POSIX minimum value for PIPE_BUF is 512 -- so in the general case a write() of more than 512 bytes may require multiple read() statements.