I have a UDP connection that connects an echo server with a test client. test client continuously sends packets and waits about a second for receiving them. if it can't get the packet, assumes that packet is lost and sends another packet. Most of the packets are sent and received successfully but some of them that assumed to be lost, will be receive in client after sending next packet. actually they are received with delay. my send and receive function run on different thread. what can I do for eliminating these delayed packets? (my program is running on my localhost- so having lost packets is not reasonable)
0
votes
Choice only one language, UDP is UDP if you need more feature look for another protocol as TCP.
– Stargateur
Sequence numbers. If you receive a reply-packet with a sequence number lower than the last you sent out, then just ignore it.
– Some programmer dude
If you are dealing with UDP you should be ready for packets getting lost, reordered and duplicated. "my send and receive function run on different thread" remark makes me think that this is actually a multithreading issue in your application, not packets getting lost (which is indeed unlikely to happen on localhost).
– user7860670
it seems that packet with lower sequence number arrives sooner, @Someprogrammerdude
– samini
What do you mean by that? Lets say that you send out packet #1 and get a reply. Then you send out packet #2, but the reply is delayed. So you continue by sending out packet #3, but before you get its reply you get the reply for #2. Then just ignore #2, and continue waiting for #3. And so on. If you receive #2 before you send #3, then all is well.
– Some programmer dude
3 Answers
0
votes
Most likely you will not be able to prevent package delays (maybe due to buffers in the network interface, kernel, application stack). UDP is unreliable, if you need to make sure that every package is delivered you should use TCP in general. Otherwise there would be options like using sequence numbers or a more elaborate protocol implmenetation to make sure that you are not out of sequence (like a confirmation for each package).
What you describe sounds more like using TCP would be appropriate though.
0
votes