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

        <legend id='O6p6s'><style id='O6p6s'><dir id='O6p6s'><q id='O6p6s'></q></dir></style></legend>
      1. <small id='O6p6s'></small><noframes id='O6p6s'>

        在 boost::asio 中发送/接收一个结构体

        sending/receiving a struct in boost::asio(在 boost::asio 中发送/接收一个结构体)
      2. <tfoot id='BKyKu'></tfoot>
          <tbody id='BKyKu'></tbody>
      3. <i id='BKyKu'><tr id='BKyKu'><dt id='BKyKu'><q id='BKyKu'><span id='BKyKu'><b id='BKyKu'><form id='BKyKu'><ins id='BKyKu'></ins><ul id='BKyKu'></ul><sub id='BKyKu'></sub></form><legend id='BKyKu'></legend><bdo id='BKyKu'><pre id='BKyKu'><center id='BKyKu'></center></pre></bdo></b><th id='BKyKu'></th></span></q></dt></tr></i><div id='BKyKu'><tfoot id='BKyKu'></tfoot><dl id='BKyKu'><fieldset id='BKyKu'></fieldset></dl></div>

          <bdo id='BKyKu'></bdo><ul id='BKyKu'></ul>

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

          • <legend id='BKyKu'><style id='BKyKu'><dir id='BKyKu'><q id='BKyKu'></q></dir></style></legend>

                  本文介绍了在 boost::asio 中发送/接收一个结构体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我打算使用 boost::asio::async_write_some 从客户端向服务器发送一个结构体,在本例中为 boost::serializationboost::property_tree 来帮忙,

                  I was going to send a struct from a client to a server using boost::asio::async_write_some, in this case boost::serialization and boost::property_tree come to help,

                  //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;
                  

                  那么,现在我如何使用 boost::asio 异步发送/接收 out_archive .. 或

                  So, now how can I send/receive out_archive using boost::asio asynchronously .. or

                  //boost::property_tree
                  boost::property_tree::ptree root;
                  root.put("id", 2);
                  root.put("name", "Rasul");
                  

                  如何使用 boost::asio 异步发送/接收根????(如果您有其他想法,请分享)

                  How can I send/receive root using boost::asio asynchronously??? (If you have some other ideas please share them)

                  推荐答案

                  好吧,完全不清楚您尝试了什么或问题是什么,所以您开始吧:

                  Ok, it's not clear at all what you've tried or what the problem is, so here you go:

                  生活在 Coliru

                  #include <boost/archive/binary_oarchive.hpp>
                  #include <boost/serialization/serialization.hpp>
                  #include <boost/asio.hpp>
                  #include <iostream>
                  
                  //boost::serialization
                  struct blank
                  {
                      int m_id;
                      std::string m_message;
                  
                      template<typename archive> void serialize(archive& ar, const unsigned /*version*/) {
                          ar & m_id;
                          ar & m_message;
                      }
                  };
                  
                  void on_send_completed(boost::system::error_code ec, size_t bytes_transferred) {
                      if (ec)
                          std::cout << "Send failed: " << ec.message() << "
                  ";
                      else
                          std::cout << "Send succesful (" << bytes_transferred << " bytes)
                  ";
                  }
                  
                  namespace ba = boost::asio;
                  using ba::ip::tcp;
                  
                  struct IO {
                      boost::asio::streambuf buf;
                  
                      void send_asynchronously(tcp::socket& socket) {
                          blank info;
                  
                          info.m_id = 1;
                          info.m_message = "Rasul";
                  
                          {
                              std::ostream os(&buf);
                              boost::archive::binary_oarchive out_archive(os);
                              out_archive << info;
                          }
                  
                          async_write(socket, buf, on_send_completed);
                      }
                  };
                  
                  int main() {
                      ba::io_service ios;
                      tcp::socket s(ios);
                      s.connect({{},6868});
                  
                      IO io;
                      io.send_asynchronously(s);
                  
                      ios.run();
                  }
                  

                  当针对例如运行时netcat -l -p 6868 |xxd:

                  Send succesful (62 bytes)
                  00000000: 1600 0000 0000 0000 7365 7269 616c 697a  ........serializ
                  00000010: 6174 696f 6e3a 3a61 7263 6869 7665 0f00  ation::archive..
                  00000020: 0408 0408 0100 0000 0000 0000 0001 0000  ................
                  00000030: 0005 0000 0000 0000 0052 6173 756c       .........Rasul
                  

                  Boost 属性树 + 序列化

                  一些变化:

                  生活在 Coliru

                  void send_asynchronously(tcp::socket& socket) {
                      boost::property_tree::ptree root;
                      root.put("id", 2);
                      root.put("name", "Rasul");
                  
                      {
                          std::ostream os(&buf);
                          boost::archive::binary_oarchive out_archive(os);
                          out_archive << root;
                      }
                  
                      async_write(socket, buf, on_send_completed);
                  }
                  

                  打印:

                  Send succesful (138 bytes)
                  00000000: 1600 0000 0000 0000 7365 7269 616c 697a  ........serializ
                  00000010: 6174 696f 6e3a 3a61 7263 6869 7665 0f00  ation::archive..
                  00000020: 0408 0408 0100 0000 0000 0000 0002 0000  ................
                  00000030: 0000 0000 0000 0000 0000 0000 0000 0200  ................
                  00000040: 0000 0000 0000 6964 0000 0000 0000 0000  ......id........
                  00000050: 0000 0000 0100 0000 0000 0000 3204 0000  ............2...
                  00000060: 0000 0000 006e 616d 6500 0000 0000 0000  .....name.......
                  00000070: 0000 0000 0005 0000 0000 0000 0052 6173  .............Ras
                  00000080: 756c 0000 0000 0000 0000                 ul........
                  

                  这篇关于在 boost::asio 中发送/接收一个结构体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to limit the number of running instances in C++(C++中如何限制运行实例的数量)
                  Using boost::asio::async_read with stdin?(将 boost::asio::async_read 与 stdin 一起使用?)
                  How to find out what dependencies (i.e other Boost libraries) a particular Boost library requires?(如何找出特定 Boost 库需要哪些依赖项(即其他 Boost 库)?)
                  What#39;s the purpose of a leading quot;::quot; in a C++ method call(引导“::的目的是什么?在 C++ 方法调用中)
                  Boost Spirit x3: parse into structs(Boost Spirit x3:解析为结构体)
                  How boost auto-linking makes choice?(boost自动链接如何做出选择?)
                1. <i id='D8nDE'><tr id='D8nDE'><dt id='D8nDE'><q id='D8nDE'><span id='D8nDE'><b id='D8nDE'><form id='D8nDE'><ins id='D8nDE'></ins><ul id='D8nDE'></ul><sub id='D8nDE'></sub></form><legend id='D8nDE'></legend><bdo id='D8nDE'><pre id='D8nDE'><center id='D8nDE'></center></pre></bdo></b><th id='D8nDE'></th></span></q></dt></tr></i><div id='D8nDE'><tfoot id='D8nDE'></tfoot><dl id='D8nDE'><fieldset id='D8nDE'></fieldset></dl></div>
                  <tfoot id='D8nDE'></tfoot>

                        1. <legend id='D8nDE'><style id='D8nDE'><dir id='D8nDE'><q id='D8nDE'></q></dir></style></legend>
                        2. <small id='D8nDE'></small><noframes id='D8nDE'>

                            <bdo id='D8nDE'></bdo><ul id='D8nDE'></ul>
                              <tbody id='D8nDE'></tbody>