Ensuring packet order in UDP

31,297

Solution 1

UDP is a lightweight protocol that by design doesn't handle things like packet sequencing. TCP is a better choice if you want robust packet delivery and sequencing.

UDP is generally designed for applications where packet loss is acceptable or preferable to the delay which TCP incurs when it has to re-request packets. UDP is therefore commonly used for media streaming.

If you're limited to using UDP you would have to develop a method of identifying the out of sequence packets and resequencing them.

Solution 2

UDP does not guarantee that your packets will arrive in order. (It does not even guarantee that your packets will arrive at all.) If you need that level of robustness you are better off with TCP. Alternatively you could add sequence markers to your datagrams and rearrange them at the other end, but why reinvent the wheel?

Solution 3

is there a way to make sure winsock and send() will send the packets the same way they got there?

It's called TCP.

Alternatively try a reliable UDP protocol such as UDT. I'm guessing you might be on a small embedded platform so you want a more compact protocol like Bell Lab's RUDP.

Share:
31,297
Davidallencoe
Author by

Davidallencoe

Updated on July 05, 2022

Comments

  • Davidallencoe
    Davidallencoe almost 2 years

    I'm using 2 computers with an application to send and receive udp datagrams. There is no flow control and ICMP is disabled. Frequently when I send a file as UDP datagrams via the application, I get two packets changing their order and therefore - packet loss.

    I've disabled and kind of firewall and there is no hardware switch connected between the computers (they are directly wired).

    Is there a way to make sure Winsock and send() will send the packets the same way they got there?

    Or is the OS doing that?

    Or network device configuration needed?

    • user1464603
      user1464603 over 3 years
      In a point-to-point connection I would expect the packets to be in order, that's an assumption that proved 100% right with my experience with embedded systems. Could it be Receive Side Scaling causing what you see?
  • Davidallencoe
    Davidallencoe over 13 years
    from a praticular reason i cant describe im limited to udp only. a rare packetloss once and then is accaptable. it just that 1 of 3 files gets a loss. i just need to find a way for the sender to send the packets in the right order, the rest doesnt matter for me. thanks
  • crazyscot
    crazyscot over 13 years
    It's not so much a question of the sender sending the packets in the right order as for the receiver to reassemble them into the correct order. Like I said, add sequence markers at some appropriate level.
  • Donal Fellows
    Donal Fellows over 13 years
    UDP does guarantee that a packet will arrive intact or not at all (i.e., it has a checksum) and it also adds port numbers to raw IP. It doesn't guarantee delivery or sequencing; those are what TCP adds (by basically shouting out a packet until the other end says that it has arrived). Guaranteed correct in-order delivery is also enough that you can pretend you've got a stream of data (hence TCP is a streaming socket, since that's pretty commonly desired).