Difference between RMI and Socket
This article describes techniques for Client and Server setup where a Client connects, sends messages to the server and the server shows them using a connection and vice versa. RMI and Sockets both are used to establish a connection between client and server but there are some major differences between RMI and Socket, let’s look into it.
but before that let’s understand what is RMI and what is Socket and their components.
Remote Method Invocation
Remote Method Invocation is basically an API which allows an object to invoke the method on an object running in a different machine’s JVM(server-side). In RMI we generally create two programs, client-side and server-side. both sides have their entry gateways i.e. Stubs and Skeletons. RMI is for high-level java to java distributed computing. In short, RMI creates a public remote server object that enables client and server-side communications through simple method calls on the server object. One bigger problem of RMI is, that it is only available in Java thus client and server need to be implemented in Java.
Elements of RMI:
- Stubs: Stubs are the gateway for the client-side, build an information block and send this information to the server-side. The information block may consist of the method name which is to be invoked, parameters to the remote JVM and identifier of the remote object to be used.
- Skeleton: Similar to stubs, skeletons are the gateway for the server-side, it sends the request from the stub object to the remote object.
- RRL: Remote reference layer is used to manage the references made by the client to the remote object.
- Transport layer: It helps to connect the client and the server and also manages the existing connection between them.
There are basically six steps to implement RMI connection are as follows:
- Defining a remote interface
- Implementing the remote interface
- Creating Stub and Skeleton objects from the implementation class using rmic (RMI compiler)
- Start the rmiregistry
- Create and execute the server application program
- Create and execute the client application program.
Socket programming is also a technique which is used to establish a connection between the client and server-side with the help of sockets. Sockets are nothing but two-sided communication links between two programs in a network. To connect to another machine we need a socket connection. It means the two machines have information about each other’s network location and TCP port. Sockets are like gateways which provide access points for programs through some specific port numbers. The socket connection may be TCP or UDP. Socket-based Communication is independent of programming languages.
There are majorly four types of socket available:
- Stream Socket: Stream socket uses TCP for data transmission.
- Datagram Socket: Datagram socket uses UDP for data transmission.
- Raw Socket: A raw socket is used to receive raw packets of data.
- Sequenced Packet Socket: These are very similar to stream sockets, the TCP stream socket does not maintain socket boundaries. but, this socket connection maintains the message boundaries similar to a UDP socket connection.
Differences between RMI and Socket:
|1.||Remote Method Invocation is basically an API which allows an object to invoke a method on an object running in a different machine’s JVM.||Sockets are nothing but two-sided communication links between two programs (client and server) in a network.|
|2.||RMI is remote method invocation which means methods are invoked remotely or accessing remote sites in client-server communication.||Sockets are like gateways which provide access points for programs through some specific port numbers.|
|3.||RMI is built on top of sockets. without sockets, RMI wouldn’t exist.||In this, we have to manage which sockets and protocols the application will use. Even though we can format messages travelling between client and server-side.|
|4.||RMI is object-oriented||Whereas it is not.|
|5.||RMI handles the formatting of messages between client and server.||Here we specify TCP or UDP type, we have to handle all the formatting of messages travelling between client and server.|
|6.||RMI is a Java-specific technology.||Socket-based Communication is independent of programming languages.|
|7.||RMI is for high-level java to java distributed computing.||Sockets are for low-level network communication.|