title: setting timeout for recv fcn of a UDP socket
body: <p>I send a UDP packet by <code>sendto</code>, then receive the answer by <code>recv</code>.if <code>recv</code> does not receive the reply, the program does not proceed. However, the udp packet might be lost, or for some reason, the packet may not be delivered, so that the program gets stuck on <code>recv</code> line. I wonder how is it possible to set a timeout for <code>recv</code> if nopacket arrives in ,e.g., a minute, then skip that line and proceed the code?</p>
  
  <p>I am not pasting the full code since it is a generic <code>udp</code> code and my question is related to the only <code>recv</code>. For the final note, the development environment is linux.</p>
  
  <pre><code>unsigned long  buf[maxlen];
  struct protoent *proto;     //
  struct sockaddr_in server_addr;
  int s;  // socket
  memset( &amp;server_addr, 0, sizeof( server_addr ));
  server_addr.sin_family=AF_INET;
  server_addr.sin_addr.s_addr = inet_addr(hostname);
  sendto(s,msg,sizeof(msg),0,(struct sockaddr *)&amp;server_addr,sizeof(server_addr));
  
  recv(s,buf,sizeof(buf),0);
  </code></pre>