简体   繁体   English

通过消息到达时间选择fd_set顺序

[英]select fd_set order by message time of arrival

I have a select over two UDP sockets. 我可以select两个UDP套接字。 Sometimes select returns 2, so both sockets are ready for reciving, but I want to recive first the oldest message avaliable to read. 有时select返回2,因此两个套接字都准备好接收,但是我想首先获取可用的最旧的消息。 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 . 消息m1在时间t1到达套接字s1
  • Message m2 at time t2 arrived in socket s2 . 消息m2在时间t2到达套接字s2

t1 < t2 : So I have to get message m1 first from the socket s1 . t1 < t2 :所以我必须首先从套接字s1获得消息m1

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. 如果select返回2,则意味着从第一个数据包到达之时到第二个数据包到达之时,您的进程已被抢占(未准备好运行)。 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. 因此,即使您是从一台计算机先发送数据包A,然后又发送数据包B(之后又是第二秒),也无法保证数据包A会先于B到达。通常,如果您的代码取决于UDP数据包到达的顺序,则它只是在现实世界中行不通。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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