You can easily send any std stream, so you can also use stringstream . You can write binary data to your string stream (it's just an array of bytes, efficiently).
A few samples:
boost::asio::streambuf request; std::ostream request_stream(&request); request_stream.write(&binarydata_buf, sizeof(binarydata_buf)); // or use stream operators: request_stream << "xxx" << yyy; // Send the request. boost::asio::write(socket, request);
If you already have a fully populated istream (using std :: cin as dummy in this example):
boost::asio::streambuf request; std::ostream request_stream(&request); request_stream << std::cin.rdbuf() << std::flush;
Istream fill methods are, for example, ostream::write or Boost Serialization binary_archive
There are many ways to throw a cat, of course, so be sure to think about other options before blindly copying it.
See How to send ostream via forced sockets in C ++?
source share