How to send large data using C# UdpClient?(如何使用 C# UdpClient 发送大数据?)
问题描述
我正在尝试使用 C# UdpClient 发送大量数据(超过 50 MB).
I'm trying to send a large amount of data (more than 50 MB) using C# UdpClient.
所以一开始我把数据分成65507字节的块,循环发送.
So at first I split the data into 65507 byte blocks and send them in a loop.
for(int i = 0; i < packetCount; i++)
myUdpClient.Send(blocks[i], block[i].Length, remoteEndPoint);
我的问题是只能接收到第一个数据包.在发送第一个数据包期间,网络负载迅速增加到 100%,然后无法接收其他数据包.
My problem is that only the first packets can be received. During sending the first packet the networkload increases rapidly to 100%, and then the other packets cannot be received.
我想获得尽可能多的数据吞吐量.
I want to get as much data throughput as possible.
对不起我的英语!提前感谢您的帮助.
I'm sorry for my English! Thanks for your help in advance.
推荐答案
我不知道具体的.Net实现,它可能正在缓冲你的数据,但是UDP数据报通常受到链接MTU的限制,即1500 on普通以太网(减去 20 字节的 IP 标头和 8 字节的 UDP 标头.)
I don't know about .Net implementation specifically, it might be buffering your data, but UDP datagram is normally limited by the link MTU, which is 1500 on normal ethernet (subtract 20 bytes for IP header and 8 bytes of UDP header.)
明确允许 UDP 丢弃和重新排序数据报,并且没有 TCP 中的流量控制.
UDP is explicitly allowed to drop and reorder the datagrams, and there's no flow control as in TCP.
超过发送方的套接字发送缓冲区将在发送尝试后忽略网络堆栈,直到缓冲区空间再次可用(您需要检查 send()
的返回值.)
Exceeding the socket send buffer on the sender side will have the network stack ignore following send attempts until the buffer space is available again (you need to check the return value of the send()
for that.)
我强烈建议使用 TCP 进行大文件传输.TCP 为您提供排序(您不必跟踪丢弃和重新排序的数据包.)它具有高级流量控制(因此快速发送方不会压倒慢速接收方.)它还进行路径 MTU 发现(即找出最优数据打包并避免 IP 碎片.)否则您将不得不自己重新实现这些功能.
I would strongly recommend going with TCP for large file transfers. TCP gives you sequencing (you don't have to keep track of dropped and re-ordered packets.) It has advanced flow control (so fast sender does not overwhelm a slow receiver.) It also does Path MTU discovery (i.e. finds out optimal data packetization and avoids IP fragmentation.) Otherwise you would have to re-implement most of these features yourself.
这篇关于如何使用 C# UdpClient 发送大数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 C# UdpClient 发送大数据?


- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- C# 中多线程网络服务器的模式 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01