Are there any guidelines as to when I should use RMI and when I should prefer plain old socket connections? What are the advantages/disadvantages of each of these technologies ?

Govind Seshadri

Prgramming sockets in Java is to apply a procedural networking idiom to an object-oriented computing environment. Sockets-based network programming can be a fairly laborious task. Here, all client/server communication must be mediated by a user-defined application protocol, which serves to define the precise byte-oriented sequence of the message. A client must not only be responsible for creating the message, but also must bear the logic to decipher the response from the server. And likewise for the server.

RMI elevates network programming to a much higher plane. With RMI, Java objects can easily invoke the methods of remote objects as if they were locally available. The amazing part is that the remote objects may be under the jurisdiction of an entirely different JVM, running on a different host halfway around the world! Also, since you interact with an RMI object much like a local object, remote methods can send and receive just about any valid Java object without having to worry about flattening it out to a serial data stream. RMI automatically provides you that feature by utilizing the underlying Object Serialization mechanism. The communication between the RMI client and server itself is facilitated by the Java Remote Method Protocol (JRMP) or Internet Inter-Orb Protocol (IIOP).

RMI is especially useful for deployment as a multi-tier bridging mechanism, and can serve as an effective "glue" for integrating other enterprise Java technologies like JDBC and JNI. RMI remote objects serving as wrappers to database and legacy system integration code are also highly scalable due to the inherently distributed nature of the technology itself.

There is no doubt that programming low-level sockets enables your client-server applications to be much more efficient in their conversation. They also do not suffer from the latency issues due to object serialization and are easily integrated through corporate firewalls. But this comes at a cost of your programs being much more difficult to develop and maintain, compared with RMI applications. Unlike RMI, you will also have to take care of multithreading issues within your server.