0
votes

I have a C# UDP socket hosted on azure cloud that receives UDP datagrams using UdpClient.receive. There could be thousands of clients sending messages to the socket concurrently and each client sends datagrams with a size of around 1Kb.

I am confused to whether the ReceiveBufferSize applies to the size of each datagram or if it sets the size of the receive queue underneath? By receive queue, I mean the queue that is used by the .NET runtime to enqueue datagrams from different clients that hasn't been read by UdpClient.receive yet.

I read in some posts that the ReceiveBufferSize only applies to the size of individual datagrams, if that's the case, how would we set the size of the receive queue underneath?

I have set the ReceiveBufferSize to 65Kb. If the ReceiveBufferSize applies to the queue size under the hood, and each client sends a 1Kb datagram at the same time, does this mean the socket can only handle data from 65 clients? If there are more clients sending at the same time the buffer overflows and datagrams are lost?

1

1 Answers

3
votes

Presumably you mean UdpClient.Client.ReceiveBufferSize, i.e. Socket.ReceiveBufferSize?

If you chase that call through, you end up at a call to setsockopt here, with the SO_RCVBUF option.

Researching that option, you end up at this answer from @DS:

SO_RCVBUF is simpler to understand: it is the size of the buffer the kernel allocates to hold the data arriving into the given socket during the time between it arrives over the network and when it is read by the program that owns this socket. With TCP, if data arrives and you aren't reading it, the buffer will fill up, and the sender will be told to slow down (using TCP window adjustment mechanism). For UDP, once the buffer is full, new packets will just be discarded.

That suggests it's the size of the entire queue associated with the socket, i.e. how large the buffer that the OS will put received UDP datagrams into for you to read is. Once this is full, additional datagrams will be discarded.