title: c udp non-blocking socket with recvfrom and select
body: <p>I want to implement at the client side non-blocking socket with select function. But it doesn't work as expected. In the code below it never runs into else , rv is always 1 and when nothing is on the socket application stops for a while and continue when another messages is on the socket. I don't want that behavior , I want that client sends back message to the server when there is nothing on the socket to recvfrom.</p>
  
  <pre><code>fd_set readfds; 
  
  fcntl(sd, F_SETFL, O_NONBLOCK); 
  
  
  while (1) {
  
          FD_ZERO(&amp;readfds);
          FD_SET(sd, &amp;readfds);
  
          rv = select(sd + 1, &amp;readfds, NULL, NULL, NULL); 
  
          if(rv == 1){ 
  
              nbytes = recvfrom(sd, buf, RW_SIZE, 0, (struct sockaddr *) &amp;srv_addr, &amp;addrlen); 
  
  
          } else {
  
              printf("I'm never here so I can't send message back to the server!\n");
  
  
          }
  
  }
  </code></pre>
  
  <p>with struct timeval:</p>
  
  <pre><code>fd_set readfds; 
  fcntl(sd, F_SETFL, O_NONBLOCK); 
  struct timeval tv;
  
  
  while (1) {
  
          FD_ZERO(&amp;readfds);
          FD_SET(sd, &amp;readfds);
  
          tv.tv_sec = 0;
          tv.tv_usec = 0;
  
          rv = select(sd + 1, &amp;readfds, NULL, NULL, &amp;tv); 
  
          if(rv == 1){ 
  
              nbytes = recvfrom(sd, buf, RW_SIZE, 0, (struct sockaddr *) &amp;srv_addr, &amp;addrlen); 
  
  
          } else {
  
              printf("I'm always here like now ! \n");
  
  
          }
  
  }
  </code></pre>