Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java standard for client/server communication

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.

like image 960
Zac Avatar asked Jan 19 '11 15:01

Zac


2 Answers

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.

like image 65
Sanjay T. Sharma Avatar answered Oct 07 '22 01:10

Sanjay T. Sharma


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.
like image 25
Victor Sorokin Avatar answered Oct 07 '22 00:10

Victor Sorokin