在windows和solaris之间交换二进制格式的数据有哪些可能的方法?

发布于 2024-08-11 05:07:45 字数 125 浏览 11 评论 0原文

有人可以帮忙告诉我是否有任何可能的方法可以通过互联网套接字在 Windows 上运行的程序和 Unix 上运行的其他程序之间传递数据结构(即二进制格式)?

任何与此相关的想法或材料链接将不胜感激。预先感谢您的帮助, MK

Could someone please help and tell me if there is any possible way to pass a data structure (i.e. binary format) through internet sockets between a program running on Windows and other program running on Unix?

Any idea or link to materials that deal with it would be very appreciated. Thanking you in advance for your help,
Mk

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

初心 2024-08-18 05:07:45

查看 Google 的协议缓冲区,将其作为序列化数据的快速方法。

Check out Google's protocol buffers as a fast way of serializing data.

掩耳倾听 2024-08-18 05:07:45

是的,您正在寻找某种序列化。存在多种执行此操作的方法,包括:

通常,您会使用库来帮助您完成此操作。您可能还会遇到一些工具,它们会生成代码以链接到您的程序中。

Yes, what you are looking for is some sort of serialization. Many ways of doing this exist, including:

Typically, you would use a library to assist you in this. You may also encounter tools that will generate code for you to link into your program.

我纯我任性 2024-08-18 05:07:45

如果您反对文本序列化并且确实想要一个结构,那么就像大多数网络协议一样,在发送时使用“主机到网络”,在接收结构中的所有字段时使用“网络到主机”。这个想法是,所有发送者,无论字节序如何,总是转换为网络(大字节序,我忘记哪个是哪个)。然后所有接收器都会转换为任何内容(也可能是大端字节序,这没有变化)。

已经有相关的 api 了。它们是 ntohs(网络到主机,16 位字段的缩写)和 ntohl(32 位字段)。当然还有 htons 和 htonl。

因此,对于这样的结构:

typedef struct
{
    unsigned char  stuff1;
    unsigned char  stuff2;
    unsigned short stuff3;
    unsigned int   stuff4;
}tFoo;

发送代码将执行类似以下操作:

tFoo to_send;
to_send.stuff1 = local_stuff1;
to_send.stuff2 = local_stuff2;
to_send.stuff3 = htons(local_stuff3);
to_send.stuff4 = htonl(local_stuff4);

接收代码将执行类似以下操作:

local_stuff3 = ntohs(from.stuff3);
local_stuff4 = ntohl(from.stuff4);

请注意,结构的打包/对齐很重要。您必须对对齐感到厌倦,因为编译器与编译器之间的对齐并不总是相同,甚至对于不同 CPU 架构上的相同编译器也是如此。它甚至可以针对数据类型本身进行更改(每个 arch 的 int 大小可能不同)。我试图用前两个 8 位字符,后跟一个 16 位和一个 32 位字符来演示这一点,总共 8 个字节。您必须确保当您移植到不同的编译器/架构时,您确实获得了正确的打包/大小。

出于这个原因,大多数人选择序列化,这可能也是为什么大多数人都会回答这个问题。这是最不容易出错的。

祝你好运。

If you are against the text serialization and really want a struct, then do it like most network protocols do with "host to network" when sending and "network to host" when receiving for all fields within the struct. The idea is that all senders no matter what endianness they are always translate to network (big-endian I forget which is which). Then all receivers translate to whatever they are (might also be big-endian which is no change).

There are apis already for this. They are ntohs (network to host short for 16-bit fields) and ntohl (32-bit fields). Then of course htons and htonl.

So for a struct like this:

typedef struct
{
    unsigned char  stuff1;
    unsigned char  stuff2;
    unsigned short stuff3;
    unsigned int   stuff4;
}tFoo;

The sending code would do something like:

tFoo to_send;
to_send.stuff1 = local_stuff1;
to_send.stuff2 = local_stuff2;
to_send.stuff3 = htons(local_stuff3);
to_send.stuff4 = htonl(local_stuff4);

The receiving code would do something like:

local_stuff3 = ntohs(from.stuff3);
local_stuff4 = ntohl(from.stuff4);

Note that packing/alignment matters of the struct. You have to be weary of alignment which isn't always the same from compiler to compiler and even for the same compiler on different cpu architectures. It can even change for the datatypes themselves (an int may not be the same size from arch to arch). I attempted to demonstrate that a bit with the first two 8-bit chars, followed by a 16-bit and a 32-bit for a total of 8 bytes. You have to be certain that when you port to a different compiler/arch that you are indeed getting the correct packing/size.

For that reason most people choose serialization and probably why most people of answered with that. It is the least error prone.

Good luck.

恬淡成诗 2024-08-18 05:07:45

Boost序列化似乎是一个不错的选择。

Boost serialization seems like a good choice.

巴黎夜雨 2024-08-18 05:07:45

尽管要求使用二进制格式,但如果您想要调试此类内容,那么拥有一种可以作为人类阅读的文本格式确实很有帮助,因此 XML 和 JSON 的建议可能是合适的。大多数系统和语言都有方便的免费库来读写这两种系统和语言,并且在许多系统和语言中,XML 是内置的。这也往往意味着它们经过了良好的测试、集成和高性能。

Although a binary format was requested, if you ever want to debug this kind of thing, it's really helpful to have a textual format that you can read as a human, so the suggestions of XML and maybe JSON might be appropriate. Most systems and languages have handy free libraries for reading and writing both of those, and in many, XML is built in. This also tends to mean they are well tested, integrated and performant.

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