title: http client blocks on recv()
body: <p>I need some help writing an http client.  The trouble comes when I try to receive data from a webserver.  The recv() call blocks the program.  Any better direction would be extremely helpful, I'll post my code below:</p>
  
  <pre><code>if ( argc != 2 )
  {
      cerr &lt;&lt; "Usage: " &lt;&lt; argv[0];
      cerr &lt;&lt; " &lt;URI&gt;" &lt;&lt; endl;
      return 1;
  }
  else
  {
      uri_string = argv[1];
  }
  
  // Create URI object and have it parse the uri_string
  URI *uri = URI::Parse(uri_string);
  
  if ( uri == NULL )
  {
      cerr &lt;&lt; "Error: Cannot parse URI." &lt;&lt; endl;
      return 2;
  }
  
  // Check the port number specified, if none use port 80
  unsigned port = 80;
  if ( uri-&gt;Is_port_defined() )
  {
      port = uri-&gt;Get_port();
  }
  
  // Create TCP socket and connect to server
  int tcp_sock = socket( AF_INET, SOCK_STREAM, 0 );
  if ( tcp_sock &lt; 0 )
  {
      cerr &lt;&lt; "Unable to create TCP socket." &lt;&lt; endl;
      return 3;
  }
  
  sockaddr_in server;
  socklen_t slen = sizeof(server);
  
  server.sin_family = AF_INET;
  server.sin_port = htons( port );
  hostent *hostp = gethostbyname( uri-&gt;Get_host().c_str() );
  memcpy( &amp;server.sin_addr, hostp-&gt;h_addr, hostp-&gt;h_length );
  
  if ( connect( tcp_sock, (sockaddr*)&amp;server, slen ) &lt; 0 )
  {
      cerr &lt;&lt; "Unable to connect to server via TCP." &lt;&lt; endl;
      close( tcp_sock );
      return 4;
  }
  
  // Build HTTP request to send to server
  HTTP_Request *request = HTTP_Request::Create_GET_request( uri-&gt;Get_path() );
  request-&gt;Set_host( uri-&gt;Get_host() );
  string request_string = "";
  request-&gt;Print( request_string );
  
  //cout &lt;&lt; request_string &lt;&lt; endl;
  
  // Send it to the server, wait for reply and use HTTP_Response to get reply
  send( tcp_sock, &amp;request_string, sizeof(request_string), 0 );
  
  char recv_buffer[1024];
  int bytes_recv = 0;
  while ( bytes_recv &lt; 1024 )
  {
      int recv_len = recv( tcp_sock, recv_buffer + bytes_recv,
          1024 - bytes_recv, 0 );
      if ( recv_len == -1 )
      {
          cerr &lt;&lt; "Error receiving response from server." &lt;&lt; endl;
          close( tcp_sock );
          return 5;
      }
      bytes_recv += recv_len;
  }
  
  
  HTTP_Response *response = HTTP_Response::Parse(recv_buffer, bytes_recv);
  string response_string = "";
  response-&gt;Print( response_string );
  cout &lt;&lt; response_string &lt;&lt; endl;
  
  return 0;
  </code></pre>
  
  <p>}</p>