Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the buffer size for the underneath Socket UDP? C#

As we know for UDP receive, we use Socket.ReceiveFrom or UdpClient.receive

Socket.ReceiveFrom accept a byte array from you to put the udp data in.

UdpClient.receive returns directly a byte array where the data is

My question is that How to set the buffer size inside Socket. I think the OS maintains its own buffer for receive UDP data, right? for e.g., if a udp packet is sent to my machine, the OS will put it to a buffer and wait us to Socket.ReceiveFrom or UdpClient.receive, right?

How can I change the size of that internal buffer?

I have tried Socket.ReceiveBuffSize, it has no effect at all for UDP, and it clearly said that it is for TCP window. Also I have done a lot of experiments which proves Socket.ReceiveBufferSize is NOT for UDP.

Can anyone share some insights for UDP internal buffer???

Thanks

I have seen some posts here, for e.g.,

http://social.msdn.microsoft.com/Forums/en-US/ncl/thread/c80ad765-b10f-4bca-917e-2959c9eb102a

Dave said that Socket.ReceiveBufferSize can set the internal buffer for UDP. I disagree.

The experiment I did is like this:

27 hosts send a 10KB udp packet to me within a LAN at the same time (at least almost). I have a while-loop to handle each of the packet. For each packet, I create a thread a handle it. I used UdpClient or Socket to receive the packets.

I lost about 50% of the packets. I think it is a burst of the UDP sending and I can't handle all of them in time.

This is why I want to increase the buffer size for UDP. say, if I change the buffer size to 1MB, then 27 * 10KB = 270KB data can be accepted in the buffer, right?

I tried changing Socket.ReceiveBufferSize to many many values, and it just does not have effects at all.

Any one can help?

like image 627
Jack Avatar asked Mar 09 '10 10:03

Jack


1 Answers

The issue with setting the ReceiveBufferSize is that you need to set it directly after creation of the UdpClient object. I had the same issue with my changes not being reflected when getting the value of ReceiveBufferSize.

UdpClient client = new UdpClient()
//no code inbetween these two lines accessing client.
client.Client.ReceiveBufferSize = somevalue
like image 122
Vort3x Avatar answered Oct 15 '22 05:10

Vort3x