title: C udp application simulate packet loss
body: <p>I have udp server-client application written in C. On the client side packet loss is detected using recvfrom function and sequence numbers of packets. How can I now simulate dropped packet's on the client side, for example if server is sending 1000 packet I want to drop 20% of them? I want to do this in the code, not for example using ip tables or something like that. And one more thing, I have few clients and I want that they can dropped different packets, not the same one.</p>
  
  <pre><code>while(1){
  
      nbytes = recvfrom(socket, buffer, MAX_SIZE, 0, (struct sockaddr *) &amp;srv_addr, &amp;addrlen);
  
          if (nbytes != -1) {
  
      // packet is received
  
          }else{
  
      //packet is not received
  
          }
  
  }
  </code></pre>