I can’t understand how and why the following code segments work:
size = 5000; err = setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(int)); if (err != 0) { printf("Unable to set send buffer size, continuing with default size\n"); }
If we check the value of the send buffer, it is really correctly set to 5000 * 2 = 10000. However, if we try to send more than the size of the send buffer, it will send it all. For example:
n = send(sockfd, buf, 30000, 0); /* Lets check how much us actually sent */ printf("No. of bytes sent is %d\n", n);
Prints 30,000.
How exactly did it work? Did the fact that the size of the send buffer be equal to 10,000? If so, what exactly happened? Some kind of fragmentation?
UPDATE: What happens if the socket is in non-blocking mode? I tried the following:
- Changing the buffer size to 10,000 (5,000 * 2) sends 16,384 bytes
- Changing the buffer size to 20,000 (10,000 * 2) causes the sending of 30,000 bytes
Once again, why?
Arun
source share