客户端/服务器通信的 Java 标准

发布于 2024-10-12 23:32:14 字数 416 浏览 6 评论 0原文

用于客户端/服务器或 P2P 通信的“官方”Java API 是什么? Java RMI?其他一些网络API?

这个官方网络 API 是 SE 和 EE 两者的标准吗?

我确信答案是特定于上下文的,所以让我们看几个实例:

  1. 您在 2 台机器上安装了 2 个 swing 客户端并连接到同一网络(或 Internet),并且您希望其中任何一个能够向对方发送一个原语,例如整数 4,或一些 POJO,例如“Widget”对象
  2. 与上面的 #1 相同,但在 Swing 客户端和完全兼容的 Java EE 后端之间(实现托管 bean、应用程序服务器,整个九码)

我没有想到具体的应用程序,我只是想知道Java世界中客户端-客户端和客户端-服务器通信的“规范”是什么。

What is the "official" Java API for client/server or P2P communication? Java RMI? Some other networking API??

Is this official networking API the standard for both SE and EE?

I'm sure the answer is very context-specific, so let's take a look at a few instances:

  1. You have 2 swing clients installed on 2 machines and connected to the same network (or the Internet), and you want either of them to send the other a primitive, such as the integer 4, or some POJO, like a "Widget" object
  2. Same as #1 above, but between a Swing client and a fully-compliant Java EE back-end (implementing managed beans, app servers, the whole nine yards)

I don't have a specific application in mind, I'm just wondering what are the "norms" for client-client and client-server communication in the world of Java.

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

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

发布评论

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

评论(5

锦欢 2024-10-19 23:32:14

如果受 Java 束缚不是问题,那么当涉及到客户端和服务器解决方案“交换”数据时(尤其是当数据是 Java 类时,这可能很难/需要太多精力来表示为文本),RMI 是一个非常抽象的解决方案数据)。只需确保您的对象实现了 Serialized,并且几乎任何内容都可以通过线路传输。

如果这不符合你的要求,并且你想放弃原始的网络内容,客户端-服务器套接字框架 Netty< /a> 是一个不错的选择。

If being bound by Java isn't a problem, RMI is a pretty abstracted solution when it comes to the client and server solution "exchanging" data (especially when the data is Java classes which might be difficult/too much effort to represent as textual data). Just make sure your object implements Serializable and almost anything can be transmitted over the wire.

If this doesn't fit your bill and you want to drop down the raw networking stuff, the client-server socket framework Netty is a pretty good choice.

烟火散人牵绊 2024-10-19 23:32:14

J2SE 中不存在最官方的网络 API,所有 J2SE API 都是官方的,因为它们都受到 Sun(现在的 Oracle)的支持。
也就是说,您应该根据以下标准选择 API:

  • 您(或您的团队)是否知道如何使用特定 API?
  • 这个 API 使用起来有多简单/复杂;
  • 您的目标吞吐量是多少?对于性能敏感的应用程序,您可能被迫使用二进制协议。对于其余情况,您可以使用基于文本的协议。

例如,在两个客户端之间,简单的基于文本的协议足以传递 POJO,例如使用 Apache MINA 或 Google 协议缓冲区。
这也适用于客户端和服务器之间。


对 Zac 在评论中提出的问题的回应:

  1. 二进制协议的性能提升来自于这样一个事实:您不需要将所有内容都转换为文本形式并返回 - 您只需以最小的更改即可传递应用程序内存的二进制表示形式,就像 BSD Sockets API 的情况一样,从主机字节顺序转换为网络字节顺序。不幸的是,我不知道 RMI/Java 序列化如何处理对象的详细信息,但我确信,它仍然比以可读形式传递所有数据要快得多;
  2. 是的,MINA 和protocol buffers 有Java API。它们只是不属于 Java SE 捆绑包的一部分,您必须单独下载它们。顺便说一句,MINA 可以使用二进制和可读序列化,具体取决于您如何使用它。
  3. 您应该以某种方式定义“好”的概念,例如回答我上面提到的问题。如果要通过网络使用对象,请使用 RMI。如果您不这样做,Netty 或 MINA 就足够了,无论您发现哪个更容易掌握。

There's no such thing as the most official networking API in J2SE, all J2SE APIs are official in the sense they are supported by Sun (now Oracle).
That said, you should choose your API based on following criteria:

  • Do you (or your team) know how to use particular API;
  • How simple/complex is this API to use;
  • What throughput are you aiming for? For performance-sensitive applications you may be forced to use binary protocol. For the rest of cases, you can use text-based protocol.

For example, between two clients simple text-based protocol will suffice for passing POJOs, for example using Apache MINA or Google protocol buffers.
This will work between client and server as well.


Response to Zac's questions in comment:

  1. Binary protocols performance gain comes from the fact you don't need to convert everything to text form and back -- you just can pass binary presentation of you application memory with minimal changes, like, in case of BSD Sockets API, converting from host byte-order to network byte-order. Unfortunately, I don't know details about how RMI/Java serialization processes objects, but I'm sure, it still much faster than passing all data in readable form;
  2. Yes, MINA and protocol buffers have Java APIs. They just not part of Java SE bundle, you have to download them separately. By the way, MINA can use both binary and readable serialization, depending on how you use it.
  3. You should define notion of 'good' somehow, for example, answering to questions I mentioned above. If you want to use objects over network, use RMI. If you don't, Netty or MINA will suffice, whatever you'll find easier to master.
诺曦 2024-10-19 23:32:14

对于 P2P,Sun 一度非常努力地推动 JXTA

我不敢使用RMI进行P2P通信。

For P2P, Sun at one point pushed JXTA pretty hard.

I wouldn't dare to use RMI for P2P communication.

黄昏下泛黄的笔记 2024-10-19 23:32:14

rmi 几乎是标准的 java 到 java 协议。它是内置的并且使用起来非常简单。大多数 j2ee 后端也使用 RMI 进行通信,尽管这不是唯一的可能性。

rmi is pretty much the standard java to java protocol. it's built in and very simple to use. most j2ee backends also communicate using rmi, although that's not the only possibility.

薄荷港 2024-10-19 23:32:14

J2SE 最常见的可能是 RMI 或原始套接字。

J2EE 使用每个人(服务器和客户端)都订阅的消息传递总线,这与 RMI 风格的解决方案有很大不同(尽管在最低级别的实现可能仍然依赖于 RMI)。它有助于自动化冗余和故障转移。如果你需要这个功能,我相信它也可以在 SE 中使用。

我已经有一段时间没有使用 J2EE 了,所以这可能已经改变了,但我对此表示怀疑。消息传递系统是J2EE 的核心组件。

J2SE the most common is probably RMI or raw sockets.

J2EE uses a messaging bus that everyone (servers and clients) subscribes to which is quite different from rmi style solutions (although at the lowest level an implementation may still rely on RMI). It helps automate redundancy and failover. If you need this functionality I believe it can be used in SE as well.

I haven't used J2EE for quite a while now, so this may have changed, but I doubt it. The messaging system was a core component of J2EE.

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