Thread: out-of-order packets

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    1

    out-of-order packets

    How to ensure my udp sockets receive all packets in order? Assuming the packets are guarantee arrive in order at the receiver machine, and no packet drop.

    I am asking this because I ran my program many times in window, and redhat. It always receive in-order packets under windows, on the other hand, out-of-order packet always happen under redhat as well as ubuntu.

    I am using boost asio library to create 50 udp sockets, receive rate set to 100 packet/sec. I also have tried without boost asio, and use a boost thread to listen to an epoll, and call recv to fd immediately, but yielded the same result. In addition, If I only open 8 sockets in linux, i receive no out-of-order packets.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    UDP does not guarantee that packets are received in order, or even received at all: User Datagram Protocol - Wikipedia, the free encyclopedia

    So to guarantee this you will either have to implement it yourself, or use TCP.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Or use SCTP, which preserves both message boundaries (which TCP does not) and message order (which UDP does not).
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Bryan Fok View Post
    How to ensure my udp sockets receive all packets in order? Assuming the packets are guarantee arrive in order at the receiver machine, and no packet drop.
    Even with those assumptions in place, UDP packets logically have no relation to each other, so the kernel doesn't have to make any special effort to ensure they are seen by the application in the same order they are seen by the network interface. You'd think that would be the case, but there are all sorts of reasons why it might not be. For instance, the packets might be queued in a LIFO fashion instead of a FIFO, simply because that was easier to implement and permitted to do.

    You're relying on undefined behavior by relying on the order of UDP packets.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    If they must be in order then attach a value that indicates the number of the packet in the sequence and re-order them yourself when they're received. Also, instead of assuming that packets won't be dropped, decide whether you can afford to drop them, and if you can't, send something back indicating that the other side successfully received the packet, and keep sending the same data until that is received. You can safely fit around 512 bytes into a UDP datagram, so keep that in mind when you're designing your protocol.
    Last edited by Barney McGrew; 02-06-2013 at 04:56 PM.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    or use Reliable Datagram Sockets, although it's unclear to me whether it enforces packet ordering.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 04-01-2011, 04:13 PM
  2. Replies: 4
    Last Post: 03-06-2008, 03:38 PM
  3. TCP/IP packets
    By Zairus in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2006, 05:35 AM
  4. Raw Packets
    By Born_2B_Alone in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-04-2005, 08:04 PM
  5. what is the significance of low order and high order bits
    By Shadow12345 in forum Windows Programming
    Replies: 1
    Last Post: 11-16-2002, 11:46 AM