Reliable udp in C #

There are several articles on the Internet on how to make udp reliable. I could not find it in C #. Therefore, perhaps I can implement my algorithm.

from research on the Internet I believe that UDP has two problems:


  • it does not guarantee that all data will achieve the goal.
  • the data may reach the addressee in a different order.
  • maybe there is a third problem that I am missing in order to make it reliable.

if you are interested to know why I want to make udp reliable and why I do not use tcp, do not look at this question . Believe me, I tried to do tcp punch for so long.

In any case, there may already be a library that I can use with C # that will allow me to do this. Since I noticed that I could find the library, I was thinking about the following algorithm:


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

here are the steps i was thinking about:


1) Computer A opens the file for reading and allows you to say that it is 5000 bytes. this means that computer A will have to send 5,000 bytes to computer B to ensure that no bytes are lost, and also in the correct order.


2) Computer A receives the first 500 bytes of the file and receives a hash of these bytes. so now computer A has two things: a hash of these 500 bytes, and also bytes. (the hash will be an efficient algorithm such as md5 to ensure that the data is received in the correct order, i.e. md5 (1,2,3)! = md5 (2,1,3))


3) the hash image of these first 500 bytes is obtained by kj82lkdi930fi1.


4) Computer B must listen for the hash and bytes.


5) computer A sends a hash to computer B. and it also sends 500 bytes. as soon as he sends out that he starts to wait for an answer.


6) Computer B should now receive a hash and bytes. computer b executes the same md5 algorithm on received bytes. if this result is equal to the received hash, then it answers A with {1,1,1,1,1,1,1}, otherwise it answers {2,2,2,2,2,2,2,2}


6.5) suppose that computer B received the data in the correct order, so it answers {1,1,1,1,1}, it also stores the hash code in memory or array.


7) Computer A must wait for a response in order to send the next 500 bytes. let's say that he gets {1,1,1}. because he received 1, he knows that he can continue and send the next 500 bytes with a new hash code of these 500 bytes.


8) Computer A sends the next 500 bytes with a hash code.


9) suppose that computer B did not receive data, so it does not respond to A. computer B will still wait for bytes and a hash


8), since computer A does not receive 1,1,1,1,1,1 or 2,2,2,2,2,2 within a reasonable amount of time, then A will send the same bytes and hash again a second time.


9) suppose that computer B receives the hash and bytes, but the received bytes are in a different order. when computer B calculates the hash on these bytes, then this hash will not match the accepted hash. as a result, he will respond with {2,2,2,2,2,2}


10) if computer A receives 2,2,2,2,2,2,2 then it will send the same bytes and hash. if he did not receive 2,2,2,2,2 for some reason, then he will send the same bytes and hash after some period of time. let them give out computer A gets 2,2,2,2,2,2


11) Computer A sends the same bytes and hash for the 3rd time.


12) Computer B receives the hash and bytes in the correct order. as a result, it answers 1,1,1,1,1,1 and stores this previous hash in memory. (recall step 6.5)


13) allows you to pretend that computer A did not receive a response 1,1,1,1 from B. Then it will send the same bytes for the fourth time.


14) Computer B checks the hashes, and if it is equal to the last one that was accepted, it again responds to 1,1,1,1 without writing these bytes to the file.


15) the algorithm continues until the file is transferred.


.

.

.

I mean, there are some other things that I need to add to this algorithm, for example, let computer B know when the transfer will be completed. perhaps to check for errors. what happens if computer A disconnects for a long time. But the main protocol will be similar to the one I described.

Do you think I should start applying this algorithm? I have to increment and send more bytes every time. I mean send 1000 instead of 500? There are many articles on the Internet telling you about several methods, but very few of them give you an example of working in the language you want. In this case, I need this in C #.

+4
source share
1 answer

The third problem is that the data may be damaged when you receive it.

You can start by reading TCP RFC to understand how TCP makes communications reliable. With this knowledge, you can implement some of your methods using UDP as a transport.

Also check out this UDP network library http://code.google.com/p/lidgren-network-gen3/

+2
source

All Articles