0% found this document useful (0 votes)
31 views

How Communication and Process Takes Place in Rmi:: Import Java - Math.Biginteger

This document discusses Remote Method Invocation (RMI) in Java and provides steps to create a basic RMI application. RMI allows objects running in one Java virtual machine to invoke methods on objects running in another JVM. The key aspects are: 1. RMI uses stub and skeleton objects to enable communication between remote objects. The stub acts as a local representative for the remote object on the client side, while the skeleton resides on the server side. 2. To create an RMI application, interfaces and classes must be defined, along with a server class to host the remote objects and a client class to look up and invoke the remote objects. 3. The application is run by starting the R

Uploaded by

jayasreepalani02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

How Communication and Process Takes Place in Rmi:: Import Java - Math.Biginteger

This document discusses Remote Method Invocation (RMI) in Java and provides steps to create a basic RMI application. RMI allows objects running in one Java virtual machine to invoke methods on objects running in another JVM. The key aspects are: 1. RMI uses stub and skeleton objects to enable communication between remote objects. The stub acts as a local representative for the remote object on the client side, while the skeleton resides on the server side. 2. To create an RMI application, interfaces and classes must be defined, along with a server class to host the remote objects and a client class to look up and invoke the remote objects. 3. The application is run by starting the R

Uploaded by

jayasreepalani02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

RMI RMI (Remote Method Invocation) is used for distributed object references system.

A distributed object is an object which


publishes its interface on other machines. A Remote Object is a distributed object whose state is encapsulated. Stub and
Skeleton are two objects used to communicate with the remote object. Stub is a gateway for client program which is used to
communicate with skeleton object, by establishing a connection between them.eletonResides on Server program which is used
for passing the request from stub to the remote interface.

How communication and process takes place in RMI:

Steps to Run Java RMI Application in Console

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

public interface Factorial

extends java.rmi.Remote {
// Declaring the method

public BigInteger fact(int num)

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 and Implement the class

// and interface respectively

public class FactorialImpl

extends java.rmi.server.UnicastRemoteObject

implements Factorial {

// Constructor Declaration

public FactorialImpl()

throws java.rmi.RemoteException

super();

// Calculation for the problem statement

// Implementing the method fact()

// to find factorial of a number


public BigInteger fact(int num)

throws java.rmi.RemoteException

BigInteger factorial = BigInteger.ONE;

for (int i = 1; i <= num; ++i) {

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 class FactorialServer {

// Implement the constructor of the class

public FactorialServer()

{
try {

// Create a object reference for the


interface

Factorial c = new FactorialImpl();

// Bind the localhost with the


service

Naming.rebind("rmi://
localhost/FactorialService", c);

catch (Exception e) {

// If any error occur

System.out.println("ERR: " + e);

public static void main(String[] args)

// 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;

public class FactorialClient {

public static void main(String[] args)

try {

// Create an remote object with the


same name

// Cast the lookup result to the


interface

Factorial c = (Factorial);

Naming.lookup("rmi://
localhost/FactorialService");

// Call the method for the results

System.out.println(c.fact(30));

// If any error occur

catch (MalformedURLException murle) {

System.out.println("\
nMalformedURLException: "

+ murle);

catch (RemoteException re) {


System.out.println("\
nRemoteException: "

+ re);

catch (NotBoundException nbe) {

System.out.println("\
nNotBoundException: "

+ nbe);

catch (java.lang.ArithmeticException ae)


{

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

Java Socket programming is used for communication between the applications running on
different JRE.

Java Socket programming can be connection-oriented or connection-less.

Socket and ServerSocket classes are used for connection-oriented socket programming and
DatagramSocket and DatagramPacket classes are used for connection-less socket programming.

The client in socket programming must know two information:

1. IP Address of Server, and


2. Port number.

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.

2) public synchronized void closes the server socket.


close()

Example of Java Socket Programming

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.

1. ServerSocket ss=new ServerSocket(6666);


2. Socket s=ss.accept();//establishes connection and waits for the client

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.

1. Socket s=new Socket("localhost",6666);

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.

You might also like