How can I set the buffer size for the underneath Socket UDP? C#(如何设置底层 Socket UDP 的缓冲区大小?C#)
问题描述
众所周知,对于 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
我的问题是如何设置 Socket 内部的缓冲区大小.我认为操作系统维护自己的缓冲区来接收 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
Dave 说 Socket.ReceiveBufferSize 可以为 UDP 设置内部缓冲区.我不同意.
Dave said that Socket.ReceiveBufferSize can set the internal buffer for UDP. I disagree.
我做的实验是这样的:
27 台主机同时(至少几乎)在一个 LAN 内向我发送一个 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.
有人可以帮忙吗?
推荐答案
我经常使用 .NET UDPClient 并且一直使用 Socket.ReceiveBufferSize 效果不错.在内部它调用 Socket.SetSocketOption使用 ReceiveBuffer 参数.下面是一些可以用来测试的快速、简单的代码:
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));
}
}
尝试调整传递给 ReceiveBufferSize 的值.我测试了在 5 秒内持续发送数据流,得到 10 个数据包.然后我增加了x4,下一次得到了38个数据包.
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.
我会查看您网络中可能丢弃数据包的其他地方.特别是因为您在 其他帖子您正在发送 10KB 数据包.当发送到 MTU 大小的数据包时,这 10KB 将被分段.如果该系列中的任何 1 个数据包被丢弃,则整个数据包都将被丢弃.
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.
这篇关于如何设置底层 Socket UDP 的缓冲区大小?C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何设置底层 Socket UDP 的缓冲区大小?C#
基础教程推荐
- 将 XML 转换为通用列表 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30