I was going to send the structure from client to server using boost :: asio :: async_write_some , in this case boost :: serialization comes to the rescue:

  //boost::serialization struct blank { int m_id; std::string m_message; template<typename archive> void serialize(archive& ar, const short version) { ar & m_id; ar & m_message; } }; blank info; info.m_id = 1; info.m_name = "Rasul"; std::stringstream ss; boost::archive::binary_oarchive out_archive(ss); out_archive << info; 

How can I send / receive out_archive using boost :: asio asynchronously? If you have any other ideas?

    1 answer 1

    std::stringstream , into which you serialize your structure, has a str method that allows you to get the contents of a stream as a string, you can already convert it to a binary buffer using asio::buffer , which can be sent asynchronously. You just need to remember that the buffer actually stores a reference to the original string, and you need to prevent it from being deleted ahead of time.

    In terms of performance, this is not the best approach, since the stream content is copied to the string, but boost :: serialization is not famous for performance anyway. You can try to optimize a little and get into the interior of the stream using rdbuf , but here you need to deal with the internal structure of standard streams. It's easier to take another serialization library without using streams — ultimately you just need to get an array of bytes.