Reputation: 2146
I am trying to read data using the following code from a socket:
n = read(fd, buffer, 50000);
The question is: when the data from the web server is larger than the tcp package size, these data will be splited into multi packages. In this case, will read function just read one data package from fd, or it will read all the packages from fd?
Note that read function is called only once.
Upvotes: 1
Views: 3030
Reputation: 60943
Because you are using TCP, your socket is of type SOCK_STREAM
. A SOCK_STREAM
socket is a byte stream and does not maintain packet boundaries, so the call to read()
or recv()
will read data that came from multiple packets if multiple packets of data have been received and there is sufficient space in your buffer. It may also return data from a portion of a packet if your buffer if not large enough to hold all of the data. The next read()
will continue reading from the next byte.
Upvotes: 4
Reputation: 13690
The function read
receives at maximum the specified count of bytes, in your example 50000.
When the function read
returns, you need to check the return value. The actual number of bytes written to buffer is in your variable n
.
Upvotes: 0