title: How do I find out if there is data available to be read from a socket in boost::asio?
body: <p>I'm using the Boost ASIO library to write a TCP client program.</p>
  
  <p>The protocol starts with a banner line when you connect followed by '\r\n'
  I can send commands at any time much like smtp.</p>
  
  <p>However, the server can also send me data when I don't ask for it and it's terminated with a '\r\n'</p>
  
  <p>I have a line like to this to read and parse the initial connection banner
  Which means, stop reading when I get "\r\n".  </p>
  
  <pre><code>boost::asio::streambuf response_;
  ...
  boost::asio::async_read_until(socket_, response_, "\r\n",
              boost::bind(&amp;PumpServerClient::HandleReadBanner, this,
                  boost::asio::placeholders::error));
  </code></pre>
  
  <p>But at various times I want to be able to ask the question, is there data available? if there is, read it until "\r\n".  I feel like I need a peek function but I can't see where that is.</p>
  
  <p>Ok, perhaps I can call async_read_until again.  But consider this... it never ends.
  The keep reading debug appears once.  Obvious, it's waiting forever to complete but never does.</p>
  
  <pre><code>void client::HandleReadRequest(const boost::system::error_code&amp; err)
  {
      if (!err){
          // Got some data, print it out
          std::ostringstream ss;
          ss &lt;&lt; &amp;response_;
  
          std::cout &lt;&lt; ss.str() &lt;&lt; std::endl;
  
           // Keep reading...
          boost::asio::async_read_until(socket_, response_, "\r\n",
            boost::bind(&amp;client::HandleReadRequest, this, boost::asio::placeholders::error));
  
          std::cout &lt;&lt; "keep reading" &lt;&lt; std::endl;
      }
      else
      {
          std::cout &lt;&lt; "Error: " &lt;&lt; err.message() &lt;&lt; "\n";
      }
  }
  </code></pre>
  
  <p>Ok, I think I get it now.  I wasn't looking at a full enough example to base my code off.</p>
  
  <p>It looks like calling:</p>
  
  <blockquote>
    <p>boost::asio::async_read_until(socket_,
    response_, "\r\n",
                  boost::bind(&amp;client::HandleReadRequest,
    this,
    boost::asio::placeholders::error));
    means that when a \r\n appears in the response_ buffer, the function HandleReadRequest will be called.  I only need to call async_read_until once.  unless a write request is called then I need to do a read again... it appears... well I have it working now at least.</p>
  </blockquote>