title: read: End of file in ASIO. Why is socket getting closed?
body: <p>I have the example(*) code like this:</p>
  
  <pre><code>std::string protocol = "http";
  std::string server = "www.example.com";
  std::string path = "/";
  
  asio::io_service service;
  
  asio::ip::tcp::resolver resolver(service);
  asio::ip::tcp::resolver::query query(server, protocol);
  
  asio::ip::tcp::socket socket(service);
  
  asio::ip::tcp::resolver::iterator end;
  auto it = resolver.resolve(query);
  asio::error_code error;
  socket.connect(*it, error);
  while(error &amp;&amp; ++it!=end)
  {
      socket.close();
      socket.connect(*it, error);
  }
  
  asio::streambuf request;
  std::ostream request_stream(&amp;request);
  
  request_stream &lt;&lt; "GET " &lt;&lt; path &lt;&lt; " HTTP/1.0\r\n";
  request_stream &lt;&lt; "Host: " &lt;&lt; server &lt;&lt; "\r\n";
  request_stream &lt;&lt; "Accept: */*\r\n";
  request_stream &lt;&lt; "Connection: close\r\n\r\n";
  
  asio::streambuf response;
  size_t s = asio::read(socket, response);
  </code></pre>
  
  <p>When <code>asio::read</code> is called i get:</p>
  
  <pre><code>terminate called after throwing an instance of 'boost::exception_detail::clone_impl&lt;boost::exception_detail::error_info_injector&lt;std::system_error&gt; &gt;'
    what():  read: End of file
  </code></pre>
  
  <p>As i understand it, this error happens because the socket is getting closed while trying to read. But i do not understand why is it getting closed.</p>
  
  <p>I have originally had <code>read_until</code> there, and it had the same error.</p>
  
  <p>Am i doing something wrong with initiation of the connection or is it getting terminated for some other reason?</p>
  
  <p>(*) Example means, that it is an example, and not a complete working program. If the example can be made shorter or better, please feel free to edit the question.</p>
  
  <hr>
  
  <p>Per sehe's answer i have attempted the following:</p>
  
  <pre><code>size_t s = asio::read(socket, response, error);
  
  if(!error || error==asio::error::eof)
  {
      if(s==0)
      {
          std::cerr &lt;&lt; "Same problem" &lt;&lt; std::endl;
      }
  }
  </code></pre>
  
  <p>The result is that "Same problem" is displayed when running the program, which means that it gets EOF before reading any data.</p>
  
  <hr>
  
  <p>Per YSC's answer i have tried the following:</p>
  
  <pre><code>for (;;)
  {
      char buf[255];
  
      size_t len = socket.read_some(asio::buffer(buf, 255), error);
  
      if (error == asio::error::eof)
      {
          std::cout &lt;&lt; std::endl &lt;&lt; "Connection closed" &lt;&lt; std::endl;
          break; // Connection closed cleanly by peer.
      }
      else if (error)
      {
          std::cerr &lt;&lt; "Error" &lt;&lt; std::endl;
          return 1;
      }
  
      std::cout.write(buf, len);
  }
  </code></pre>
  
  <p>No data appears, and "Connection closed" is shown after a slight delay. Which means that i get EOF without any data with that approach as well.</p>