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

    <small id='TRTQt'></small><noframes id='TRTQt'>

        • <bdo id='TRTQt'></bdo><ul id='TRTQt'></ul>

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

        时间:2023-10-06

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

        <small id='unfAj'></small><noframes id='unfAj'>

            • <bdo id='unfAj'></bdo><ul id='unfAj'></ul>
                <legend id='unfAj'><style id='unfAj'><dir id='unfAj'><q id='unfAj'></q></dir></style></legend>

                <tfoot id='unfAj'></tfoot>

                  本文介绍了使用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序列化和发送数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

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

                  相关文章

                  最新文章

                  1. <legend id='Ah4p6'><style id='Ah4p6'><dir id='Ah4p6'><q id='Ah4p6'></q></dir></style></legend>

                    <tfoot id='Ah4p6'></tfoot>

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

                      • <bdo id='Ah4p6'></bdo><ul id='Ah4p6'></ul>