You are on page 1of 4

Simple RMI Application

HelloImpl.java
importjava.rmi.*;
publicinterfaceHelloImplextendsRemote
{
StringsayHello(Stringa)throwsRemoteException;
}

RMIHelloServer.java
importjava.rmi.*;
importjava.rmi.registry.*;
importjava.rmi.server.*;
publicclassRMIHelloServerextendsUnicastRemoteObjectimplementsHelloImpl
{
publicRMIHelloServer()throwsRemoteException
{
System.out.println("Theserverisinstantiated");
}
publicStringsayHello(Stringa)
{
//Returnsthesquarerootofthenumberpassedasargument
Strings;
s="Hello,"+a;
returns;
}
publicstaticvoidmain(String[]arguments)
{
try
{
//InstantiatestheRMIServerobject
RMIHelloServerserver=newRMIHelloServer();
/*Therebind()methodtakestwoarguments,URLnameandthe
Remoteobjectwhichbindstheremoteobjecttotheremote
registry*/
Naming.rebind("call1",server);
/*Thenamecall1isabindnamegiventotheremoteobject,
whichgetsregisteredwiththeRMIregistrythroughwhich
remotecallsaremade*/
}
catch(Exceptionexce)
{
System.out.println("Error"+exce.toString());
exce.printStackTrace();
}
}//Endofmain()method
}//EndofRMIServerclass

RMIHelloClient.java
importjava.rmi.*;
importjava.rmi.registry.*;
publicclassRMIHelloClient
{
publicstaticvoidmain(String[]arguments)
{
try
{
HelloImplmthdIp=
(HelloImpl)Naming.lookup("rmi://127.0.0.1/call1");

/*'127.0.0.1'istheIPaddressoftheServermachine
andcall1isthenamewithwhichtheServerObjectis
registered.*/
Strings=mthdIp.sayHello(arguments[0]);
System.out.println(s);
}
catch(Exceptionexec)
{
System.out.println("Error"+exec.toString());
exec.printStackTrace();
}
}//Endofmain()method
}//EndofRMIClientclass

Complete following steps:


1. Go to your working directory (F:\RMI\Hello)
2. Write all Java programs. (Interface, server, client)
3. Compile all java programs
4. Once this has been completed, the stubs and skeletons for the Remote interfaces should be
compiled
by using the rmic stub compiler. The stub and skeleton of the example Remote interface are
compiled with the command:
rmicRMIHelloServer

5. Open other command window. Go to directory where server files are kept and run server
javaRMIHelloServer

6. Open other command window. Go to directory where client files are kept and run client
javaRMIHelloClientGirish

Screen shots are shown below:


8.2 Simple RMI Calculator Application
CalcImpl.java
importjava.rmi.*;
publicinterfaceCalcImplextendsRemote
{
doubleadd(inta,intb)throwsRemoteException;
doublesub(inta,intb)throwsRemoteException;
doublemul(inta,intb)throwsRemoteException;
doublediv(inta,intb)throwsRemoteException;
}

RMICalcServer.java
importjava.rmi.*;
importjava.rmi.registry.*;
importjava.rmi.server.*;
publicclassRMICalcServerextendsUnicastRemoteObjectimplementsCalcImpl
{
publicRMICalcServer()throwsRemoteException
{
System.out.println("Theserverisinstantiated");
}
publicdoubleadd(inta,intb)
{
returna+b;
}
publicdoublesub(inta,intb)
{

returnab;
}
publicdoublemul(inta,intb)
{
returna*b;
}
publicdoublediv(inta,intb)
{
return(double)a/b;
}
publicstaticvoidmain(String[]arguments)
{
try
{
//InstantiatestheRMIServerobject
RMICalcServerserver=newRMICalcServer();
/*Therebind()methodtakestwoarguments,URLnameandthe
Remoteobjectwhichbindstheremoteobjecttotheremote
registry*/
Naming.rebind("call12",server);
/*Thenamecall1isabindnamegiventotheremoteobject,
whichgetsregisteredwiththeRMIregistrythroughwhich
remotecallsaremade*/
}
catch(Exceptionexce)
{
System.out.println("Error"+exce.toString());
exce.printStackTrace();
}
}//Endofmain()method
}//EndofRMIServerclass

RMICalcClient.java
importjava.rmi.*;
importjava.rmi.registry.*;
publicclassRMICalcClient
{
publicstaticvoidmain(String[]arguments)
{
try
{
CalcImplmthdIp=
(CalcImpl)Naming.lookup("rmi://localhost/call12");
inta=Integer.parseInt(arguments[0]);
intb=Integer.parseInt(arguments[1]);
doubleans;
ans=mthdIp.add(a,b);
System.out.println(a+"+"+b+"="+ans);
ans=mthdIp.sub(a,b);
System.out.println(a+""+b+"="+ans);
ans=mthdIp.mul(a,b);
System.out.println(a+"*"+b+"="+ans);
ans=mthdIp.div(a,b);
System.out.println(a+"/"+b+"="+ans);
}
catch(Exceptionexec)
{

System.out.println("Error"+exec.toString());
exec.printStackTrace();
}
}//Endofmain()method
}//EndofRMIClientclass

You might also like