title: socket programming-recv() function
body: <p>i have a problem with recv() function in non-blocking socket (c language under unix)
  i have set socket to be non-blocking with below code(server program):</p>
  
  <pre><code>int x;
  x=fcntl(listen_sd,F_GETFL,0);
  fcntl(listen_sd,F_SETFL,x | O_NONBLOCK);
  </code></pre>
  
  <p>when i call <code>recv()</code>, if message are available it returns the length of message,if not it blocks!!</p>
  
  <p>i sued select function in my code too.</p>
  
  <pre><code>while(1)
         {
            /**********************************************/
            /* Receive data on this connection until the  */
            /* recv fails with EWOULDBLOCK.  If any other */
            /* failure occurs, we will close the          */
            /* connection.                                */
            /**********************************************/
            rc = recv(i, buffer, sizeof(buffer), 0);
            if (rc &lt; 0)
            {
           if(errno == EAGAIN||errno == EWOULDBLOCK)
           {
          printf("no message\n");
          break;
           }
  
                  perror(" recv() failed");
                  close_conn = TRUE;
  
            }
  
            /**********************************************/
            /* Check to see if the connection has been    */
            /* closed by the client                       */
            /**********************************************/
            if (rc == 0)
            {
               printf("connection closed\n");
               close_conn = TRUE;
               break;
  
  
            }
  
            /**********************************************/
            /* Data was recevied                          */
            /**********************************************/
            len = rc;
            printf("  %d bytes received\n", len);
          }
  </code></pre>
  
  <p>if client send a message and doesn't close connection then server in first call of recv() gets message and in second call is blocked(in other words recv() doesn't return EWOULDBLOCK error at all!!)
  why? </p>