How Communication and Process Takes Place in Rmi:: Import Java - Math.Biginteger
How Communication and Process Takes Place in Rmi:: Import Java - Math.Biginteger
1. Creation of classes and interfaces for the problem statement: The steps involved in this are as follows:
Create a Remote Interface which extends java.rmi.Remote: A remote interface determines the object that can be
invoked remotely by the client. This interface can be communicated with the client’s program. This Interface must
extend java.rmi.Remote Interface. Problem Statement: Create an RMI Application for finding the factorial of a number
Interface Program
import java.math.BigInteger;
// Creating an Interface
extends java.rmi.Remote {
// Declaring the method
throws java.rmi.RemoteException;
Create a class which extends java.rmi.server.UnicastRemoteObject and implements the previous interface. This
class will implement the remote interface. Do the required calculation for the problem statement.
Implementation of Interface
import java.math.BigInteger;
extends java.rmi.server.UnicastRemoteObject
implements Factorial {
// Constructor Declaration
public FactorialImpl()
throws java.rmi.RemoteException
super();
throws java.rmi.RemoteException
factorial = factorial
.multiply(
BigInteger
.valueOf(i))
;
return factorial;
Create a Server Class (with localhost and service name) For hosting a service, the server program is created whereby
using java.rmi.Naming.rebind() method can be called which takes two arguments i.e., an object reference (service name)
and instances reference.
Server Program
import java.rmi.Naming;
public FactorialServer()
{
try {
Naming.rebind("rmi://
localhost/FactorialService", c);
catch (Exception e) {
// Create an object
new FactorialServer();
Create a Client Class (with localhost and service name) Client program will invokes java.rmi.Naming.lookup() method
for RMI URL and returns an instance of object type (Factorial Interface). All RMI is done on this object
Client Program
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
try {
Factorial c = (Factorial);
Naming.lookup("rmi://
localhost/FactorialService");
System.out.println(c.fact(30));
System.out.println("\
nMalformedURLException: "
+ murle);
+ re);
System.out.println("\
nNotBoundException: "
+ nbe);
System.out.println("\
nArithmeticException: " + ae);
}
2. Compilation of all program Use javac to compile all four programs and rmic (RMI Compiler) to create a stub and skeleton
class files.
3. Running the system: After the compilation phase, the system is now ready to run. To run the system, open three console
screen (move to that path where the program resides). One for the client, one for server and one for the RMI Registry.
Start with a registry, use rmiregistry, if there is no error registry will start running and now move to second screen.
In the second console run the server program and host the FactorialService. It will start and wait for the client connection
and it will load the implementation into memory.
In the third console, run the client program.
In this way RMI can be run in three console for localhost. RMI uses Network stack and TCP/IP Stack for communication of three
different JVM’s.
Java Socket programming is used for communication between the applications running on
different JRE.
Socket and ServerSocket classes are used for connection-oriented socket programming and
DatagramSocket and DatagramPacket classes are used for connection-less socket programming.
Here, we are going to make one-way client and server communication. In this application, client
sends a message to the server, server reads the message and prints it. Here, two classes are being
used: Socket and ServerSocket. The Socket class is used to communicate client and server.
Through this class, we can read and write message. The ServerSocket class is used at server-side.
The accept() method of ServerSocket class blocks the console until the client is connected. After
the successful connection of client, it returns the instance of Socket at server-side.
Socket class
A socket is simply an endpoint for communications between the machines. The Socket class can
be used to create a socket.
Important methods
Method Description
1) public InputStream getInputStream() returns the InputStream attached with this socket.
2) public OutputStream getOutputStream() returns the OutputStream attached with this socket.
3) public synchronized void close() closes this socket
ServerSocket class
The ServerSocket class can be used to create a server socket. This object is used to establish
communication with the clients.
Important methods
Method Description
1) public Socket accept() returns the socket and establish a connection between server and
client.
Creating Server:
To create the server application, we need to create the instance of ServerSocket class. Here, we
are using 6666 port number for the communication between the client and server. You may also
choose any other port number. The accept() method waits for the client. If clients connects with
the given port number, it returns an instance of Socket.
Creating Client:
ADVERTISEMENT
To create the client application, we need to create the instance of Socket class. Here, we need to
pass the IP address or hostname of the Server and a port number. Here, we are using "localhost"
because our server is running on same system.
Let's see a simple of Java socket programming where client sends a text and server receives and
prints it.
File: MyServer.java
1. import java.io.*;
2. import java.net.*;
3. public class MyServer {
4. public static void main(String[] args){
5. try{
6. ServerSocket ss=new ServerSocket(6666);
7. Socket s=ss.accept();//establishes connection
8. DataInputStream dis=new DataInputStream(s.getInputStream());
9. String str=(String)dis.readUTF();
10. System.out.println("message= "+str);
11. ss.close();
12. }catch(Exception e){System.out.println(e);}
13. }
14. }
File: MyClient.java
1. import java.io.*;
2. import java.net.*;
3. public class MyClient {
4. public static void main(String[] args) {
5. try{
6. Socket s=new Socket("localhost",6666);
7. DataOutputStream dout=new DataOutputStream(s.getOutputStream());
8. dout.writeUTF("Hello Server");
9. dout.flush();
10. dout.close();
11. s.close();
12. }catch(Exception e){System.out.println(e);}
13. }
14. }
download this example
To execute this program open two command prompts and execute each program at each
command prompt as displayed in the below figure.
After running the client application, a message will be displayed on the server console.
Example of Java Socket Programming (Read-Write both side)
In this example, client will write first to the server then server will receive and print the text. Then server will write to the client and client will
receive and print the text. The step goes on.