boost::asio::buffer与矢量结构
boost::asio::buffer with vectors structs
我有两个问题:
-
在我的信息结构中,如果我有
float
或double
类型而不是std::string
,它可以正常工作,但如果我像下面这样使用std::string
,在我的客户端部分,我会收到结构,但之后它就会崩溃。 -
我可以
t even send it using
std::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;
}
- 在我的信息结构中,如果我使用
float
或double
类型而不是std::string
,它可以正常工作,但如果我使用下面的std::string
,在我的客户端部分,我会接收到结构,但之后它就会崩溃
这是因为float
或double
是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
等的原始二进制形式
相关文章:
- 如何循环打印顶点结构
- 通过方法访问结构
- 使用不带参数的函数访问结构元素
- 预处理器:插入结构名称中的前一个行号
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 孤立代码块在结构中引发异常
- 有什么方法可以遍历结构吗
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 无法将结构注册为增强几何体3D点
- 多成员Constexpr结构初始化
- C++将文本文件中的数据读取到结构数组中
- 如何重构类层次结构以避免菱形问题
- 如何在C++中序列化结构数据
- std::vector的包装器,使数组的结构看起来像结构的数组
- 没有为自己的结构调用列表推回方法
- 奇怪的结构&GCC&clang(void*返回类型)
- 在 c++ 中拥有一组结构的正确方法是什么?
- boost::asio::buffer与矢量结构
- 将嵌套结构变量从 C++ 传递到C++ dll 会导致 (L "Buffer is too small" && 0)