简体   繁体   English

如何设置Socket UDP下面的缓冲区大小? C#

[英]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 正如我们所知道的UDP接收,我们使用Socket.ReceiveFrom或UdpClient.receive

Socket.ReceiveFrom accept a byte array from you to put the udp data in. Socket.ReceiveFrom接受你的一个字节数组来放入udp数据。

UdpClient.receive returns directly a byte array where the data is UdpClient.receive直接返回数据所在的字节数组

My question is that How to set the buffer size inside Socket. 我的问题是如何设置Socket内的缓冲区大小。 I think the OS maintains its own buffer for receive UDP data, right? 我认为操作系统维护自己的缓冲区来接收UDP数据,对吧? for eg, 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? 例如,如果将udp数据包发送到我的机器,操作系统会将它放到缓冲区并等待我们进入Socket.ReceiveFrom或UdpClient.receive,对吧?

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. 我尝试过Socket.ReceiveBuffSize,它对UDP没有任何影响,它清楚地说它是用于TCP窗口的。 Also I have done a lot of experiments which proves Socket.ReceiveBufferSize is NOT for UDP. 此外,我做了很多实验,证明Socket.ReceiveBufferSize不适用于UDP。

Can anyone share some insights for UDP internal buffer??? 任何人都可以分享UDP内部缓冲区的一些见解???

Thanks 谢谢

I have seen some posts here, for eg, 我在这里看过一些帖子,例如,

http://social.msdn.microsoft.com/Forums/en-US/ncl/thread/c80ad765-b10f-4bca-917e-2959c9eb102a 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. Dave说Socket.ReceiveBufferSize可以为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). 27台主机同时在局域网内向我发送一个10KB的udp数据包(至少差不多)。 I have a while-loop to handle each of the packet. 我有一个while循环来处理每个数据包。 For each packet, I create a thread a handle it. 对于每个数据包,我创建一个处理它的线程。 I used UdpClient or Socket to receive the packets. 我使用UdpClient或Socket接收数据包。

I lost about 50% of the packets. 我丢失了大约50%的数据包。 I think it is a burst of the UDP sending and I can't handle all of them in time. 我认为这是UDP发送的爆发,我无法及时处理所有这些。

This is why I want to increase the buffer size for UDP. 这就是为什么我想增加UDP的缓冲区大小。 say, if I change the buffer size to 1MB, then 27 * 10KB = 270KB data can be accepted in the buffer, right? 比方说,如果我将缓冲区大小更改为1MB,那么缓冲区中可以接受27 * 10KB = 270KB的数据,对吗?

I tried changing Socket.ReceiveBufferSize to many many values, and it just does not have effects at all. 我尝试将Socket.ReceiveBufferSize更改为许多值,它根本就没有效果。

Any one can help? 任何人都可以帮忙吗?

I use the .NET UDPClient often and I have always used the Socket.ReceiveBufferSize and have good results. 我经常使用.NET UDPClient ,并且我一直使用Socket.ReceiveBufferSize并且效果很好。 Internally it calls Socket.SetSocketOption with the ReceiveBuffer parameter. 在内部,它使用ReceiveBuffer参数调用Socket.SetSocketOption 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. 尝试调整传递给ReceiveBufferSize的值。 I tested sending a constant stream of data for the 5 seconds, and got 10 packets. 我测试了5秒内发送一个恒定的数据流,并获得了10个数据包。 I then increased x4 and the next time got 38 packets. 然后我增加了x4,下一次获得了38个数据包。

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. 特别是因为您在其他帖子中提到您正在发送10KB数据包。 The 10KB will be fragmented when it is sent to packets the size of MTU. 当将10KB发送到MTU大小的数据包时,它将被分段。 If any 1 packet in the series is dropped the entire packet will be dropped. 如果系列中的任何1个数据包被丢弃,则整个数据包将被丢弃。

The issue with setting the ReceiveBufferSize is that you need to set it directly after creation of the UdpClient object. 设置ReceiveBufferSize的问题是您需要在创建UdpClient对象后直接设置它。 I had the same issue with my changes not being reflected when getting the value of ReceiveBufferSize. 我在获取ReceiveBufferSize的值时没有反映出我的更改时遇到了同样的问题。

UdpClient client = new UdpClient()
//no code inbetween these two lines accessing client.
client.Client.ReceiveBufferSize = somevalue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM