How do I wait for particular response without neglecting other responses using select in c sockets?
<p>I have a single client program where I need to <code>send</code> a request and timed wait for the corresponding <code>response</code>. But I should be receiving all other responses even if it is <code>waiting or not</code>.</p>

<p>My pseudocode:</p>

<p>Recv Section:</p>

<pre><code>struct timeval timeout = {
     .tv_sec = 5,
     .tv_usec = 0
};

while (1) {
   ret = select(maxfd + 1, &amp;rdfd, NULL, NULL, &amp;timeout);
   if (ret == 0) {
       fprintf(stderr, "No response for request\n");
       return;
   }

   ret = recvfrom(.buf..);

   struct standard_msg *std_msg = buf;

   switch(std_msg-&gt;type) {
        case TYPE_1: 
            if expecting response for TYPE_1
            read and retun from here;
            else loop until timeout
        case TYPE_2:
            if expecting response for TYPE_2
            read and return from here
            else loop until timeout
        default:
            print buf
   }
}
</code></pre>

<p>Send Section:</p>

<pre><code>while(1) {
   struct timeval timeout = {
      .tv_sec = 50,
      .tv_usec = 0
   }
   select(1, NULL, NULL, NULL, &amp;timeout);
   send_a_packet_wait_for_ITS_response(); //which will call the recv_secion
}
</code></pre>

<p>The <code>send</code> happens by reading the contents of file every <code>50</code> secs. Lets say if a packet is sent got reply right back, now I will be waiting for  <code>50</code> secs to send other packet. During this time I cannot <code>read</code> any packet that is received. And if the file reaches <code>EOF</code> there is nothing to send and now also I cannot <code>read</code> any packet.</p>

<p>So here I need to continuously listen to all packets and also listen for a particular packet. Is it possible to run a <code>listen</code> code for all other packets and <code>listen</code> code for particular packets? How do I solve this problem?</p>

<p>My intention is to get reply for a particular request within a configured timeout, without losing other packets.</p>

<p>Thanks in advance </p>