title: Async Read server BoostAsio not calling handler
body: <p>I'm currently working with Boost::Asio to do a basic Read/Write server, I have a little problem when it comes to the usage of the async_read function of the library.</p>
  
  <p>Here's my code snippet :</p>
  
  <p><strong>main.cpp</strong> :</p>
  
  <pre><code>#include &lt;ctime&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;boost/asio.hpp&gt;
  #include "TCPServer.hpp"
  #include "TCPConnection.hpp"
  
  using boost::asio::ip::tcp;
  
  int main()
  {
    try
      {
        boost::asio::io_service io_service;
        TCPServer server(io_service);
        io_service.run();
      }
    catch (std::exception&amp; e)
      {
        std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
      }
  
    return 0;
  }
  </code></pre>
  
  <p><strong>TCPServer.hpp :</strong>                                                                                                                                                                           </p>
  
  <pre><code>#ifndef TCPSERVER_HPP_
  #define TCPSERVER_HPP_
  
  #include &lt;ctime&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;boost/bind.hpp&gt;
  #include &lt;boost/shared_ptr.hpp&gt;
  #include &lt;boost/enable_shared_from_this.hpp&gt;
  #include &lt;boost/asio.hpp&gt;
  #include "TCPConnection.hpp"
  
  using boost::asio::ip::tcp;
  
  class TCPServer
  {
  private:
    tcp::acceptor _acceptor;
  
  public:
    TCPServer(boost::asio::io_service&amp; ioService)
      : _acceptor(ioService, tcp::endpoint(tcp::v4(), 4040))
    {
      startAccept();
    }
  
  private:
    void startAccept()
    {
      TCPConnection::pointer newConnection =
        TCPConnection::create(_acceptor.get_io_service());
      _acceptor.async_accept(newConnection-&gt;getSocket(),
                             boost::bind(&amp;TCPServer::handleAccept, this, newConnection,
                                         boost::asio::placeholders::error));
    }
  
    void handleAccept(TCPConnection::pointer newConnection, const boost::system::error_code&amp; error)
    {
      if (!error)
        {
          newConnection-&gt;asyncWrite("JIOLE");
          startAccept();
        }
    }
  };
  
  #endif
  </code></pre>
  
  <p><strong>TCPConnection.hpp :</strong></p>
  
  <pre><code>#ifndef TCPCONNECTION_HPP_
  #define TCPCONNECTION_HPP_
  
  #include &lt;ctime&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;boost/bind.hpp&gt;
  #include &lt;boost/shared_ptr.hpp&gt;
  #include &lt;boost/enable_shared_from_this.hpp&gt;
  #include &lt;boost/asio.hpp&gt;
  
  class TCPConnection : public boost::enable_shared_from_this&lt;TCPConnection&gt;
  {
  private:
    boost::asio::ip::tcp::socket _socket;
    std::string _readMessage;
    boost::asio::streambuf _response;
  
  public:
    typedef boost::shared_ptr&lt;TCPConnection&gt; pointer;
  
    static pointer create(boost::asio::io_service&amp; ios)
    {
      return pointer(new TCPConnection(ios));
    }
  
    boost::asio::ip::tcp::socket&amp; getSocket()
    {
      return _socket;
    }
  
    void  asyncWrite(const std::string &amp;message)
    {
      boost::asio::async_write(_socket,
                               boost::asio::buffer(message),
                               boost::bind(&amp;TCPConnection::handleWrite, shared_from_this(),
  
  
      boost::asio::placeholders::error));
          std::cout &lt;&lt; "AsyncWrite" &lt;&lt; std::endl;
        }
  
    void asyncRead()
    {
      std::cout &lt;&lt; "1st \"asyncRead\"" &lt;&lt; std::endl;
      boost::asio::async_read(_socket,
                              _response,
                              boost::bind(&amp;TCPConnection::handleRead, shared_from_this(),
                                          boost::asio::placeholders::error));
      std::cout &lt;&lt; "2nd \"asyncRead\"" &lt;&lt; std::endl;
    }
  
    void close()
    {
      _socket.close();
    }
  
  private:
  
    TCPConnection(boost::asio::io_service &amp;ioS) : _socket(ioS) {}
  
    void  handleWrite(const boost::system::error_code&amp; error)
    {
      std::cout &lt;&lt; "Write Handler" &lt;&lt; std::endl;
      if (!error)
        {
          asyncRead();
        }
      //SEE WHAT TO DO IN CASE OF ERROR                                                                                                                                                                                
    }
  
    void handleRead(const boost::system::error_code&amp; error)
    {
      std::cout &lt;&lt; "Read Handler" &lt;&lt; std::endl;
      if (!error)
        {
          std::cout &lt;&lt; &amp;_response &lt;&lt; std::endl;
          asyncRead();
        }
      else
        {
          std::cout &lt;&lt; &amp;error &lt;&lt; std::endl;
          _socket.close();
        }
      //CREATE SENDER(RSEPONSE::ERROR)                                                                                                                                                                                 
    }
  
  };
  
  #endif
  </code></pre>
  
  <p>The problem is that my async_read doesn't call the handler. Here is the output : </p>
  
  <p><strong>Output :</strong></p>
  
  <pre><code>AsyncWrite
  Write Handler
  1st "asyncRead"
  2nd "asyncRead"
  </code></pre>
  
  <p>When I'm writing something on a NetCat Client nothing is being received.</p>
  
  <p>Here what happens when i'm pressing ctrl+C :</p>
  
  <pre><code>Read Handler
  0x7fff645f3be0
  </code></pre>
  
  <p>I don't understand why nothing is received.</p>