views:

524

answers:

1

Hi all

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?

+1  A: 

I use the .NET UDPClient often and I have always used the Socket.ReceiveBufferSize and have good results. Internally it calls Socket.SetSocketOption with the ReceiveBuffer parameter. Here is a some quick, simple, code you can test with:

public static void Main(string[] args)
{
  IPEndPoint remoteEp = null;
  UdpClient client = new UdpClient(4242);
  client.Client.ReceiveBufferSize = 4096;

  Console.Write("Start sending data...");
  client.Receive(ref remoteEp);
  Console.WriteLine("Good");

  Thread.Sleep(5000);
  Console.WriteLine("Stop sending data!");
  Thread.Sleep(1500);

  int count = 0;
  while (true)
  {
    client.Receive(ref remoteEp);
    Console.WriteLine(string.Format("Count: {0}", ++count));
  }
}

Try adjusting the value passed into the ReceiveBufferSize. I tested sending a constant stream of data for the 5 seconds, and got 10 packets. I then increased x4 and the next time got 38 packets.

I would look to other places in your network where you may be dropping packets. Especially since you mention on your other post that you are sending 10KB packets. The 10KB will be fragmented when it is sent to packets the size of MTU. If any 1 packet in the series is dropped the entire packet will be dropped.

heavyd
@heavyd: thanks for your reply.My code is very similar to yours. but if you are interested, you can do a simple experiment.we have separate two classes - Server and Client.in your Client.cs, you simply use UdpClient to send 200 packet (10KB each) to your local port 8888. (whatever port you want)in your Server.cs, you do constantly receive just like shown in your codeof course, please run your server.cs first.my result is 90% of the packets can be received. The point is the UDP sending is a burst that too many packets arrive and your server cannot handle. and the buffer overflows.
Jack
Yes, and are you saying that adjusting ReceiveBufferSize from a value, say `4096` to a higher value, say `4 * 4096` does nothing?
heavyd
@heavyd, can I know what's the upper limit for ReceiveBufferSize?
Jack
@heavyd: in the local experiment above, it has effect that without setting ReceiveBufferSize, 90% of the packets are received. If I increase it to 16*4096, all can be received. but in my original experiment, it does not work.
Jack
@Jack, I'm not for sure on the upper limit. My initial guess would be 64K, but I can't find any supporting docs. After your experiment, are you sure that your network is not dropping the packets before they get to your computer. You may be saturating a switch somewhere in the mix and loosing packets there rather than of the stack on your computer.
heavyd
@heavyd, no, I am not sure. I just think it is not so likely because it is Lan. Anyway, can I ask you that if my upload bandwidth is 50KB/s, and I send out 27 packets (10KB each) to you at veryvery fast speed. will you be able to receive them all? I just want to figure out how udp works. Will socket buffer the sending?
Jack
@Jack, assuming all 27 packets are sent in 1 second: 10KB * 27 = 270KB/sec > 50 KB/sec, so very likely you would drop packets. And the thing to remember about UDP is that delivery is not guaranteed like TCP, so once its dropped, it is gone and you have no way of knowing that it was dropped.
heavyd