title: Reliable udp in c#
body: <p>There are several articles on the internet about how to make udp reliable. I have not been able to find one on c#. So maybe I can implement my algorithm. </p>
  
  <p>from researching on the internet I believe udp has two problems:</p>
  
  <hr>
  
  <ol>
  <li>it does not ensure that all data reaches it's destination.</li>
  <li>data may reach it's destination on a different order</li>
  <li>maybe there is a third problem that I am missing in order to make it reliable</li>
  </ol>
  
  <hr>
  
  <p>if you are interested in knowing why I want to make udp reliable and why I don't use tcp instead take a look at <a href="https://stackoverflow.com/questions/7225150/forward-traffic-from-port-x-to-computer-b-with-c-udp-punch-hole-into-firewall">this question</a>. Believe me, I been trying to do tcp punch holing for so long. </p>
  
  <p>anyways maybe there is already a library that I can use with c# that will enable me to do this. Because I have note been able to find a library I been thinking about the following algorithm:</p>
  
  <hr>
  
  <p>"Imagine there is computer A and computer B and computer A is the one that is sending the file to computer B";</p>
  
  <p>here are the steps that I been thinking of:</p>
  
  <hr>
  
  <p>1) computer A opens the file for reading and let's say it is 5000 bytes. that means that computer A will have to send 5000 bytes to computer B making sure no bytes are lost and also in the right order.</p>
  
  <hr>
  
  <p>2) computer A get's the first 500 bytes of the file and it get's the hash of those bytes. so now computer A has two things the hash of those 500 bytes and also the bytes. (the hash will be an efficient algorithm such as md5 to make sure data got received in the right order. that is md5(1,2,3) != md5(2,1,3))</p>
  
  <hr>
  
  <p>3) imaging the hash of those first 500 bytes comes out to be kj82lkdi930fi1. </p>
  
  <hr>
  
  <p>4) computer B should be listening for a hash and bytes.</p>
  
  <hr>
  
  <p>5) computer A sends the hash to computer B. and it sends the 500 bytes too. as soon as it sends that it start's waiting for a reply.</p>
  
  <hr>
  
  <p>6) computer B should now receive the hash and the bytes. computer b performs the same algorithm md5 on the received bytes. if that result is equal to the hash that was received then it replies back to A with {1,1,1,1,1,1} otherwise it replies with {2,2,2,2,2,2,2}</p>
  
  <hr>
  
  <p>6.5) let's assume computer B got the data on the right order so it replies {1,1,1,1,1,} it also saves the hash code on memory or array. </p>
  
  <hr>
  
  <p>7) computer A should be waiting for a response in order to send the next 500 bytes. let's say that it receives {1,1,1}. because it received a 1 it knows it can proceed and send the next 500 bytes with a new hash code of those 500 bytes. </p>
  
  <hr>
  
  <p>8) computer A sends the next 500 bytes with its hash code.</p>
  
  <hr>
  
  <p>9) let's pretend computer B did not receive the data so it does not reply back to A. computer B will still wait for bytes and a hash</p>
  
  <hr>
  
  <p>8) since computer A has not receive a 1,1,1,1,1, or 2,2,2,2,2, for a reasonable amount of time, then A will send the same bytes and hash again for a second time.</p>
  
  <hr>
  
  <p>9) let's assume computer B receives the hash and the bytes but the bytes got received on a different order.  when computer B calculates the hash on those bytes then that hash will not match the hash that was received. as a result it will reply back with {2,2,2,2,2,2}</p>
  
  <hr>
  
  <p>10) if computer A receives the 2,2,2,2,2,2 then it will send the same bytes and hash. if it did not receive the 2,2,2,2,2 for some reason then it will send the same bytes and hash after some period of time. let's pretend computer A receives 2,2,2,2,2 </p>
  
  <hr>
  
  <p>11) computer A sends the same bytes and hash for the 3th time.</p>
  
  <hr>
  
  <p>12) computer B receives the hash and bytes on the right order. as a result it replies 1,1,1,1,1,1 and saves that previous hash on memory. (recall step 6.5)</p>
  
  <hr>
  
  <p>13) lets pretend computer A did not receive the 1,1,1,1 response from B. It will then send the same bytes for the forth time.</p>
  
  <hr>
  
  <p>14) computer B checks the hashes and if it is equal to the last one that was accepted then it replies 1,1,1,1 again without writing those bytes to the file. </p>
  
  <hr>
  
  <p>15) the algorithm continues like so until the file get's transferred.</p>
  
  <hr>
  
  <p>.</p>
  
  <p>.</p>
  
  <p>.</p>
  
  <p>I mean there are obviously some other things that I need to add to this algorithm such as letting computer B know when the transfer is done. maybe checking for more errors. what happens if computer A get disconnected for a long time. But the main protocol will be something like the one I described. </p>
  
  <p>So do you think I should start implementing this algorithm? should I increase and send more bytes every time. I mean send 1000 instead of 500? There are lots of articles on the internet that tell you about several techniques but very few of them give you a working example on the language that you want. In this case I need this in c#. </p>