I would like to serialize instances of a class and send them over TCP connection using C++ / Boost libraries. There are too many working examples out there... Some using text streams for buffers and some using tcp::iostream
. I'm not sure which one is suitable for my needs.
Requirements:
- Portability over different architectures (endiannes and bitness should not be a problem)
- Data need to be in binary format. (There is no text)
Current code:
// Client side:
boost::asio::streambuf b;
std::ostream os(&b);
boost::archive::binary_oarchive oa(os);
message m; // The `message' class is serializable
// construct `m'
oa << m;
boost::asio::write(socket,b.data(),boost::asio::transfer_all());
// Server side:
boost::asio::streambuf b;
std::istream is(&b);
boost::archive::binary_iarchive ia(is);
boost::asio::read(socket,b,boost::asio::transfer_all());
message m;
ia >> m;
Which is not working. Server exits with invalid signature
exception.