加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c boost :: asio :: buffer和结构

发布时间:2020-12-16 10:18:49 所属栏目:百科 来源:网络整理
导读:无论如何基本上做以下事情: #include boost/asio.hppstruct testStruct{ int x; int y;};int main(){ struct testStruct t; boost::asio::buffer b; b = boost::asio::buffer(t); return 0;} 似乎失败的地方是将’t’传递到缓冲区’b’. 解决方法 使用多个
无论如何基本上做以下事情:

#include <boost/asio.hpp>

struct testStruct{
    int x;
    int y;
};

int main(){
    struct testStruct t;
    boost::asio::buffer b;
    b = boost::asio::buffer(t); 
    return 0;
}

似乎失败的地方是将’t’传递到缓冲区’b’.

解决方法

使用多个缓冲区的 scatter操作:

#include <boost/asio.hpp>

#include <vector>

struct testStruct{
    int x;
    int y;
};

int
main()
{
    struct testStruct t;
    t.x = 5;
    t.y = 7;

    std::vector<boost::asio::const_buffer> buffers;
    buffers.push_back( boost::asio::buffer(&t.x,sizeof(t.x) ) );
    buffers.push_back( boost::asio::buffer(&t.y,sizeof(t.y) ) );

    boost::asio::io_service io_service;
    boost::asio::ip::tcp::socket socket( io_service ); // note not connected!
    std::size_t length = boost::asio::write( socket,buffers );

    return 0;
}

请注意,您需要在接收方使用相应的聚集.除了你提出的人为例子之外,这一点非常繁琐.这就是I suggested在previous question中使用更强大的序列化机制的原因.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读