<legend id='uEF68'><style id='uEF68'><dir id='uEF68'><q id='uEF68'></q></dir></style></legend>

      <tfoot id='uEF68'></tfoot>

      <i id='uEF68'><tr id='uEF68'><dt id='uEF68'><q id='uEF68'><span id='uEF68'><b id='uEF68'><form id='uEF68'><ins id='uEF68'></ins><ul id='uEF68'></ul><sub id='uEF68'></sub></form><legend id='uEF68'></legend><bdo id='uEF68'><pre id='uEF68'><center id='uEF68'></center></pre></bdo></b><th id='uEF68'></th></span></q></dt></tr></i><div id='uEF68'><tfoot id='uEF68'></tfoot><dl id='uEF68'><fieldset id='uEF68'></fieldset></dl></div>
        <bdo id='uEF68'></bdo><ul id='uEF68'></ul>
      1. <small id='uEF68'></small><noframes id='uEF68'>

        使用Boost序列化和发送数据结构?

        时间:2023-06-04
      2. <legend id='QNDvH'><style id='QNDvH'><dir id='QNDvH'><q id='QNDvH'></q></dir></style></legend>

            <bdo id='QNDvH'></bdo><ul id='QNDvH'></ul>
            • <small id='QNDvH'></small><noframes id='QNDvH'>

                  <tbody id='QNDvH'></tbody>

              1. <tfoot id='QNDvH'></tfoot>
              2. <i id='QNDvH'><tr id='QNDvH'><dt id='QNDvH'><q id='QNDvH'><span id='QNDvH'><b id='QNDvH'><form id='QNDvH'><ins id='QNDvH'></ins><ul id='QNDvH'></ul><sub id='QNDvH'></sub></form><legend id='QNDvH'></legend><bdo id='QNDvH'><pre id='QNDvH'><center id='QNDvH'></center></pre></bdo></b><th id='QNDvH'></th></span></q></dt></tr></i><div id='QNDvH'><tfoot id='QNDvH'></tfoot><dl id='QNDvH'><fieldset id='QNDvH'></fieldset></dl></div>
                • 本文介绍了使用Boost序列化和发送数据结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个如下所示的数据结构:

                  <前>类型定义结构{无符号短 m_short1;无符号短 m_short2;无符号字符 m_character;我的数据类型;

                  我想使用 boost::serialization 来序列化这个数据结构,然后使用 boost::asio 通过 TCP/IP 传输它,然后让另一个应用程序接收数据并使用相同的 boost 库反序列化它.

                  我正在尝试关注 boost::serialization教程,(正如其他一些 SO 问题所建议的那样) 但该示例专门用于写入/读取文件,而不是使用 boost::asio 的套接字.

                  我很确定我有适合这项工作的工具——我只是需要帮助让它们协同工作.写入套接字与写入文件不会有什么不同,对吗?

                  非常感谢任何建议.谢谢!

                  解决方案

                  对于如此简单的结构,boost::serialization 是矫枉过正,开销巨大.

                  做得更简单:

                  vector净(3,0);net[0]=htons(data.m_short1);net[1]=htons(data.m_short2);net[2]=htons(data.character);asio::async_write(socket,buffer((char*)&net.front(),6),callback);向量净(3,0);asio::async_read(socket,buffer((char*)&net.front(),6),callback);打回来:data.m_short1=ntohs(net[0]);data.m_short2=ntohs(net[1]);data.character=ntohs(net[2]);

                  并为自己节省 boost::serialization 带来的巨大开销

                  如果您使用具有相同字节顺序的计算机工作的私有协议(大/小)只是按原样发送结构——POD.

                  I have a data structure that looks like this:

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

                  I want to use boost::serialization to serialize this data structure, then use boost::asio to transmit it via TCP/IP, then have another application receive the data and de-serialize it using the same boost libraries.

                  I'm trying to following boost::serialization tutorial, (as some other SO questions have suggested) but the example is specifically for writing/reading to a file, not to a socket using boost::asio.

                  I'm pretty sure I've got the right tools for the job -- I just need help making them work together. Writing to a socket can't be that different from writing to a file, right?

                  Any suggestions are very much appreciated. Thanks!

                  解决方案

                  For such simple structure, boost::serialization is overkill and huge overhead.

                  Do simpler:

                  vector<uint16_t> net(3,0);
                  
                  net[0]=htons(data.m_short1);
                  net[1]=htons(data.m_short2);
                  net[2]=htons(data.character);
                  
                  asio::async_write(socket,buffer((char*)&net.front(),6),callback);
                  
                  vector<uint16_t> net(3,0);
                  asio::async_read(socket,buffer((char*)&net.front(),6),callback);
                  
                  callback:
                  data.m_short1=ntohs(net[0]);
                  data.m_short2=ntohs(net[1]);
                  data.character=ntohs(net[2]);
                  

                  And Save yourself HUGE overhead that boost::serialization has

                  And if you private protocol where computers with same order of bytes work (big/little) that just send structure as is -- POD.

                  这篇关于使用Boost序列化和发送数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何使用 Boost.serialize 序列化派生模板类? 下一篇:C++ 序列化性能

                  相关文章

                • <legend id='4ocZn'><style id='4ocZn'><dir id='4ocZn'><q id='4ocZn'></q></dir></style></legend>
                    <i id='4ocZn'><tr id='4ocZn'><dt id='4ocZn'><q id='4ocZn'><span id='4ocZn'><b id='4ocZn'><form id='4ocZn'><ins id='4ocZn'></ins><ul id='4ocZn'></ul><sub id='4ocZn'></sub></form><legend id='4ocZn'></legend><bdo id='4ocZn'><pre id='4ocZn'><center id='4ocZn'></center></pre></bdo></b><th id='4ocZn'></th></span></q></dt></tr></i><div id='4ocZn'><tfoot id='4ocZn'></tfoot><dl id='4ocZn'><fieldset id='4ocZn'></fieldset></dl></div>

                      <bdo id='4ocZn'></bdo><ul id='4ocZn'></ul>

                    <small id='4ocZn'></small><noframes id='4ocZn'>

                    1. <tfoot id='4ocZn'></tfoot>