First of All Create Interface
import java.rmi.*;public interface interfacestudent extends Remote
{
public double getPercent(String name)throws RemoteException;
public double getCount(char sex)throws RemoteException;
public double getAvg(char sex)throws RemoteException;
}
interface.java
Second Implement interface
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class implstudent extends UnicastRemoteObject implements interfacestudent
{
String name[]={"Sanjay","Chauhan","KK","Vivek","Vishal","Katrina","Anushka"};
double percent[]={99.99,88.88,77.77,66.66,55.55,44.44,33.33};
char sex[]={'m','m','m','m','m','f','f'};
public implstudent() throws RemoteException
{
super();
}
public double getPercent(String nm)throws RemoteException
{
for(int i=0;i<name.length;i++)
{
if(nm.equals(name[i]))
{
return percent[i];
}
}
return 0.0;
}
public double getCount(char sx) throws RemoteException
{
double cnt=0.0;
for(int i=0;i<sex.length;i++)
{
if(sex[i]==sx)
cnt++;
}
return cnt;
}
public double getAvg(char sx) throws RemoteException
{
double cnt=0;
double sum=0;
for(int i=0;i<sex.length;i++)
{
if(sex[i]==sx)
{
cnt++;
sum+=percent[i];
}
}
return sum/cnt;
}
}
Now Bind RMI with Server
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Server
{
public static void main(String atgs[])
{
try
{
interfacestudent ifs;
ifs=new implstudent();
Registry r;
r=LocateRegistry.createRegistry(6000);
r.bind("Stud1", ifs);
System.out.println("Server is waiting....");
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
}
}
import java.rmi.registry.Registry;
public class Server
{
public static void main(String atgs[])
{
try
{
interfacestudent ifs;
ifs=new implstudent();
Registry r;
r=LocateRegistry.createRegistry(6000);
r.bind("Stud1", ifs);
System.out.println("Server is waiting....");
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
}
}
Now implement Client
package internalassign;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
public class client
{
public static void main(String[] args) throws RemoteException, NotBoundException
{
try
{
interfacestudent ifs=(interfacestudent)
Naming.lookup("rmi://127.0.0.1:6000/Stud1");
double per=ifs.getPercent("Vivek");
System.out.println(per+"");
double cnt=ifs.getAvg('f');
System.out.println(cnt+"");
double avg=ifs.getAvg('m');
System.out.println(avg+"");
}
catch (Exception ex)
{
System.out.println(ex+"");
}
}
}
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
public class client
{
public static void main(String[] args) throws RemoteException, NotBoundException
{
try
{
interfacestudent ifs=(interfacestudent)
Naming.lookup("rmi://127.0.0.1:6000/Stud1");
double per=ifs.getPercent("Vivek");
System.out.println(per+"");
double cnt=ifs.getAvg('f');
System.out.println(cnt+"");
double avg=ifs.getAvg('m');
System.out.println(avg+"");
}
catch (Exception ex)
{
System.out.println(ex+"");
}
}
}
first of all run server.java
second run client.java
RMI Using java Example
Reviewed by Unknown
on
5:11 pm
Rating:
No comments: