如何设置底层Socket UDP的缓冲区大小? [英] How can I set the buffer size for the underneath Socket UDP?

查看:756
本文介绍了如何设置底层Socket UDP的缓冲区大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

Socket.ReceiveFrom接受您的字节数组,以将udp数据放入其中。

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

UdpClient.receive直接返回数据所在的字节数组

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

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

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?

我已经尝试过Socket.ReceiveBuffSize,它对UDP完全没有影响,并且它显然说这是为了TCP窗口。我也做了很多实验,证明Socket.ReceiveBufferSize不适用于UDP。

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.

任何人都可以分享一些有关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

戴夫说Socket.ReceiveBufferSize可以为UDP设置内部缓冲区。我不同意。

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

我所做的实验是这样的:

The experiment I did is like this:

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

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.

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

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.

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

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?

我尝试将Socket.ReceiveBufferSize更改为很多许多值,但根本没有作用。

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

任何人都可以帮忙吗?

推荐答案

我偶尔会丢弃数据包,并且通过增加缓冲区获得了成功。

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

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

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.

在我的场景中,可以清楚地看到大小越小液滴越多,则液滴越大。我终于为我的应用选择了2 ^ 18的值。

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.

这篇关于如何设置底层Socket UDP的缓冲区大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆