title: Making an asynchronous Client with boost::asio
body: <p>i'm trying to make an asynchronous Client with boost::asio, 
  i use the daytime asynchronous Server(in the tutorial).</p>
  
  <p>However sometimes the Client don't receive the Message, sometimes it do :O</p>
  
  <p>I'm sorry if this is too much code, but i don't know what's wrong :/</p>
  
  <p>Client:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  #include &lt;ostream&gt;
  
  #include &lt;boost/thread.hpp&gt;
  #include &lt;boost/bind.hpp&gt;
  #include &lt;boost/array.hpp&gt;
  #include &lt;boost/asio.hpp&gt;
  
  using namespace std;
  
  using boost::asio::ip::tcp;
  
  class TCPClient
  {
  public:
      TCPClient(boost::asio::io_service&amp; IO_Service, tcp::resolver::iterator EndPointIter);
  
      void Write();
      void Close();
  private:
      boost::asio::io_service&amp; m_IOService;
      tcp::socket m_Socket;
  
      boost::array&lt;char, 128&gt; m_Buffer;
      size_t m_BufLen;
  
  private:
      void OnConnect(const boost::system::error_code&amp; ErrorCode,
              tcp::resolver::iterator EndPointIter);
  
      void OnReceive(const boost::system::error_code&amp; ErrorCode);
  
      void DoClose();
  };
  
  TCPClient::TCPClient(boost::asio::io_service&amp; IO_Service, tcp::resolver::iterator EndPointIter)
  : m_IOService(IO_Service), m_Socket(IO_Service)
  {
      tcp::endpoint EndPoint = *EndPointIter;
  
      m_Socket.async_connect(EndPoint,
              boost::bind(&amp;TCPClient::OnConnect, this, boost::asio::placeholders::error, ++EndPointIter));
  }
  
  void TCPClient::Close()
  {
      m_IOService.post(
          boost::bind(&amp;TCPClient::DoClose, this));
  }
  void TCPClient::OnConnect(const boost::system::error_code&amp; ErrorCode,
      tcp::resolver::iterator EndPointIter)
  {
      if (ErrorCode == 0)
      // Successful connected
      {
          m_Socket.async_receive(boost::asio::buffer(m_Buffer.data(), m_BufLen),
                  boost::bind(&amp;TCPClient::OnReceive, this, boost::asio::placeholders::error));
  
  
      } else if (EndPointIter != tcp::resolver::iterator())
      {
          m_Socket.close();
          tcp::endpoint EndPoint = *EndPointIter;
  
          m_Socket.async_connect(EndPoint,
                  boost::bind(&amp;TCPClient::OnConnect, this, boost::asio::placeholders::error, ++EndPointIter));
      }
  }
  
  void TCPClient::OnReceive(const boost::system::error_code&amp; ErrorCode)
  {
      if (ErrorCode == 0)
      {
          std::cout &lt;&lt; m_Buffer.data() &lt;&lt; std::endl;
  
          m_Socket.async_receive(boost::asio::buffer(m_Buffer.data(), m_BufLen),
                  boost::bind(&amp;TCPClient::OnReceive, this, boost::asio::placeholders::error));
      } else {
          DoClose();
      }
  }
  
  void TCPClient::DoClose()
  {
      m_Socket.close();
  }
  
  int main()
  {
      try {
          boost::asio::io_service IO_Service;
  
          tcp::resolver Resolver(IO_Service);
          tcp::resolver::query Query("127.0.0.1", "daytime");
  
          tcp::resolver::iterator EndPointIterator = Resolver.resolve(Query);
  
          TCPClient Client(IO_Service, EndPointIterator);
  
          boost::thread ClientThread(
                  boost::bind(&amp;boost::asio::io_service::run, &amp;IO_Service));
  
          std::cout &lt;&lt; "Client started." &lt;&lt; std::endl;
  
          std::string Input;
          while (Input != "exit")
          {
              std::cin &gt;&gt; Input;
          }
  
          Client.Close();
          ClientThread.join();
      } catch (std::exception&amp; e)
      {
          std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
      }
  
  }
  </code></pre>
  
  <p>Server:
  <a href="http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio/tutorial/tutdaytime3/src.html" rel="nofollow noreferrer">http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio/tutorial/tutdaytime3/src.html</a></p>