用不同语言编写的两个程序之间的通信 - 序列化?

发布于 2024-08-29 06:34:27 字数 167 浏览 2 评论 0原文

驻留在两台不同机器/网络/互联网上的程序之间的通信过程中何时需要序列化、编组等?

假设我有一个java/flash的客户端程序和一个C语言的服务器程序,我不能使用自己的自定义协议来实现通信吗?大概吧。什么时候需要序列化等?我知道Java RMI、CORBA 等都有这些机制。但为什么?是必须的吗?请赐教?

when is serialization,marshaling etc required during communication between programs residing across 2 different machines /network/Internet?

Suppose I have a client program in java/flash and a server program in C. Can't I implement communication using a custom protocol of my own ? I guess so. When is serialization etc needed?I am aware Java RMI,CORBA etc have these mechanisms. But why? Is it a must? please enlighten me?

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

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

发布评论

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

评论(3

橪书 2024-09-05 06:34:27

程序中的对象具有由编译器强加的明确定义的内存布局。但是,在另一台机器上运行、由不同编译器编译的另一个程序中,该布局不会完全相同。而且它通常与传输介质(例如网络连接或文件)不太兼容。您需要注意将对象从一台机器传输到另一台机器。

文件和网络数据包是简单的字节流。这就是序列化发挥作用的地方,您需要将内存中的对象序列化为字节流。它需要在接收端进行反序列化,从字节流返回到对象。

最明显的方法是二进制序列化。您获取对象中每个字段的字节并将它们写入流。效率很高,但也很麻烦。您遇到的第一个问题是接收端对对象的外观有不同的想法。它可能使用不同版本的对象声明进行编译,例如添加了字段的版本。当对象在不同机器之间交换时,问题更加明显。他们对于整数中的字节数可能有非常不同的想法。或字节顺序(字节序)。

这个问题有很多解决方案。它们通常涉及某种描述对象中字段的元数据。 Unicode 的出现使得将元数据和字段值放入文本描述中成为可能,XML 就是最好的例子。

Objects in your program have a well defined memory layout, imposed by your compiler. But that layout is not going to be exactly the same in another program, running on another machine, compiled by a different compiler. And it is not typically very compatible with the transport medium, like a network connection or a file. Which you need to take care of to get the object from one machine to another.

Files and network packets are simple streams of bytes. That's where serialization comes into play, you'll need to serialize the in-memory object into a stream of bytes. And it needs to be de-serialized at the receiving end, back from a stream of bytes into an object.

The obvious way to do so is binary serialization. You take the bytes for each field in the object and write them to the stream. Very efficient, but also very troublesome. The first problem you run into is that the receiving end has a different idea of what the object looks like. It might be compiled with a different version of the object declaration, one that had an added field for example. The problem is starker when the object is exchanged between different machines. They may have a very different idea about the number of bytes in an integer. Or the order of the bytes (endian-ness).

There have been many solutions to this problem. They typically involve some kind of metadata that describes the fields in the object. The advent of Unicode made it possible to put both the metadata and the field values into a textual description, XML is the best example of this.

z祗昰~ 2024-09-05 06:34:27

我不能使用以下方式实现通信吗
我自己的自定义协议?我猜
所以。

你可以。你可能不应该重新发明轮子。序列化很棘手。使用经过充分测试的标准溶液可以获得更好的结果。与编写数据传递例程相比,您学习 API 所花费的时间要少得多。

什么时候需要序列化等?

对于初学者来说,需要将一些内存结构从一个进程转移到另一个进程。

这里描述了更多用例:http://en.wikipedia.org/wiki/Serialization

我知道Java RMI、CORBA等都有这些机制。但为什么?是必须的吗?请赐教?

这些都不是“必须的”,就像你说的,你可以编写自己的协议。在我看来,您最好依靠该领域的一些现有技术,例如 XML 或您提到的其他技术之一。您使用哪种技术实际上取决于您想要做什么,所以我不会推测:)

传递序列化数据的一种很好的机制是 Google 的协议缓冲区。 它们负责编码(以比 XML 更有效的方式)和字节序转换。

Can't I implement communication using
a custom protocol of my own ? I guess
so.

You can. You probably shouldn't reinvent the wheel. Serialization is tricky. Use a well tested, standard solution for better results. You'll spend far less time learning an API than writing data passing routines.

When is serialization etc needed?

For starters, it's needed to take some in memory structure from one process to another.

There are more use cases described here: http://en.wikipedia.org/wiki/Serialization

I am aware Java RMI,CORBA etc have these mechanisms. But why? Is it a must? please enlighten me?

None of these "are a must", like you said, you could write your own protocol. You're far better of (IMO) leaning on some existing technologies in this area, like XML or one of the others you mention. Which technology you use is really dependent on what you're trying to do, so I'm not going to speculate :)

One great mechanism for passing serialized data is Google's protocol buffers. They take care of the encoding (in a much more efficient way than XML) and endian translation.

许一世地老天荒 2024-09-05 06:34:27

目前最好的方法是来回发送 XML 消息。

The best way to this these days is to send XML messages back and forth.

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