0% found this document useful (0 votes)
22 views11 pages

Assignment 4

Uploaded by

Karan Margaje
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)
22 views11 pages

Assignment 4

Uploaded by

Karan Margaje
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/ 11

SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

4. Write a program to implement RPC that performs the following two tasks

a) Perform Basic Mathematical Operation

Code:

Servinterface.java

package calculator;

import java.rmi.*;

/**

* @author Karan

*/

public interface servinterface extends Remote

public double add(double a,double b) throws RemoteException;

public double sub(double a,double b) throws RemoteException;

public double mul(double a,double b) throws RemoteException;

public double div(double a,double b) throws RemoteException;

public double mod(double a,double b) throws RemoteException;

Servimpl.java

package calculator;

import java.rmi.*;

import java.rmi.server.UnicastRemoteObject;

/**

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

* @author Karan

*/

public class servimpl extends UnicastRemoteObject implements servinterface

public servimpl() throws RemoteException

super();

@Override

public double add(double a, double b) throws RemoteException {

return (a+b);

@Override

public double sub(double a, double b) throws RemoteException {

return (a-b);

@Override

public double mul(double a, double b) throws RemoteException {

return (a*b);

@Override

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

public double div(double a, double b) throws RemoteException {

return (a/b);

@Override

public double mod(double a, double b) throws RemoteException {

return (a%b);

Server.java

package calculator;

import java.rmi.*;

import java.rmi.registry.LocateRegistry;

import java.rmi.registry.Registry;

/**

* @author Karan

*/

public class server extends servimpl{

public server() throws RemoteException

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

public static void main(String args[]) throws RemoteException{

try

Registry reg = LocateRegistry.createRegistry(9999);

servimpl s = new servimpl();

reg.rebind("Server",s);

System.out.println("Server is ready!");

catch(Exception e)

System.out.println(e);

Client.java

package calculator;

import java.io.*;

import java.util.*;

import java.rmi.*;

import java.rmi.registry.Registry;

import java.rmi.registry.LocateRegistry;

/**

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

* @author Karan

*/

public class client {

public static void main(String args[]) throws RemoteException{

String choice;

Scanner scan =new Scanner(System.in);

try

System.out.println("Enter two number: ");

int a = scan.nextInt();

int b = scan.nextInt();

System.out.println("Enter operation: ");

choice = scan.next();

Registry reg=LocateRegistry.getRegistry("127.0.0.1",9999);

servinterface i = (servinterface)reg.lookup("Server");

System.out.println("First number: "+a);

System.out.println("Second number: "+b);

switch(choice)

case "+":

System.out.println("Add:\t"+i.add(a,b));

break;

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

case "-":

System.out.println("Sub:\t"+i.sub(a,b));

break;

case "*":

System.out.println("Mul:\t"+i.mul(a,b));

break;

case "/":

System.out.println("Div:\t"+i.div(a,b));

break;

case "%":

System.out.println("Mod:\t"+i.mod(a,b));

break;

catch(Exception e)

System.out.println(e);

Output:

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

b) Retrieve Date and Time from Server

Code:

Idt.java

package Date_Time;

import java.rmi.*;

/**

* @author Karan

*/

public interface idt extends Remote

public String date() throws RemoteException;

public String time() throws RemoteException;

Dtremote.java

package Date_Time;

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

import java.rmi.*;

import java.rmi.server.*;

import java.util.Date;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

/**

* @author Karan

*/

public class dtremote extends UnicastRemoteObject implements idt

DateFormat df;

dtremote() throws RemoteException

super();

@Override

public String date() throws RemoteException {

df = new SimpleDateFormat("dd/MM/yyyy");

return(df.format(new Date()));

@Override

public String time() throws RemoteException {

df = new SimpleDateFormat("HH:mm:ss");

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

return(df.format(new Date()));

Server.java

package Date_Time;

import java.rmi.Naming;

import java.rmi.registry.LocateRegistry;

import java.rmi.registry.Registry;

/**

* @author Karan

*/

public class server {

public static void main(String ar[]){

try

Registry reg = LocateRegistry.createRegistry(9999);

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

dtremote d =new dtremote();

reg.rebind("Server",d);

System.out.println("Server is ready!");

catch (Exception e)

e.printStackTrace();

Client.java

package Date_Time;

import java.rmi.*;

import java.rmi.registry.LocateRegistry;

import java.rmi.registry.Registry;

/**

* @author Karan

*/

public class client {

public static void main(String ar[]){

try

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

Registry reg=LocateRegistry.getRegistry("127.0.0.1",9999);

idt stub = (idt)reg.lookup("Server");

System.out.println("Current Date : "+stub.date());

System.out.println("Current Time : "+stub.time());

catch(Exception e)

System.out.println(e);

Output:

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020

You might also like