如何使用 C# UdpClient 发送大数据? [英] How to send large data using C# UdpClient?

查看:20
本文介绍了如何使用 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 发送大数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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