title: Boost/Asio Server gets invalid argument C++
body: <p>I tried <a href="http://www.boost.org/doc/libs/1_48_0/doc/html/boost_asio/example/serialization/" rel="nofollow">this</a> example, works correctly. But i want server = receiver and client = sender, So i stared to rewrite this example as dynamic library.</p>
  
  <p>Server:</p>
  
  <pre><code>Server::Server(protocol::Service&amp; io_service, unsigned short port) : acceptor_(io_service, protocol::Endpoint(boost::asio::ip::tcp::v4(), port)) 
  {
      connectionPtr newConn(new Connection(acceptor_.io_service()));
      acceptor_.async_accept(newConn-&gt;socket(), boost::bind(&amp;Server::HandleAccept, this, boost::asio::placeholders::error, newConn));
  }
  
  
  void Server::HandleAccept(const boost::system::error_code &amp;e, connectionPtr conn)
  {
      if(!e) {
          conn-&gt;AsyncRead(pkg, boost::bind(&amp;Server::HandleRead, this, boost::asio::placeholders::error));
          connectionPtr newConn(new Connection(acceptor_.io_service()));
          acceptor_.async_accept(newConn-&gt;socket(), boost::bind(&amp;Server::HandleAccept, this, boost::asio::placeholders::error, newConn));
      }
  
      else {
          std::cerr &lt;&lt; e.message() &lt;&lt; std::endl;
      }
  }
  
  void Server::HandleRead(const boost::system::error_code &amp;e)
  {
      if(!e) {
          for(std::size_t i = 0; i &lt; pkg.size(); i++) {
              std::cout &lt;&lt; "Package Number " &lt;&lt; i &lt;&lt; std::endl;
              std::cout &lt;&lt; "ID: " &lt;&lt; pkg[i].uuId &lt;&lt; std::endl;
              std::cout &lt;&lt; "GENDER: " &lt;&lt; pkg[i].gender &lt;&lt; std::endl;
          }
      }
      else {
          std::cerr &lt;&lt; e.message() &lt;&lt; std::endl;
      }
  }
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>Client::Client(protocol::Service &amp;io_service, const std::string &amp;host, const std::string &amp;service, Package stock) : connection_(io_service)
  {
      // Resolve the host name into an IP address.
      protocol::Resolver resolver(io_service);
      protocol::ResolverQuery query(host, service);
      protocol::ResolverIter endpointIter = resolver.resolve(query);
      protocol::Endpoint endpoint = *endpointIter;
  
      pkg.push_back(stock);
  
      // Start an asynchronous connect operation.
      connection_.socket().async_connect(endpoint, boost::bind(&amp;Client::HandleConnect, this, boost::asio::placeholders::error, ++endpointIter));
  }
  
  void Client::HandleConnect(const boost::system::error_code &amp;e, protocol::ResolverIter endpointIter)
  {
      if(!e) {
          connection_.AsyncWrite(pkg, boost::bind(&amp;Client::HandleWrite, this, boost::asio::placeholders::error(), conn));
      }
      else if(endpointIter != protocol::ResolverIter()) {
          connection_.socket().close();
          protocol::Endpoint endpoint = *endpointIter;
  
          connection_.socket().async_connect(endpoint, boost::bind(&amp;Client::HandleConnect, this, boost::asio::placeholders::error(), ++endpointIter));
      }
      else{
          std::cerr &lt;&lt; e.message() &lt;&lt; std::endl;
      }
  }
  
  void Client::HandleWrite(const boost::system::error_code &amp;e, connectionPtr conn)
  {
      std::cout &lt;&lt; "Sent Files: " &lt;&lt; std::endl;
      std::cout &lt;&lt; "ID: " &lt;&lt;  pkg.front().uuId &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>Connection.h:</p>
  
  <pre><code>class Connection : public boost::enable_shared_from_this&lt;Connection&gt;{
  public:
      Connection(protocol::Service&amp; io_service) : socket_(io_service) {}
      protocol::Socket&amp; socket(){
          return socket_;
      }
  
      template &lt;typename T, typename Handler&gt;
      void AsyncWrite(const T&amp; t, Handler handler)
      {
            std::ostringstream archiveStream;
            boost::archive::text_oarchive archive(archiveStream);
            archive &lt;&lt; t;
            outboundData = archiveStream.str();
  
            // Format the header.
            std::ostringstream headerStream;
            headerStream &lt;&lt; std::setw(headerLength) &lt;&lt; std::hex &lt;&lt; outboundData.size();
  
            if (!headerStream || headerStream.str().size() != headerLength)
            {
                  // Something went wrong, inform the caller.
                  boost::system::error_code error(boost::asio::error::invalid_argument);
                  socket_.io_service().post(boost::bind(handler, error));
                  return;
            }
  
            outboundHeader = headerStream.str();
  
            // Write the serialized data to the socket. We use "gather-write" to send
            // both the header and the data in a single write operation.
  
            std::vector&lt;boost::asio::const_buffer&gt; buffers;
            buffers.push_back(boost::asio::buffer(outboundHeader));
            buffers.push_back(boost::asio::buffer(outboundData));
            boost::asio::async_write(socket_, buffers, handler);
      }
  
  
      template &lt;typename T, typename Handler&gt;
      void AsyncRead(T&amp; t, Handler handler)
      {
          // Issue a read operation to read exactly the number of bytes in a header.
          void (Connection::*f)(const boost::system::error_code&amp;, T&amp;, boost::tuple&lt;Handler&gt;) = &amp;Connection::HandleReadHeader&lt;T, Handler&gt;;
          boost::asio::async_read(socket_, boost::asio::buffer(inboundHeader),
              boost::bind(f, shared_from_this(), boost::asio::placeholders::error, boost::ref(t), boost::make_tuple(handler)));
      }
  
  
      template &lt;typename T, typename Handler&gt;
      void HandleReadHeader(const boost::system::error_code&amp; e, T&amp; t, boost::tuple&lt;Handler&gt; handler)
      {
            if (e)
                boost::get&lt;0&gt;(handler)(e);
            else {
                // Determine the length of the serialized data.
                std::istringstream is(std::string(inboundHeader, headerLength));
                std::size_t inboundDataSize = 0;
  
                if (!(is &gt;&gt; std::hex &gt;&gt; inboundDataSize)) {
                    // Header doesn't seem to be valid. Inform the caller.
                    boost::system::error_code error(boost::asio::error::invalid_argument);
                    boost::get&lt;0&gt;(handler)(error);
                    return;
                }
  
                // Start an asynchronous call to receive the data.
                inboundData.resize(inboundDataSize);
                void (Connection::*f)(const boost::system::error_code&amp;,T&amp;, boost::tuple&lt;Handler&gt;) = &amp;Connection::HandleReadData&lt;T, Handler&gt;;
                boost::asio::async_read(socket_, boost::asio::buffer(inboundData), boost::bind(f, this, boost::asio::placeholders::error, boost::ref(t), handler));
            }
      }
  
  
      template &lt;typename T, typename Handler&gt;
      void HandleReadData(const boost::system::error_code&amp; e, T&amp; t, boost::tuple&lt;Handler&gt; handler)
      {
          if (e)
              boost::get&lt;0&gt;(handler)(e);
          else {
          // Extract the data structure from the data just received.
               try {
                  std::string archiveData(&amp;inboundData[0], inboundData.size());
                  std::istringstream archiveStream(archiveData);
                  boost::archive::text_iarchive archive(archiveStream);
                  archive &gt;&gt; t;
               }
               catch (std::exception&amp; e)
               {
                  // Unable to decode data.
                  boost::system::error_code error(boost::asio::error::invalid_argument);
                  boost::get&lt;0&gt;(handler)(error);
                  return;
               }
  
              // Inform caller that data has been received ok.
              boost::get&lt;0&gt;(handler)(e);
          }
      }
  
  
  private:
      protocol::Socket socket_;
      enum { headerLength = 8 };
      std::string outboundHeader;
      std::string outboundData;
      char inboundHeader[headerLength];
      std::vector&lt;char&gt; inboundData;
  };
  
  typedef boost::shared_ptr&lt;Connection&gt; connectionPtr;
  </code></pre>
  
  <p>I have created 2 projects (Server &amp; Client) and included this dynamic lib. When client sends Package struct, Server writes exception: Invalid Argument</p>
  
  <p>Where did i make mistake?</p>