0% found this document useful (0 votes)
14 views10 pages

Practical 2

The document outlines a practical assignment for Java RMI programming, detailing the implementation of two services: a 'Hello World' service and a 'Calculator' service. It includes step-by-step instructions for creating server and client files, compiling them, generating stubs, and running the services. The assignment is submitted by Gaurav Akabari from the Department of Information Technology for the academic year 2024-2025.

Uploaded by

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

Practical 2

The document outlines a practical assignment for Java RMI programming, detailing the implementation of two services: a 'Hello World' service and a 'Calculator' service. It includes step-by-step instructions for creating server and client files, compiling them, generating stubs, and running the services. The assignment is submitted by Gaurav Akabari from the Department of Information Technology for the academic year 2024-2025.

Uploaded by

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

[ 2CEIT603: CLOUD COMPUTING]

AY 2024-2025

Practical: 2

AIM- Java RMI Programming

Submitted by: Gaurav Akabari


Enrollment number: 22012021001

Department of Information Technology


(1) Implementation of “Hello World” Service using Java RMI.

Code-
Step 1: Create an Interface File to define the Client and Server Object.
File Name: HelloServer.java

import java.rmi.*;
public interface HelloServer extends Remote
{
public String greeting() throws RemoteException;
}

Step 2 : Now we Have to Create a remote Server file by extending the Unicast Remote Object of
Java RMI.

File Name : HelloServerImpl.java

import java.util.Calendar;
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;

public class HelloServerImpl extends UnicastRemoteObject implements HelloServer {

// Constructor
HelloServerImpl() throws RemoteException {
super();
}

// Implementation of the remote method


public String greeting() throws RemoteException {
Calendar c = Calendar.getInstance();
int timeOfDay = c.get(Calendar.HOUR_OF_DAY);

if (timeOfDay < 12) {


return "Good morning";
} else if (timeOfDay < 16) {
return "Good afternoon";
} else if (timeOfDay < 21) {
return "Good evening";
} else {
return "Good night";
}
}
// Main method inside the class
public static void main(String args[]) {
try {
HelloServerImpl server = new HelloServerImpl();

// Bind the server to RMI Registry


Naming.rebind("Hello-SERVER", server);

System.out.println("Server waiting.....");
} catch (java.net.MalformedURLException me) {
System.out.println("Malformed URL: " + me.toString());
} catch (RemoteException re) {
System.out.println("Remote exception: " + re.toString());
}
}
}

Step 3: After Creating the Server file now we have to create remote Client file to make remote
communication with the server.
File Name : HelloClient.java

import java.rmi.*;
import java.rmi.server.*;

public class HelloClient {


public static void main(String[] args) {

try {
System.out.println("Security Manager loaded");
String url = "//localhost/Hello-SERVER";
HelloServer remoteObject = (HelloServer) Naming.lookup(url);
System.out.println("Got remote object");
System.out.println("Hello," + remoteObject.greeting());
} catch (RemoteException exc) {
System.out.println("Error in lookup: " + exc.toString());
} catch (java.net.MalformedURLException exc) {
System.out.println("Malformed URL: " + exc.toString());
} catch (java.rmi.NotBoundException exc) {
System.out.println("NotBound: " + exc.toString());
}
}
}

Step 4 We have Created the all required file now we have to Compile this all files to get .class
file.
 Open cmd in the particular folder where this al files are located.
 After opening cmd write the following command:
 Command : javac *.Java (To compile all the file at same time )
 Or javac filename.java (To compile file one by one use the name of file)

Step 5 When the Compilation is successful it will generate total 3 class files named as bellow:
 HelloClient.class
 HelloServer.class
 HelloServerImpl.class

Step 6 Now we have to generate the stub to work as a middleware translator between client and
server.
 For Creating the stub write the following command in the cmd.
 Command : rmic HelloServerImpl
 This will create a HelloServerImpl_Stub.class file in particular folder.

Step 6: After this start the rmiregistry to register the stub using following command.
 Command : start rmiregistry
 This will start the rmi registry.

Step 7: Now run the Server File using following command


java HelloServerImpl

Step 8: Open a New Terminal from the same folder and run the client file using the following
Command:
 Command : java HelloClient
 Here we are using command line input so write java HelloClient parameter
Output:
(2) Implementation of “Calculator” Service (Add, Sub, Mul, Div)
using Java RMI

Step 1: Create an Interface File to define the Client and Server Object.
File Name: CalcServer.java
import java.rmi.*;
public interface CalcServer extends Remote
{
public int sum(int a,int b) throws RemoteException;
public double div(int a,int b) throws RemoteException;
public double mod(int a,int b) throws RemoteException;
public int sub(int a,int b) throws RemoteException;
public double mul(int a,int b) throws RemoteException;
}

Step 2 : Now we Have to Create a remote Server file by extending the Unicast Remote Object of
Java RMI.
File Name : CalcServerImpl.java

import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
public class CalcServerImpl extends UnicastRemoteObject
implements CalcServer
{
CalcServerImpl() throws RemoteException
{
super();
}
public static void main(String args[])
{
try
{
CalcServerImpl Server = new CalcServerImpl();
Naming.rebind("//localhost/SAMPLE-SERVER",
Server); System.out.println("Server waiting. ");
}
catch (java.net.MalformedURLException me) {
System.out.println("Malformed URL: " + me.toString()); }
catch (RemoteException re) {
System.out.println("Remote exception: " + re.toString()); }
}

// Function Which Provides the Appropriate Output


public int sum(int a,int b) throws RemoteException
{
return a + b;
};
public double div(int a,int b) throws RemoteException
{
return a / b;
};
public double mod(int a,int b) throws RemoteException
{
return a % b;
};
public int sub(int a,int b) throws RemoteException
{
return a - b;
};
public double mul(int a,int b) throws RemoteException
{
return a * b;
};
}

Step 3: After Creating the Server file now we have to create remote Client file to make remote
communication with the server.
File Name : CalcClient.java
import java.rmi.*;
22012021001 6CEIT-C1 Gaurav Akabari
-------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------

import java.rmi.server.*; public


class CalcClient
{
public static void main(String[] args)
{
try
{
String url = "//localhost/SAMPLE-SERVER";
CalcServer remoteObject = (CalcServer)Naming.lookup(url);
System.out.println("Got remote object"); System.out.println("Addition:
" + remoteObject.sum(50,15) ); System.out.println("Substraction: " +
remoteObject.sub(50,15) ); System.out.println("Multiplication: " +
remoteObject.mul(50,15) ); System.out.println("Division: " +
remoteObject.div(50,15) ); System.out.println("Modulo: " +
remoteObject.mod(50,15) );
}
catch (RemoteException exc) {
System.out.println("Error in lookup: " + exc.toString()); }
catch (java.net.MalformedURLException exc) {
System.out.println("Malformed URL: " + exc.toString()); }
catch (java.rmi.NotBoundException exc) {
System.out.println("NotBound: " + exc.toString());
}
}
}

Step 4 We have Created the all required file now we have to Compile this all files to get .class file.
 Open cmd in the particular folder where this al files are located.
 After opening cmd write the following command:
 Command : javac *.Java (To compile all the file at same time )
 Or javac filename.java (To compile file one by one use the name of file)

Step 5 When the Compilation is successful it will generate total 3 class files named as bellow:
 CalcClient.class
 CalcServer.class

Page 2 of 10
22012021001 6CEIT-C1 Gaurav Akabari
-------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------

 CalcServerImpl.class

Step 6 Now we have to generate the stub to work as a middleware translator between client and server.
 For Creating the stub write the following command in the cmd.
 Command : rmic CalcServerImpl
 This will create a CalcServerImpl_Stub.class file in particular folder.

Step 7: After this start the rmiregistry to register the stub using following command.

 Command : start rmiregistry


 This will start the rmi registry.

Step 8: Now run the Server File using following command:


Command : java CalcServerImpl

Step 9: Open a New Terminal from the same folder and run the client file using the following
Command:
 Command : java CalcClient
Page 3 of 10
22012021001 6CEIT-C1 Gaurav Akabari
-------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------

 Here we are using command line input so write java CalcClient parameter
 Output:

Page 4 of 10

You might also like