boost::asio::buffer与矢量结构

boost::asio::buffer with vectors structs

本文关键字:结构 buffer asio boost      更新时间:2023-10-16

我有两个问题:

  1. 在我的信息结构中,如果我有floatdouble类型而不是std::string,它可以正常工作,但如果我像下面这样使用std::string,在我的客户端部分,我会收到结构,但之后它就会崩溃。

  2. 我可以t even send it usingstd::vector`,如下所示:


struct info
{
int id;
std::string name;
};

int main(int argc, char** argv)
{
boost::asio::io_service ios;
boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string("127.0.0.1"), 12345);
boost::asio::ip::tcp::socket cl1(ios);
cl1.open(ep.protocol());
boost::system::error_code ec;
cl1.connect(ep, ec);
if (ec == 0)
cout << "Connected" << endl;
else {
cout << "Not connected" << endl;
system("pause");
return -2;
}
info student;
student.id = 7;
student.name = "Rasul";
cl1.send(boost::asio::buffer(&student, sizeof(student)));
if (ec == 0)
cout << "Written" << endl;
else {
cout << "Not written" << endl;
system("pause");
return -2;
}
cout << "Done" << endl;
system("pause");
return 0;
}
  1. 在我的信息结构中,如果我使用floatdouble类型而不是std::string,它可以正常工作,但如果我使用下面的std::string,在我的客户端部分,我会接收到结构,但之后它就会崩溃

这是因为floatdouble是POD数据类型,而std::string不是,违反了合同:

boost::asio::buffer函数用于创建一个缓冲区对象来表示原始内存、POD元素数组、POD元素向量或std::string

准确地说,它们的意思是"[a(array|vector)of POD elements]或[astd::string]">,当您查看过载列表时,这一点很清楚

添加一个静态断言,您将看到:

static_assert(std::is_pod<info>::value, "whoops, that can't work");
cl1.send(boost::asio::buffer(&student, sizeof(student)));

如果你想序列化它,你必须写一些代码。假设体系结构的独立性和可移植性通常不是问题,则可以:

int32_t id_and_size[] = {student.id, static_cast<int32_t>(student.name.length())};
assert(id_and_size[1] >= 0); // because `size_t` is unsigned and larger than int32_t
cl1.send(boost::asio::buffer(id_and_size));
cl1.send(boost::asio::buffer(student.name.data(), student.name.length()));

这将发送与数据分开的长度:Live On Coliru](http://coliru.stacked-crooked.com/a/897573028a86e16e)

00000000:0700 0000 0500 0000 5261 7375 6c。。。。。。。。Rasul

尽管这很容易出错,也很乏味。如果你控制两端,考虑一个序列化框架(Boost serialization,Google Protobuf…)

由于您已经提到序列化double等的原始二进制形式