C++ 中的网络协议对象序列化
我正在编写一些必须通过 TCP/IP 发送数据的 C++ 代码。我希望这段代码可以在 Linux/Windows/Osx 上移植。现在,因为这是我第一次编写可移植网络代码,所以我基本上需要一些简单的函数来添加到某些对象,例如:
class myclass{
...member...
public:
string serialize(){
std::ostringstream out();
out << member1;
out << member2;
out << member3;
return out.str();
}
}
...这就是我现在所需要的。不管怎样,我开始阅读 ostringstream 相关文档并发现了二进制/文本问题。事实上,它会将换行符转换为每个系统的正确顺序。例如,假设一个成员是一个指向 const char* foo = "Hello\nMan\n" 的指针,它将在 Linux 上以特定字节序列进行翻译,在 Windows 上以另一个字节序列进行翻译......等等。我的字节将通过互联网传输到数据包中,不同的操作系统机器将读取它们,我认为会发生问题...现在我读到我可以使用 ostringstream(ios: :bin)
...它能解决问题吗(假设我将使用一个使用 istringstream(ios::bin)
的反序列化函数???我对整个图片感到困惑,如果您能花一些澄清的话,我们将不胜感激
。
I'm writing some C++ code that will have to send data over TCP/IP. I want this code to be portable on Linux/Windows/Osx. Now, as it is the first time I write portable network code, I basically need some simple functions to add to certain objects like:
class myclass{
...member...
public:
string serialize(){
std::ostringstream out();
out << member1;
out << member2;
out << member3;
return out.str();
}
}
... which is all I need for now. Anyway I started reading ostringstream related docs and turns out the binary/text problem. In fact it will convert line breaks to the right sequence of everysystem. Suppose for example that a member is a pointer to const char* foo = "Hello\nMan\n", that will be translated in certain byte sequence on linux, another on windows... and so on. My bytes will go on a packet over the internet, a different OS machine will read them and I think trouble will occurr... Now I read that I might initialize ostringstream
with ostringstream(ios::bin)
... Will it solve the problem (provided that I will use a de-serialization function that will use a istringstream(ios::bin)
??? I'm confused about the whole picture, if you may spend a few clarifying lines that'll be much appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果有像 Boost 这样的优秀库,为什么还要手动进行呢? .您可以构建的序列化?
从他们的目标来看:
您可能还感兴趣的是第 4 点和第 5 点:
Why do it all manually if there are great libraries like Boost.Serialization that you can build on?
From their goals:
Also of interest for you might be points 4 and 5:
借用——使用经过测试的序列化库,例如前面提到的 < strong>Boost::Serialization 或 Google Protocol Buffers。这些不应引入进一步的依赖性。
如果您对全新框架持开放态度,那么 Qt 还具有 Qt 序列化
Seconded -- use a tested serialization library like the aforementioned Boost::Serialization or Google Protocol Buffers. These should not introduce further dependencies.
If you are open to a whole new framework, then Qt also has Qt Serialization
另一种选择是 ACE 框架。它通过 CORBA 编组提供序列化/反序列化(请参阅类 ACE_InputCDR 和 ACE_OutputCDR)。
如果您不了解 ACE,这是一个巨大的框架,包括完整的 CORBA-Runtime。但您只需要核心 ACE 库即可进行序列化。
Another option is the ACE framework. It provides serialization/deserialization with CORBA marshalling (see classes ACE_InputCDR and ACE_OutputCDR).
If you don't know ACE, this is a huge framework including a complete CORBA-Runtime. But you only need the core ACE libraries for serialization.