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

  1. <tfoot id='6gwfC'></tfoot>
      • <bdo id='6gwfC'></bdo><ul id='6gwfC'></ul>
      <legend id='6gwfC'><style id='6gwfC'><dir id='6gwfC'><q id='6gwfC'></q></dir></style></legend>

      <small id='6gwfC'></small><noframes id='6gwfC'>

    1. boost asio 写/读向量

      时间:2023-09-15
      <tfoot id='XciXk'></tfoot>

          <tbody id='XciXk'></tbody>

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

        1. <legend id='XciXk'><style id='XciXk'><dir id='XciXk'><q id='XciXk'></q></dir></style></legend>
              <bdo id='XciXk'></bdo><ul id='XciXk'></ul>
                <i id='XciXk'><tr id='XciXk'><dt id='XciXk'><q id='XciXk'><span id='XciXk'><b id='XciXk'><form id='XciXk'><ins id='XciXk'></ins><ul id='XciXk'></ul><sub id='XciXk'></sub></form><legend id='XciXk'></legend><bdo id='XciXk'><pre id='XciXk'><center id='XciXk'></center></pre></bdo></b><th id='XciXk'></th></span></q></dt></tr></i><div id='XciXk'><tfoot id='XciXk'></tfoot><dl id='XciXk'><fieldset id='XciXk'></fieldset></dl></div>
              • 本文介绍了boost asio 写/读向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我无法从 boost asio 缓冲区读取向量.我有这个向量:

                I have trouble reading a vector from boost asio buffer. I have this vector:

                 std::vector<float> points;
                

                我用 boost asio write 发送

                And I send it with boost asio write

                  boost::asio::write (socket, boost::asio::buffer(&new_buffers->points.front(), nr_points * 3 * sizeof (float)));
                

                另一方面,我有:

                std::vector<float> recv_vector;
                tcp_socket.async_read_some(boost::asio::buffer(recv_vector), read_handler);
                

                当我执行 recv_vector.size() 时,它总是空的.

                When I do recv_vector.size(), its always empty.

                谁能告诉我我做错了什么?

                Can somebody tell me what I am doing wrong?

                马雷克

                推荐答案

                Asio 不是序列化库.它不会序列化随机向量(你可以使用 Boost Serialization).

                Asio is not a serialization library. It does not serialize random vectors (you could use Boost Serialization for that).

                它只是将您的缓冲区视为 IO 操作的数据缓冲区.所以,例如在第二种情况下:

                It merely treats your buffer as the data buffer for the IO operation. So, e.g. in the second instance:

                std::vector<float> recv_vector;
                tcp_socket.async_read_some(boost::asio::buffer(recv_vector), read_handler);
                

                您告诉它接收适合由向量 recv_vector 表示的 POD 缓冲区的数据量,该向量为空.因此,您是在告诉 Asio 接收 0 字节,我认为它会为您带来快乐.

                you tell it to receive the amount of data that fits into the POD buffer represented by the vector recv_vector, which is empty. You are therefore telling Asio to receive 0 bytes, which I presume it will do happily for you.

                要解决问题,请使用序列化(将责任交给另一个库)或在实际数据之前手动发送向量的大小.

                To fix things, either use serialization (putting the responsibility in the hands of another library) or manually send the size of the vector before the actual data.

                我在这里有更多解释和完整示例:如何从套接字读取中接收自定义数据类型?

                I have more explanations and a full sample here: How to receive a custom data type from socket read?

                还要注意,您不必做那么复杂的事情:

                Note, as well, that you don't have to do that complicated thing:

                boost::asio::write (socket, boost::asio::buffer(&new_buffers->points.front(), nr_points * 3 * sizeof (float)));
                

                相反,对于 POD 类型向量,只需 让 Asio 进行计算和转换:

                Instead, for POD type vectors, just let Asio do the calculations and casts:

                boost::asio::write (socket, buffer(new_buffers->points));
                

                这篇关于boost asio 写/读向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:错误:'i' 没有用 auto 命名类型 下一篇:如何将 std::string 复制到 std::vector<char> 中?

                相关文章

                最新文章

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

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

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