title: Recvfrom re-reading old data
body: <p>I am programming a real-time networking system in C/C++ with Berkeley C sockets, which works on a tick-based interval. Every tick, the client program will read data from the socket with recvfrom().</p>
  
  <p>The problem is that if the server did not send any data, the client will read the last data that was sent on the socket.</p>
  
  <p>For example the client (pseudocode)</p>
  
  <pre><code>while (true)
  {
     if (time_to_update())
     {
        int n = recvfrom(data,...);
        printf("%s",data);
     }
  }
  </code></pre>
  
  <p>And server:</p>
  
  <pre><code>int main()
  {
     int m = recvfrom(data,...);
     int n = sendto(...,"hello");
  }
  </code></pre>
  
  <p>Then the client would keep printing "hello", whereas I only need it to print it once.</p>
  
  <p>This is qutie similar to this question, <a href="https://stackoverflow.com/questions/22487120/recvfrom-re-reading-data-on-socket">recvfrom re-reading data on socket</a>
  ,but I do not understand the answer very well and it does seem to solve the problem more generally.</p>
  
  <p>I'm guessing the data is stored in a buffer on the netword card, in which case is there a away to clear the buffer after 1 read?
  Another method I have in mind is to number the packets and only read new ones, but all types are finite so I'm hoping there might be a cleaner way.</p>
  
  <p>How would I make it so the client only reads the received data once?</p>