Boost logo

Boost Users :

Subject: [Boost-users] serialization using archive
From: Long P (longcae_at_[hidden])
Date: 2010-07-15 21:33:55



I have come across the example for serialization in


In the following method async_write, the data typename T is put into an archive before being sent out. I would need to do the same thing here but the archive should be binary stream because I need to send this data to a third party application which expects a binary stream. Does anyone know how to do it?


Thanks & Regards





  /// Asynchronously write a data structure to the socket.
  template <typename T, typename Handler>
  void async_write(const T& t, Handler handler)
    // Serialize the data first so we know how large it is.
    std::ostringstream archive_stream;
    boost::archive::text_oarchive archive(archive_stream);
    archive << t;
    outbound_data_ = archive_stream.str();

    // Format the header.
    std::ostringstream header_stream;
    header_stream << std::setw(header_length)
      << std::hex << outbound_data_.size();
    if (!header_stream || header_stream.str().size() != header_length)
      // Something went wrong, inform the caller.
      boost::system::error_code error(boost::asio::error::invalid_argument);
      socket_.io_service().post(boost::bind(handler, error));
    outbound_header_ = header_stream.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<boost::asio::const_buffer> buffers;
    boost::asio::async_write(socket_, buffers, handler);


Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at