title: How to flush Input Buffer of an UDP Socket in C?
body: <p>How to flush Input Buffer (if such thing exists at all) of an UDP Socket in C ?</p>
  
  <p>I'm working on an embedded Linux environment and using C to create some native application. There are several of these embedded machines on the same network, and when an event occurs on one of them (lets call it the WHISTLE-BLOWER), WHISTLE-BLOWER should send a network message to the network broadcast address, so that all machines on the network (including the WHISTLE-BLOWER) knows about the event and executes some actions according to it. <strong>I'm using UDP socket by the way...</strong></p>
  
  <p>Here's the pseudo-code for it:</p>
  
  <pre><code>main
  {
      startNetworkListenerThread( networkListenerFunction );
  
      while( not received any SIGTERM or such )
      {
          localEventInfo = checkIfTheLocalEventOccured();
          broadcastOnNetwork( localEventInfo );
      }
  }
  
  networkListenerFunction
  {
      bindSocket;
  
      while( not SIGTERM )
      {
  // THIS IS WHERE I WANT TO FLUSH THE RECV BUFFER...
          recv_data = recvfrom( socket );
          if( validate recv data )
          {
              startExecuteLocalAction;
              sleep( 5 );
              stopExecuteLocalAction;
          }
      }
  }
  </code></pre>
  
  <p>The way I <strong>expect and want</strong> to work this code is:</p>
  
  <pre><code>1. LOCAL_EVENT occured
  2. Broadcasted LOCAL_EVENT_INFO on network
  3. All machines received EVENT_INFO, including the original broadcaster
  4. All machines started executing the local action, including the original broadcaster
  5. All machines' network listener(thread)s are sleeping
  6. Another LOCAL_EVENT2 occured
  7. Since all machines' listener are sleeping, LOCAL_EVENT2 is ignored
  8. All machines' network listener(thread)s are now active again
  9. GO BACK TO 1 / RESTART CYCLE
  RESULT = TOTAL 2 EVENTS, 1 IGNORED
  </code></pre>
  
  <p>The way it actually works is:</p>
  
  <pre><code>1. LOCAL_EVENT occured
  2. Broadcasted LOCAL_EVENT_INFO on network
  3. All machines received EVENT_INFO, including the original broadcaster
  4. All machines started executing the local action, including the original broadcaster
  5. All machines' network listener(thread)s are sleeping
  6. Another LOCAL_EVENT2 occured
  7. Eventhough all machines' listener are sleeping; LOCAL_EVENT2 is queued  SOMEHOW
  8. All machines' network listener(thread)s are now active again
  9. All machines received EVENT_INFO2 and executed local actions again, slept and reactivated
  10. GO BACK TO 1 / RESTART CYCLE
  RESULT = TOTAL 2 EVENTS, 0 IGNORED
  </code></pre>
  
  <p><strong>tl,dr: The packets/messages/UDP Broadcasts sent to an already binded socket, whoose parent thread is sleeping at the delivery-moment; are somehow queued/buffered and delivered at the next 'recvfrom' call on the said socket.</strong> </p>
  
  <p>I want those UDP broadcasts to be ignored so I was thinking of flushing the receive buffer (obviously not the one i'm giving as parameter to the recvfrom method) if it exists before calling recvfrom. How can I do that? or what path should I follow?</p>