title: Select () function is getting timed out even after the socket has data to read in linux C
body: <p>I am facing a problem with select.  </p>
  
  <p>Below is my code snippet for receiving the data (server side):</p>
  
  <pre><code>int tcpip_receive_data (int sockfd, char *recv_buffer, int buffer_len, int flags, long sec_timeout)
  {
   int ret = 0 ;
   fd_set fdsock ;
   struct timeval timeout = {0x00,} ;
  
   if ((sockfd &lt; 0) || !(recv_buffer) || !(buffer_len))
   {
    return FAIL ;
   }
  
   if (sec_timeout &gt; 0)
   {
    /* the socket to monitor */
    FD_ZERO (&amp;fdsock) ;
    FD_SET ((unsigned int) sockfd, &amp;fdsock) ;
  
    /* set a timeout */
    timeout.tv_sec = sec_timeout ;
    timeout.tv_usec = 0 ;
  
    /* now check whether some data is there or not */
    if ((ret = select (sockfd + 1, &amp;fdsock, NULL, NULL, &amp;timeout)) &lt;= 0)
    {
  
     printf ("ERROR: Select() retValue[%d] for SockID[%d]%s:%d", ret, sockfd, __FILE__, __LINE__) ;
     return FAIL ; 
    }
    else if (ret == 0) 
    {
     printf ("ERROR: Select timed out for SockID[%d]- %s:%d", sockfd, __FILE__, __LINE__) ;
     return FAIL ;
    }
  
   }
   return (recv (sockfd, recv_buffer, buffer_len, flags)) ;
  }
  </code></pre>
  
  <p>Below is my code snippet for sending the data ( client side):</p>
  
  <pre><code>int main ( int argc, char **argv )
  {
                  struct sockaddr_in sock_client;
                  char ip_address [IP_ADDR_LEN] = {0,};
                  char filename [MAXSTR] = {0,};
                  int port = 0;
                  int sock_fd = 0;
                  FILE *input_fp = NULL;
                  FILE *output_fp = NULL;
                  int len = 0, ret = 0;
                  char data [ BUF_SIZE ] = {0,};
                  char recv_buffer [ BUF_SIZE ] = {0,};
                  struct hostent *host;
  
                  printf ("%d\n", argc);
                  if ( argc != 4 ) 
                  {   
                          fprintf ( stdout, "&lt;Usage&gt; ./wsp_client &lt;IPAddress&gt; &lt;Port&gt; &lt;DataFilename&gt;\n" );
                          return 0;
                  }   
  
                  strcpy ( ip_address ,argv[1] );
                  port = atoi ( argv[2] );
                  strcpy ( filename, argv[3] );
  
                  printf ("%s:%d:%s\n", ip_address, port, filename );
  
                  if ( ( input_fp = fopen ( filename, "rb" ) ) == NULL )
                  {   
                          perror ("Input file fopen:");
                          return 0;
                  }   
  
                  if ( ( output_fp = fopen ( "./read_data", "wb" ) ) == NULL )
                  {   
                          perror ("Outuput fopen:");
                          return 0;
                  }   
  
                  while ( !feof(input_fp) )
                  {   
                                  if ( ( ret = fread ( data, 1, BUF_SIZE, input_fp ) ) &lt; 0 ) 
                                  {   
                                          printf ("fread failed, ferror=[%d] feof=[%d]", ferror (stdin), feof(input_fp));
  
                                          break ;
                                  }
                                  len += ret;
                  }
  
                  fwrite ( data, sizeof (char), len, output_fp );
                  fclose ( input_fp );
                  fclose ( output_fp );
  
                  //create a scoket.
                  if ( ( sock_fd = socket ( AF_INET, SOCK_STREAM, 0 ) ) &lt; 0 )
                  {
                          perror ( "Socket Error:" );
                          return 0;
                  }
  
                  //form the structure.
                  host = gethostbyname(ip_address);
                  sock_client.sin_family = AF_INET;
                  sock_client.sin_port = htons (port);
                  sock_client.sin_addr = *((struct in_addr *)host-&gt;h_addr);
  
                  //connect
                  printf ( "Connecting to port %d\n", port );
                  if ( connect ( sock_fd, (struct sockaddr *) &amp;sock_client, sizeof ( sock_client ) ) &lt; 0 )
                  {
                          perror ( "connect:" );
                          return 0;
                  }
  
                  printf ("Connection Established\n");
  
                  printf ("Sending data of lenght:%d\n", len);
                  if ( ( ret = send ( sock_fd, data, len, 0 ) ) &lt; 0 )
                  {
                          perror ("send:");
                          return 0;
                  }
                  printf ("Sent data of lenght:%d\n", len);
  
                  recv (sock_fd, recv_buffer, BUF_SIZE, 0);
  
                  close ( sock_fd );
  }
  </code></pre>
  
  <p>The problem is the client program which i have written is able to send the data but the server is not able to receive the data. But the with other client program(actually a tool) I am able to send data and the server is able to receive the data and process the data received.</p>
  
  <p>And from the tcpdump, there has been a connection established, and the client have sent data, and the server has acknowledged it( Please check the Wrieshark pasted below ). 
  But the server is giving me the logs saying that timeout occurred.</p>
  
  <p><img src="https://i.stack.imgur.com/kTbee.png" alt="enter image description here"></p>
  
  <p>Why is select is not waking up when the data present in the socket. Why it is getting timedout. </p>
  
  <p>Please help me to sort the problem.</p>