Serialize and send a data structure using Boost?

后端 未结 7 1406
半阙折子戏
半阙折子戏 2020-12-02 10:45

I have a data structure that looks like this:

typedef struct
{
  unsigned short m_short1;
  unsigned short m_short2;
  unsigned char m_character;
} MyDataType;
         


        
7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 11:33

    There is a good serialization example in the asio documentation: server.cpp, stock.hpp, connection.hpp.

    Here's a snippet:

    std::ostringstream archive_stream;
    boost::archive::text_oarchive archive(archive_stream);
    archive << your_struct;
    outbound_data_ = archive_stream.str();
    boost::asio::async_write(socket_, 
        boost::asio::buffer(outbound_data_), handler);
    

提交回复
热议问题