如何设置底层 Socket UDP 的缓冲区大小? C#

发布于 2024-08-24 05:30:43 字数 1138 浏览 6 评论 0原文

正如我们所知,对于 UDP 接收,我们使用 Socket.ReceiveFrom 或 UdpClient.receive

Socket.ReceiveFrom 接受您发送的字节数组以放入 udp 数据。

UdpClient.receive 直接返回一个字节数组,其中数据是

我的问题是如何设置Socket内的缓冲区大小。我认为操作系统维护自己的缓冲区来接收 UDP 数据,对吧?例如,如果 udp 数据包发送到我的机器,操作系统会将其放入缓冲区并等待我们 Socket.ReceiveFrom 或 UdpClient.receive,对吗?

如何更改内部缓冲区的大小?

我试过Socket.ReceiveBuffSize,它对UDP完全没有影响,而且它明确表示它是针对TCP窗口的。我还做了很多实验,证明 Socket.ReceiveBufferSize 不适用于 UDP。

谁能分享一些关于 UDP 内部缓冲区的见解???

谢谢

我在这里看到了一些帖子,例如

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

Dave说Socket.ReceiveBufferSize可以设置内部缓冲区对于UDP。我不同意。

我做的实验是这样的:

一个局域网内有27台主机同时(至少几乎)向我发送一个10KB的udp数据包。我有一个 while 循环来处理每个数据包。对于每个数据包,我创建一个线程来处理它。我使用 UdpClient 或 Socket 来接收数据包。

我丢失了大约 50% 的数据包。我认为这是UDP发送的突发,我无法及时处理所有这些。

这就是为什么我想增加 UDP 缓冲区大小。比如说,如果我将缓冲区大小更改为 1MB,那么缓冲区中可以接受 27 * 10KB = 270KB 数据,对吧?

我尝试将 Socket.ReceiveBufferSize 更改为许多值,但它根本没有效果。

任何人都可以帮忙吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟酉 2024-08-31 05:30:43

我经常使用 .NET UDPClient,并且一直使用 Socket.ReceiveBufferSize 并有很好的效果。它在内部调用 Socket.SetSocketOption使用 ReceiveBuffer< /a> 参数。您可以使用以下一些快速、简单的代码进行测试:

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));
  }
}

尝试调整传递到 ReceiveBufferSize 的值。我测试了连续 5 秒发送数据流,收到了 10 个数据包。然后我增加了 4 倍,下次得到了 38 个数据包。

我会查看网络中可能丢失数据包的其他位置。特别是因为您在 其他帖子表明您正在发送 10KB 数据包。这10KB在发送到MTU大小的数据包时会被分片。如果该系列中的任何 1 个数据包被丢弃,则整个数据包都将被丢弃。

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.

长安忆 2024-08-31 05:30:43

设置 ReceiveBufferSize 的问题是您需要在创建 UdpClient 对象后直接设置它。我遇到了同样的问题,在获取 ReceiveBufferSize 的值时,我的更改没有得到反映。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文