title: Client cannot receive data - Exception with socket.async_receive in client
body: <p>I have a simple version of a client using boost asio. The client is suppose to receive a response from the server once it sends data. Here is the code of the client</p>
  
  <pre><code>void RunClient()
      {
          try
          {
              boost::asio::io_service io_service;
              boost::asio::ip::tcp::resolver resolver(io_service);
              boost::asio::ip::tcp::resolver::query query( "127.0.0.1", boost::lexical_cast&lt; std::string &gt;( 7777 )); //9100
              boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
              boost::asio::ip::tcp::socket socket(io_service);
              socket.async_receive(boost::asio::buffer(buf_client, 3000), 0, ClientReceiveEvent);
  
              boost::asio::connect(socket, endpoint_iterator);
  
              boost::system::error_code ignored_error;
              std::cout &lt;&lt; "Sending message \n";
              boost::asio::write(socket, boost::asio::buffer("Data to send"), ignored_error);
              io_service.run();
          }
          catch (std::exception &amp; ex)
          {
              std::cout &lt;&lt; "[" &lt;&lt; boost::this_thread::get_id()&lt;&lt; "] Exception: " &lt;&lt; ex.what() &lt;&lt; std::endl;   
          }
      }
  </code></pre>
  
  <p>Here is my ClientReceiveEvent</p>
  
  <pre><code>void ClientReceiveEvent(const boost::system::error_code&amp; error, std::size_t bytes_transferred)
      {
          if(!error)
          {
              std::cout &lt;&lt; "Message: " &lt;&lt; buf_client.data() &lt;&lt; std::endl;
  
          }
          else
          {
              std::cout &lt;&lt; "Error occurred." &lt;&lt; error.message() &lt;&lt; std::endl;
          }
      }
  </code></pre>
  
  <p>I am getting an error from the above method for incoming data</p>
  
  <pre><code>Error occurred.The file handle supplied is not valid
  </code></pre>
  
  <p>Any suggestion what i am doing wrong in the client ?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I got the code working however I am confused as to why the statement</p>
  
  <pre><code>socket-&gt;async_receive(boost::asio::buffer(buf_client, 3000), 0, ClientReceiveEvent);
  </code></pre>
  
  <p>needs to be placed after connect. and why the statement</p>
  
  <pre><code>io_service-&gt;run();
  </code></pre>
  
  <p>needs to be placed at the end. I thought this starts the asynch process.</p>
  
  <p>I also wanted to know how would i resend data to the server. I could send it once successfully. How do i resend the command again?</p>
  
  <p>The working code is:</p>
  
  <pre><code>boost::shared_ptr&lt; boost::asio::io_service &gt; io_service(new boost::asio::io_service);
              boost::shared_ptr&lt; boost::asio::ip::tcp::socket &gt; socket(   new boost::asio::ip::tcp::socket( *io_service ) );
              boost::asio::ip::tcp::resolver resolver(*io_service);
              boost::asio::ip::tcp::resolver::query query( "127.0.0.1", boost::lexical_cast&lt; std::string &gt;( 7777 )); //9100
              boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
              socket-&gt;connect(endpoint_iterator-&gt;endpoint());
              socket-&gt;async_receive(boost::asio::buffer(buf_client, 3000), 0, ClientReceiveEvent);
              boost::system::error_code ignored_error;
              std::cout &lt;&lt; "Sending message \n";
              boost::asio::write(*socket, boost::asio::buffer("some data"), ignored_error);           
              io_service-&gt;run();
  </code></pre>