samedi 28 février 2015

What is the size of a socket send buffer in Windows?

Based on my understanding, each socket is associated with two buffers, a send buffer and a receive buffer, so when I call the send() function, what happens is that the data to send will be placed into the send buffer, and it is the responsibility of Windows now to send the content of this send buffer to the other end.


In a blocking socket, the send() function does not return until the entire data supplied to it has been placed into the send buffer.


So what is the size of the send buffer?


I performed the following test (sending 1 GB worth of data):



// Create 1 GB buffer ("AAAAAA...A")
char *buffer = new char[1073741824];
memset(buffer, 0x41, 1073741824);

// Send buffer
send(s, buffer, 1073741824, 0);

printf("send() has returned, WSAGetLastError(): %d\n", WSAGetLastError());


send() has returned immediately, this means that the send buffer has a size of at least 1 GB!


Note: this does not necessarily means that the entire 1 GB was copied to the send buffer, it could be that only the address of the data was copied, and Windows is using some "copy-on-write" strategy to only copy the bytes I may change at a later time.




Edit: the return value of send() is 1073741824 (this means that no error has occurred).


Aucun commentaire:

Enregistrer un commentaire