title: Setting time out for connect() function tcp socket programming in C breaks recv()
body: <p>In my program If the server is not reachable the connect function take too much time. So i try to give time out to connect using select(). Now the problem is that when i try to receive data from server using recvfrom() i got error "EAGAIN". here is code used to connect and receive data from server.</p>
  
  <pre><code>int sock;
  struct sockaddr_in addr;
  int connectWithServer
  {
  
      int status;
  
      struct timeval  timeout;
      timeout.tv_sec = 10;
      timeout.tv_usec = 0;
  
      addr.sin_port = htons(port);
      sock = socket (AF_INET,SOCK_STREAM,0);
      inet_pton(AF_INET,serverIP,&amp;addr.sin_addr);
  
      fd_set set;
      FD_ZERO(&amp;set);
      FD_SET(sock, &amp;set);
  
      fcntl(sock, F_SETFL, O_NONBLOCK);
  
      if ( (status = connect(sock, (struct sockaddr*)&amp;addr, sizeof(addr))) == -1)
      {
          if ( errno != EINPROGRESS )
              return status;
  
      }
      status = select(sock+1, NULL, &amp;set, NULL, &amp;timeout);
  
      return status;
  }
  
  
  long int receiveResponse (void *response , unsigned int length)
  {
      socklen_t sockLen = sizeof(struct sockaddr);
      long int received = recvfrom(sock, response, length, 0,(struct sockaddr *)&amp;addr,  &amp;sockLen);
      printf("Received %ld bytes...  err %d\n",received, errno);
  
      return received;
  }
  </code></pre>