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

发布于 2024-08-24 06:16:02 字数 1137 浏览 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???

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技术交流群

发布评论

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

评论(1

蓝天白云 2024-08-31 06:16:02

我偶尔会丢失数据包,通过增加缓冲区我取得了成功。

我使用了 UdpClient,并在它的套接字上设置了一个比默认值(8192 字节)更大的值。这为我解决了问题。

在我的场景中可以清楚地跟踪到,尺寸越小,掉落的次数越多,越大,则越少。我最终为我的应用程序选择了 2^18 值。

I have had occasionally dropped packets, and I had success by increasing the buffer.

I used UdpClient, and on it's socket, I set a larger value than the default (which was 8192 bytes). This solved the problem for me.

In my scenario could clearly track that the smaller the size, the more often the drops, the larger, the fewer. I finally chose a value of 2^18 for my application.

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