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)

Thanks in advance

You should be more precise about what OS and which API you are using to read the socket for people to help you.
However, in general functions that read sckets do not concern themselfs about the state of your buffer. It is your responibility to copy the buffer contents after you have read from the socket. The next call to the read function will overwrite your buffer if tehre is data on the socket, however if there is no date your buffer will remain untouched. The read function will not clean it. The read function usualy gives you as return value the nubmer of bytes it has written to the buffer.

regrards

Andrija

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.