Uncategorized

RMI Client server implementation

RMIServer.java

import java.rmi.Naming;
public class RMIServer{

public static void main(String args[]) throws Exception{
MyRemoteInterfaceImpl obj = new MyRemoteInterfaceImpl();
Naming.rebind(“RMIServer”,obj);
System.out.println(“Object Registered”);
}

}

RMIClient.java

import java.rmi.Naming;
import java.util.Scanner;

public class RMIClient{
public static void main(String arg[]) throws Exception{
MyRemoteInterface obj = (MyRemoteInterface)Naming.lookup(“RMIServer”);
System.out.println(“1.Farenheit to Celcius\n2.Celcius to Farenheit”);
Scanner sc = new Scanner(System.in);
int op = sc.nextInt();

System.out.println(“Enter Temperature”);
int temp= sc.nextInt();
double res =0;
switch(op){
case 1: res = obj.ToDegree(temp);
break;
case 2: res = obj.ToFarenheit(temp);
break;
}

System.out.println(“Result is “+res);
}
}

MyRemoteInterface.java

import java.rmi.Remote;
import java.rmi.RemoteException;
public interface MyRemoteInterface extends Remote{
public double ToDegree(int f) throws RemoteException;
public double ToFarenheit(int c) throws RemoteException;
}

MyRemoteInterfaceImp.java

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class MyRemoteInterfaceImpl extends UnicastRemoteObject implements MyRemoteInterface
{
protected MyRemoteInterfaceImpl() throws Exception{
super();
}
@Override
public double ToDegree(int f) throws RemoteException {
return (5/9)*(f-32);
}

@Override

public double ToFarenheit(int c) throws RemoteException{
double res= (c*(9/5))+32;
return res;
}
}

 

how to run

  1. complie all files
  2. cmd -> rmic MyRemoteInterfaceImpl
  3. new cmd -> rmiregistry
  4. new cmd -> java RMIServer
  5. new cmd -> java RMIClient

 

Leave a comment