title: Boost::Asio : Problem with async_read
body: <p>I'm writing a simple ProxyServer that analyzes packages and sends them to another server instance, e.g. something like this:</p>
  
  <p>client -> MyProxy -> SQLServer  -><br/>
  client &lt;- MyProxy &lt;- SQLServer  &lt;-</p>
  
  <p>It should run in an infinite loop.
  My problem now is that the proxy seems to loose packages, sometimes it even hangs.
  When I add a lot of debug information (which is written to the console), the ProxyServer is
  much more stable. It seems like the ProxyServer is too fast.. :-)</p>
  
  <p>I'm pretty sure I'm doing something wrong, here is code of my session class (the code is derived from the Boost::Asio examples).</p>
  
  <pre><code>#include "session.h"
  
  #include &lt;iostream&gt;
  using namespace std;
  
  session::session(boost::asio::io_service&amp; io_service)
  : socket_(io_service)
  , sqlsocket_(io_service)
  , io_service_(io_service)
  , resolver(io_service)
  {
      cout &lt;&lt; "session::session()" &lt;&lt; endl;
  }
  
  session::~session()
  {
      cout &lt;&lt; "session::~session()" &lt;&lt; endl;
      cout &lt;&lt; "closing session ..." &lt;&lt; endl;
  }
  
  tcp::socket&amp; session::socket()
  {
      return socket_;
  }
  
  void session::start()
  {
      cout &lt;&lt; "session::start()" &lt;&lt; endl;
      cout &lt;&lt; "starting session ..." &lt;&lt; endl;
  
      // connect to the sqlserver database
      tcp::resolver::query query("192.168.1.50", "1317");
      tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
      tcp::endpoint endpoint = *endpoint_iterator;
  
      sqlsocket_.async_connect(endpoint,
          boost::bind(&amp;session::handle_sqlserver_connect, this,
          boost::asio::placeholders::error, ++endpoint_iterator));
  
      // TODO: connect to the connector
  }
  
  void session::handle_read(const boost::system::error_code&amp; error,
                            size_t bytes_transferred)
  {
      cout &lt;&lt; "session::handle_read()" &lt;&lt; endl;
      if (!error)
      {
          cout &lt;&lt; "session::handle_read() (read: " 
               &lt;&lt; bytes_transferred &lt;&lt; ")"
               &lt;&lt; endl;
          boost::asio::async_write(sqlsocket_,
              boost::asio::buffer(data_, bytes_transferred),
              boost::bind(&amp;session::handle_sqlserver_write, this,
              boost::asio::placeholders::error, bytes_transferred));
      }
      else
      {
          delete this;
      }
  }
  
  void session::handle_sqlserver_read(const boost::system::error_code&amp; error,
                                      size_t bytes_transferred)
  {
      cout &lt;&lt; "session::handle_sqlserver_read()" &lt;&lt; endl;
      if (!error)
      {
          cout &lt;&lt; "session::handle_sqlserver_read() (read: " 
               &lt;&lt; bytes_transferred &lt;&lt; ")"
               &lt;&lt; endl;
          boost::asio::async_write(socket_,
              boost::asio::buffer(data_, bytes_transferred),
              boost::bind(&amp;session::handle_write, this,
              boost::asio::placeholders::error, bytes_transferred));
      }
      else
      {
          delete this;
      }
  }
  
  void session::handle_write(const boost::system::error_code&amp; error,
                             size_t bytes_transferred)
  {
      static int count = 0;
      cout &lt;&lt; ++count &lt;&lt; ". session::handle_write()" &lt;&lt; endl;
      if (!error)
      {
          cout &lt;&lt; "session::handle_write() (read: " 
               &lt;&lt; bytes_transferred &lt;&lt; ")"
               &lt;&lt; endl;
          socket_.async_read_some(boost::asio::buffer(data_, max_length),
              boost::bind(&amp;session::handle_read, this,
              boost::asio::placeholders::error,
              boost::asio::placeholders::bytes_transferred));
      }
      else
      {
          delete this;
      }
  }
  
  void session::handle_sqlserver_write(const boost::system::error_code&amp; error,
                                       size_t bytes_transferred)
  {
      cout &lt;&lt; "session::handle_sqlserver_write()" &lt;&lt; endl;
      if (!error)
      {
          cout &lt;&lt; "session::handle_sqlserver_write() (read: " 
               &lt;&lt; bytes_transferred &lt;&lt; ")"
               &lt;&lt; endl;
          sqlsocket_.async_read_some(boost::asio::buffer(data_, max_length),
              boost::bind(&amp;session::handle_sqlserver_read, this,
              boost::asio::placeholders::error,
              boost::asio::placeholders::bytes_transferred));
      }
      else
      {
          delete this;
      }
  }
  
  void session::handle_sqlserver_connect(const boost::system::error_code&amp; error,
                                         tcp::resolver::iterator endpoint_iterator)
  {
      cout &lt;&lt; "session::handle_sqlserver_connect()" &lt;&lt; endl;
      if (!error)
      {
          socket_.async_read_some(boost::asio::buffer(data_, max_length),
              boost::bind(&amp;session::handle_read, this,
              boost::asio::placeholders::error,
              boost::asio::placeholders::bytes_transferred));
      }
      else if (endpoint_iterator != tcp::resolver::iterator())
      {
          sqlsocket_.close();
          tcp::endpoint endpoint = *endpoint_iterator;
          sqlsocket_.async_connect(endpoint,
              boost::bind(&amp;session::handle_sqlserver_connect, this,
              boost::asio::placeholders::error, ++endpoint_iterator));
      }
  }
  </code></pre>
  
  <p>Do I need to use other methods instead of async_* for my type of proxy?
  I'm porting the code from some old project that my company wants to restart again, but with boost instead of the Winsock stuff that was used before.</p>
  
  <p>Any idea what could be the problem?</p>
  
  <p>The old code did something like this:
  The main method with the accept method call created two threads</p>
  
  <pre><code>CreateThread(0, 0, (LPTHREAD_START_ROUTINE)listenatclient, (LPVOID)cs, 0, 0);
  CreateThread(0, 0, (LPTHREAD_START_ROUTINE)listenatserver, (LPVOID)cs, 0, 0);
  </code></pre>
  
  <p>and the threads called the following functions:</p>
  
  <pre><code>void listenatclient(LPVOID connection)
  {
      connection_s* cs = (connection_s*)connection;
      char inMessagecli[MSG_SIZE];
      int rcount = 0;
  
      ...
  
      do
      {
          memset(inMessagecli, 0, MSG_SIZE);
          rcount = recv((SOCKET)cs-&gt;client, inMessagecli, MSG_SIZE, 0);
          if (rcount != SOCKET_ERROR)
          {
              // analyze package
              ...
  
              send((SOCKET)cs-&gt;server, inMessagecli, rcount, 0);
          }
      } while (rcount &gt; 0);
  }
  
  void listenatserver(LPVOID connection)
  {
      connection_s* cs = (connection_s*)connection;
      char inMessageserv[MSG_SIZE];
      int rcount = 0;
  
      do
      {
          memset(inMessageserv, 0, MSG_SIZE);
          rcount = recv((SOCKET)cs-&gt;server, inMessageserv, MSG_SIZE, 0);
          if (rcount != SOCKET_ERROR)
          {
              send((SOCKET)cs-&gt;client, inMessageserv, rcount, 0);         
          }
      } while (rcount &gt; 0);
  }
  </code></pre>
  
  <p>[EDIT]:
  I tried to run the async_read commands for the client and the sqlserver simultaneously, but now I get crashes all the time, sometimes in boost::bind, sometimes in other parts of the boost library. </p>
  
  <p>What seems to happen is that 2 or three conections are created ( 3 sessions). While closing the first session, the crash seems to happen in the second session.</p>
  
  <p>Is boost asio not treadsafe or am I doing something terribly wrong here :-) ?</p>
  
  <p>I posted the code for the little ProxyServer here:</p>
  
  <p>session.h : <a href="http://pastebin.com/eC5fn7U8" rel="nofollow">link</a></p>
  
  <p>session.cpp : <a href="http://pastebin.com/LDnQqCNr" rel="nofollow">link</a></p>
  
  <p>server.h: <a href="http://pastebin.com/1fDJU6sx" rel="nofollow">link</a></p>
  
  <p>server.cpp: <a href="http://pastebin.com/seMAVx4D" rel="nofollow">link</a></p>
  
  <p>ProxyServer.cpp: <a href="http://pastebin.com/8S7YfE8L" rel="nofollow">link</a></p>