0

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

2
Contributors
1
Reply
24
Views
2 Years
Discussion Span
Last Post by aradicev
0

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

Edited by aradicev: typo

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.