How to clear input buffer (if such a thing exists at all) UDP Socket in C?
I am working on an embedded Linux environment and using C I am creating my own application. There are several such embedded machines in one network, and when an event occurs on one of them (let's call it WHISTLE-BLOWER), WHISTLE-BLOWER must send a network message to the broadcast address of the network so that all machines on the network (including WHISTLE-BLOWER) know about event and performs some actions in accordance with it. I am using a UDP socket, by the way ...
Here's the pseudo code for it:
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; } } }
The way I expect and want to work with this 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
Principle of operation:
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
tl, dr: packets / messages / UDP broadcasts sent to an already bound socket, whoose parent thread - delivery sleep time; somehow queued / buffered and delivered the next time “recvfrom” is called on the specified socket.
I want these UDP transmissions to be ignored, so I thought about flushing the receive buffer (obviously not the one I provide as a parameter to the recvfrom method) if it exists before recvfrom is called. How can i do this? or which way should i stick to?