[ACCEPTED]-How to send large data using C# UdpClient?-udp

Accepted answer
Score: 11

For all those people saying to use TCP....are 34 foolishly wrong. Although TCP is reliable 33 and the window maintained by the kernel 32 it's fairly "set and forget" protocol, but 31 when it comes to the guy wanting to use 30 100% of his throughput, TCP will not do 29 (it throttles too hard, and the wait for 28 an ACK automatically puts at least 50% trashed 27 because of the RTT).

To the original question, you 26 are sending UDP packets nonstop in that 25 for-loop, the window fills up and then any 24 new data is dropped immediately and doesn't 23 even attempt to go on the line. You are 22 also splitting your data too large. I would 21 recommend building your own throttle mechanism 20 that starts off with 2k segments per second, and 19 slowly ramps up. Each "segment" contains 18 a SEQ (sequence identifier for acknowledgements 17 or ACK) and OFF (offset inside the file 16 for this data set). As the data is being 15 tagged, let the server keep track of these 14 tags. When the other side gets them, it 13 stores the SEQ numbers in an ACK list, and 12 any missing SEQ numbers are placed into 11 a NACK timer list, when the timer runs out 10 (if they haven't been received) it moves 9 to a NACK list. The receiver should send 8 5 or so ACKs from the ACK list along with 7 up to 5 NACKs in a single transmission every 6 couple seconds or so. If the sender receives 5 these messages and there are any NACKs, it 4 should immediately throttle down and resend 3 the missing fragment before continuing. The 2 data that is ACKed can be freed from memory.

Good 1 luck!

Score: 4

I don't know about .Net implementation specifically, it 21 might be buffering your data, but UDP datagram 20 is normally limited by the link MTU, which 19 is 1500 on normal ethernet (subtract 20 18 bytes for IP header and 8 bytes of UDP header.)

UDP 17 is explicitly allowed to drop and reorder 16 the datagrams, and there's no flow control 15 as in TCP.

Exceeding the socket send buffer 14 on the sender side will have the network 13 stack ignore following send attempts until 12 the buffer space is available again (you 11 need to check the return value of the send() for 10 that.)


I would strongly recommend going with 9 TCP for large file transfers. TCP gives 8 you sequencing (you don't have to keep track 7 of dropped and re-ordered packets.) It has 6 advanced flow control (so fast sender does 5 not overwhelm a slow receiver.) It also 4 does Path MTU discovery (i.e. finds out 3 optimal data packetization and avoids IP 2 fragmentation.) Otherwise you would have 1 to re-implement most of these features yourself.

Score: 2

I hate to say it but you need to sleep the 4 thread. You are overloading your throughput. UDP 3 is not very good for lossless data transfer. UDP 2 is for when you don't mind dropping some 1 packets.

Score: 0

Reliably - no, you won't do it with UDP.

As 3 far as I understand, this makes sense for 2 sending to multiple computers at a time 1 (broadcasting).

In this case,

  • establish a TCP connection with each of them,
  • split the data into blocks,
  • give each block an ID,
  • send list of IDs to each computer with TCP connection,
  • broadcast data with UDP,
  • inform clients (via TCP) that data transmission is over,
  • than clients should ask to resend the dropped packets

More Related questions