简体   繁体   中英

select fd_set order by message time of arrival

I have a select over two UDP sockets. Sometimes select returns 2, so both sockets are ready for reciving, but I want to recive first the oldest message avaliable to read. There is any way of archiving this?

I need to get the message that first arrived, first:

  • Message m1 at time t1 arrived in socket s1 .
  • Message m2 at time t2 arrived in socket s2 .

t1 < t2 : So I have to get message m1 first from the socket s1 .

Right now I have something similar to this:

recived do_recive(fd_set* container, int nfds, int* sockets, unsigned n_sockets) {

    // ...

    int activity = select(nfds, container, NULL, NULL, NULL);

    // ...

    for(i=0;i<n_sockets;i++) {
        if(FD_ISSET(sockets[i], container)) {
           recvfrom(...);
           break;
        }
    }

    // ...

}

If select returns 2, it means that your process was preempted (not ready to run) from the time that the first packet arrived to the time that the second packet arrived. So there's no way to know which arrived first. As far as your process is concerned, the two packets arrived simultaneously.

Also note that routers in the network can (and do) reorder and delay packets. So even if you sent packet A first and then B (half a second later) from a single computer, there's no guarantee that packet A will arrive before B. In general, if your code depends on the order in which UDP packets arrive, then it just isn't going to work in the real world.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM