Q:

Read() in C/C++ - regarding what is read vs buffer size

I'm trying to read a message (string/text) from the server, and I set the buffer size really large (buffer_size = 1000) so that I only need to read once from the server. 
So my question is if the message is exactly 10 bytes, and I call read(socket, buffer, buffer_size), then is it gonna read only 10 bytes, since it is less than the actual buffer size? I guess I'm just curious about the behavior of the call in case what is read is actually not as much as what is expected.
Also if I call the read() again, will it overwrite what is in the buffer? By that I mean empty the buffer and overwrite it with new input.

A:

You're presumably using TCP, which is a streaming protocol - message boundaries are not sent, just the stream of bytes. So even if the server does a single write, you may end up having to do several reads to get the data.
Keep reading until either you have enough bytes, or read returns 0 (which means EOF).
If a read gets you N bytes, and it's not enough, then you need to issue another read targeting buffer + N.

