title: Understanding how to send larger data chunks over UDP reliably
body: <p>I've used TCP for many things over the years and understand it pretty well. I now have a requirement to use UDP.</p>
  
  <p>Short version: A server allows a small number of clients (5-10) to connect. The server is running a simulation. Clients should be able to update parameters for the simulation and see (a subset) of the simulation results.</p>
  
  <p>In this instance, timing (when the parameters change) is important and the delay between the client requesting a change and it being implemented must be as low as possible.</p>
  
  <p>I've been <a href="http://www.codeproject.com/Articles/2614/Testing-TCP-and-UDP-socket-servers-using-C-and-NET" rel="nofollow noreferrer">doing</a> <a href="http://social.msdn.microsoft.com/Forums/en-US/92846ccb-fad3-469a-baf7-bb153ce2d82b/simple-udp-example-code?forum=netfxnetcom" rel="nofollow noreferrer">a</a> <a href="http://gafferongames.com/networking-for-game-programmers/virtual-connection-over-udp/" rel="nofollow noreferrer">lot</a> <a href="http://www.erg.abdn.ac.uk/~gorry/course/inet-pages/udp.html" rel="nofollow noreferrer">of</a> <a href="https://stackoverflow.com/questions/16652298/is-it-possible-to-send-large-udp-packets-by-setting-ip-header-to-enable-ip-fragm">reading</a> and I'm still not "getting it".</p>
  
  <p>Can someone please confirm/deny my understanding...</p>
  
  <ul>
  <li>A datagram is stored inside a single packet</li>
  <li>The largest payload I can reliably send is 506 bytes (576 MTU - 60 IP header - 8 UDP header)</li>
  <li>Sending more than that may cause fragmentation</li>
  <li>Fragmentation isn't handled at a lower level and would require me to recombine datagrams (Not sure about this - if it's handled automatically, why do I care about fragmentation?)</li>
  <li>I need to implement my own ACK/Throttling mechanism</li>
  </ul>
  
  <p>So... If I want to send (say) 800 bytes of data from the client to the server, I need to:</p>
  
  <ul>
  <li>Determine an arbitrary "Protocol" id to be used represented by a Byte(2) that is common between client and server and is used to filter out messages not meant for my app.</li>
  </ul>
  
  <h3>Client</h3>
  
  <ul>
  <li>Create a random message id</li>
  <li>Split the data into two, add the message id and a global sequence so they can be rejoined at the other end</li>
  <li>Record the data against the sequence id somewhere in memory</li>
  <li>Send them to the server</li>
  <li>If an Ack isn't received in a given timespan (Say RTT * 3), resend that packet.</li>
  </ul>
  
  <h3>Server</h3>
  
  <ul>
  <li>Inside simulation loop, check (non-blocking) if there is a message on the socket.</li>
  <li>If so, immediately send back a new packet containing an ACK for the sequence Id (actually, to mitigate Ack packet loss, I should Ack the last 30 or so received packets)</li>
  <li>Store the packet in memory until I've received the 2nd half</li>
  <li>Combine the two and process the payload</li>
  </ul>
  
  <p>For messages going in the other direction, I need to do exactly the same in reverse.</p>
  
  <p>I can't help feeling I'm missing something and don't quite understand the implications of a packet fragmenting. Can someone please clarify / point to a better resource?</p>