1

I am writing a program in C, using socket UDP. My program receives an input message from a socket and then do some calculations before reading an other message from the socket. But, in the meantime, more messages arrive. If I'm right in my tests, they go in a sort of buffer (kind of FIFO, stored directly in the OS ? - more explanation would be appreciated), and everytime I read from the socket, I read the oldest one, right?

Is there a proper way to clear that buffer (FIFO) ? Or i need to make a specific function that reads everything until there nothing more to read so i can ignore all the "in-between" messages ? Or maybe "close" the socket and "re-open" after my prediction calculus ?

And of course, is my understanding of the FIFO right or i'm missing something ?

Context if you want to understand more : I have a real-time program that receives every seconds a monitoring message via UDP socket from an external equipment. I read the first one, make some predictions during multiples seconds and then i need to read each seconds the newest messages, and not having everything shifted by reading the older's one. So during this "prediction" i want to ignore those messages.

Thanks

HacHac
  • 147
  • 11
  • 1
    You need to read all the data. Duplicate. – user207421 Jul 09 '19 at 09:49
  • 1
    Read out all and use the last. Also, while it's busy with your calculation you could be receiving new messages, keeping always latest/last received. When calculation ends, next round will just use the last read. (Btw, not very clear what kind of sampling you doing, as you seems reading every second but then you take multiple seconds to do calculation during which you want to ignore data, then you resume reading again seems every second ... ) – v01d Jul 09 '19 at 09:58
  • Depending on your sockets and UDP/IP layer implementation (if your system supports it), you might want to also check setsockopt(), option SO_RCVBUF. If your incoming packets size is known and same you could may be set total buffer size be a multiple of the packet/message. – v01d Jul 09 '19 at 10:06

0 Answers0