C++ 中的网络协议对象序列化

发布于 2024-08-13 20:49:08 字数 741 浏览 6 评论 0原文

我正在编写一些必须通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云淡月浅 2024-08-20 20:49:08

如果有像 Boost 这样的优秀库,为什么还要手动进行呢? .您可以构建的序列化

从他们的目标来看:

数据可移植性 - 在一个平台上创建的字节流应该可以在任何其他平台上读取。

您可能还感兴趣的是第 4 点和第 5 点:

  • 深度指针保存和恢复。也就是说,指针的保存和恢复是保存和恢复所指向的数据。
  • 正确恢复共享数据的指针。

Why do it all manually if there are great libraries like Boost.Serialization that you can build on?

From their goals:

Data Portability - Streams of bytes created on one platform should be readable on any other.

Also of interest for you might be points 4 and 5:

  • Deep pointer save and restore. That is, save and restore of pointers saves and restores the data pointed to.
  • Proper restoration of pointers to shared data.
紫轩蝶泪 2024-08-20 20:49:08

借用——使用经过测试的序列化库,例如前面提到的 < strong>Boost::SerializationGoogle 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

烂柯人 2024-08-20 20:49:08

另一种选择是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文