24

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):

#include <stdio.h>

#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")

#include <Windows.h>

int main()
{
    // Initialize Winsock
    WSADATA wsa;
    WSAStartup(MAKEWORD(2, 2), &wsa);

    // Create socket
    SOCKET s = socket(AF_INET, SOCK_STREAM, 0);

    //----------------------

    // Connect to 192.168.1.7:12345
    sockaddr_in address;
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = inet_addr("192.168.1.7");
    address.sin_port = htons(12345);
    connect(s, (sockaddr*)&address, sizeof(address));

    //----------------------

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

    // Send buffer
    int i = send(s, buffer, 1073741824, 0);

    printf("send() has returned\nReturn value: %d\nWSAGetLastError(): %d\n", i, WSAGetLastError());

    //----------------------

    getchar();
    return 0;
}

Output:

send() has returned
Return value: 1073741824
WSAGetLastError(): 0

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

This is some information about the test:

  • I am using a TCP blocking socket.
  • I have connected to a LAN machine.
  • Client Windows version: Windows 7 Ultimate 64-bit.
  • Server Windows version: Windows XP SP2 32-bit (installed on Virtual Box).

Edit: I have also attempted to connect to Google (173.194.116.18:80) and I got the same results.

Edit 2: I have discovered something strange, setting the send buffer to a value between 64 KB and 130 KB will make send() work as expected!

int send_buffer = 64 * 1024;    // 64 KB
int send_buffer_sizeof = sizeof(int);
setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char*)send_buffer, send_buffer_sizeof);

Edit 3: It turned out (thanks to Harry Johnston) that I have used setsockopt() in an incorrect way, this is how it is used:

setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char*)&send_buffer, send_buffer_sizeof);

Setting the send buffer to a value between 64 KB and 130 KB does not make send() work as expected, but rather setting the send buffer to 0 makes it block (this is what I noticed anyway, I don't have any documentation for this behavior).

So my question now is: where can I find a documentation on how send() (and maybe other socket operations) work under Windows?

15
  • If no error occurs, send returns the total number of bytes sent, which can be less than the number requested to be sent in the len parameter. msdn.microsoft.com/en-us/library/windows/desktop/… Feb 28, 2015 at 19:14
  • 3
    You can discover the size for yourself with getsockopt(). And how do you know it returned immediately? That code can't tell you that.
    – user207421
    Feb 28, 2015 at 20:27
  • You can tell it returned (error or no error) immediately because he sent a 1GB buffer. Unless you have SUPER fast internet, this would take him quite a long time. Most likely it failed.
    – Brandon
    Feb 28, 2015 at 21:13
  • @Brandon You cannot know from this code when the send starts and finishes. There is also a connect() in there, that could take appreciable time. He isn't using the Internet, he's using a LAN.
    – user207421
    Feb 28, 2015 at 21:21
  • @EJP Actually, I connected to Google (173.194.116.18:80), and send() has also returned immediately without errors.
    – user4344762
    Feb 28, 2015 at 22:22

3 Answers 3

18
+500

After investigating on this subject. This is what I believe to be the correct answer:

When calling send(), there are two things that could happen:

  • If there are pending data which are below SO_SNDBUF, then send() would return immediately (and it does not matter whether you are sending 5 KB or you are sending 500 MB).

  • If there are pending data which are above or equal SO_SNDBUF, then send() would block until enough data has been sent to restore the pending data to below SO_SNDBUF.

Note that this behavior is only applicable to Windows sockets, and not to POSIX sockets. I think that POSIX sockets only use one fixed sized send buffer (correct me if I'm wrong).


Now back to your main question "What is the size of a socket send buffer in Windows?". I guess if you have enough memory it could grow beyond 1 GB if necessary (not sure what is the maximum limit though).

6

I can reproduce this behaviour, and using Resource Monitor it is easy to see that Windows does indeed allocate 1GB of buffer space when the send() occurs.

An interesting feature is that if you do a second send immediately after the first one, that call does not return until both sends have completed. The buffer space from the first send is released once that send has completed, but the second send() continues to block until all the data has been transferred.

I suspect the difference in behaviour is because the second call to send() was already blocking when the first send completed. The third call to send() returns immediately (and 1GB of buffer space is allocated) just as the first one did, and so on, alternating.

So I conclude that the answer to the question ("how large are the send buffers?") is "as large as Windows sees fit". The upshot is that, in order to avoid exhausting the system memory, you should probably restrict blocking sends to no more than a few hundred megabytes.

Your call to setsockopt() is incorrect; the fourth argument is supposed to be a pointer to an integer, not an integer converted to a pointer. Once this is corrected, it turns out that setting the buffer size to zero causes send() to always block.

To summarize, the observed behaviour is that send() will return immediately provided:

  • there is enough memory to buffer all the provided data
  • there is not a send already in progress
  • the buffer size is not set to zero

Otherwise, it will return once the data has been sent.

KB214397 describes some of this - thanks Hans! In particular it describes that setting the buffer size to zero disables Winsock buffering, and comments that "If necessary, Winsock can buffer significantly more than the SO_SNDBUF buffer size."

(The completion notification described does not quite match up to the observed behaviour, depending I guess on how you interpret "previously buffered send". But it's close.)

Note that apart from the risk of inadvertently exhausting the system memory, none of this should matter. If you really need to know whether the code at the other end has received all your data yet, the only reliable way to do that is to get it to tell you.

12
  • Unfortunately I don't think that this is the correct answer. I have tried sending only a 2 MB buffer, and send() has also returned immediately (before the 2 MB were sent to the other side, so it is not that it returned immediately because I have a fast connection!). As for the sending of a second large buffer, it is not that it is a large buffer, I have attempted to send a second buffer of only 1 KB in size after the 1 GB buffer and I experienced the same behavior you described (what appears that the second send() has blocked).
    – user4344762
    Mar 1, 2015 at 22:56
  • What makes all of this very strange is what I said in my question: setting the send buffer to a value between 64 KB and 130 KB will make send() work as expected, even in the case of the sending of a second buffer (send() will block until the first buffer is sent, and will also block until the second buffer is sent).
    – user4344762
    Mar 1, 2015 at 22:58
  • I think that we need to know for sure how Windows handle send() (and maybe other socket related operations). So when we create programs that uses sockets, we don't get unpredictable behavior.
    – user4344762
    Mar 2, 2015 at 0:21
  • Yes, you are right. I forgot to put an & before send_buffer in (char*)send_buffer! I will test the code again.
    – user4344762
    Mar 2, 2015 at 0:30
  • You are right, nice discovery! Looks like the memory values at addresses 65536 (64 KB) to 133120 (130 KB) are 0. But is this how send() works under other operating systems? And does Microsoft provides documentation of how all of this works if this is not standard behavior?
    – user4344762
    Mar 2, 2015 at 0:45
0

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

That is not guaranteed. If there is available buffer space, but not enough space for the entire data, the socket can (and usually will) accept whatever data it can and ignore the rest. The return value of send() tells you how many bytes were actually accepted. You have to call send() again to send the remaining data.

So what is the size of the send buffer?

Use getsockopt() with the SO_SNDBUF option to find out.

Use setsockopt() with the SO_SNDBUF option to specify your own buffer size. However, the socket may impose a max cap on the value you specify. Use getsockopt() to find out what size was actually assigned.

24
  • This is not correct. A blocking mode socket will block until all the data is transferred into the socket send buffer. Source: Posix.
    – user207421
    Feb 28, 2015 at 21:22
  • See Edit 2 in my question.
    – user4344762
    Mar 1, 2015 at 0:49
  • @EJP: And that is not entirely correct, either. If the message is too long to be passed to the underlying protocol atomically, it shall fail. So, it might block, but not necessarily. (Which is what I guess is happening here, I think this 1G-send just failed. Only funny thing is that no error is reported, but quite possibly Winsock is buggy.)
    – Damon
    Mar 1, 2015 at 0:59
  • @Damon I do not think that send() has failed. I got the entire 1 GB at the other end (it returned immediately but kept sending!). I have discovered a strange "solution" to make send() work as expected, see Edit 2 in my question.
    – user4344762
    Mar 1, 2015 at 1:24
  • 1
    @Damon it is not that Google has accepted a 1 GB of data (of course it didn't!). It is that Windows seems to placing the 1 GB of data immediately in the send buffer. Try my code on your machine and see what happens.
    – user4344762
    Mar 1, 2015 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy