Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reliable udp in c#

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.

from researching on the internet I believe udp has two problems:


  1. it does not ensure that all data reaches it's destination.
  2. data may reach it's destination on a different order
  3. maybe there is a third problem that I am missing in order to make it reliable

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 this question. Believe me, I been trying to do tcp punch holing for so long.

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:


"Imagine there is computer A and computer B and computer A is the one that is sending the file to computer B";

here are the steps that I been thinking of:


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.


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))


3) imaging the hash of those first 500 bytes comes out to be kj82lkdi930fi1.


4) computer B should be listening for a hash and bytes.


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.


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}


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.


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.


8) computer A sends the next 500 bytes with its hash code.


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


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.


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}


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


11) computer A sends the same bytes and hash for the 3th time.


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)


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.


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.


15) the algorithm continues like so until the file get's transferred.


.

.

.

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.

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#.

like image 429
Tono Nam Avatar asked Nov 04 '22 14:11

Tono Nam


1 Answers

The third problem is that data can be corrupted when you receive it.

You can start by reading TCP RFC just to understand how TCP makes communication reliable. Having that knowledge you can implement some of its techniques using UDP as transport.

Also take a look at this UDP network library http://code.google.com/p/lidgren-network-gen3/

like image 78
Vadym Stetsiak Avatar answered Nov 18 '22 07:11

Vadym Stetsiak