public class commImp extends UnicastRemoteObject implements comm
{
public commImp(String strInfo) throws RemoteException
{
Info = strInfo;
}
public String showInfo() throws RemoteException
{
return "Message from commImp : "+Info;
}
private String Info;
}
-------------------------------------------------------------------------
Program 3: Server program commServer.java
-----------------------------------------------------------------------------
import java.rmi.*;
import java.rmi.server.*;
public class commServer
{
public static void main(String[] args)
{
try
{
System.out.print("Constructing Server Implementation...");
commImp cimp1 = new commImp("This is Developed By Raja");
commImp cimp2 = new commImp("And it is an RMI Example.");
System.out.print("Done!nBinding Server Implementation...");
Naming.rebind("author",cimp1);
Naming.rebind("about",cimp2);
System.out.print("Done!nWaiting for Invocations from client...n");
}
catch(Exception e)
{ System.out.print("nError Generated in commServer : "+e.toString()+"n");
}
}
}
-----------------------------------------------------------------------------
Program 4: Client program commClient.java
---------------------------------------------------------------------------
import java.rmi.*;
import java.rmi.server.*;
import sun.applet.*;
public class commClient
{
public static void main(String[] args)
{
System.setSecurityManager(new RMISecurityManager());
try
{
System.out.print("Naming.lookup for Bindings...");
comm com1 = (comm) Naming.lookup("author");
comm com2 = (comm) Naming.lookup("about");
System.out.print("Done!n");
System.out.print("Info from author object: "+com1.showInfo()+"n");
System.out.print("Info from about object: "+com2.showInfo()+"n");
}
catch(Exception e)
{ System.out.print("Error Generated in commServer : "+e.toString()+"n");
}
System.exit(0);
}
}
--------------------------------------------------------------------------
Program 5: Show bings class showBindings.java
-------------------------------------------------------------------------
import java.rmi.*;
import java.rmi.server.*;
public class showBindings
{
public static void main(String[] args)
{
try
{
String[] bindings = Naming.list("");
for(int i=0;i<bindings.length;i++)
System.out.println("Binding ["+(i+1)+"] => "+bindings[i]);
}
catch(Exception e)
{ System.out.println("Error in showBindings : "+e.toString()); }
}
}
------------------------------------------------------------------------
Program 6: client.policy
----------------------------------------------------------
grant
{
permission java.net.SocketPermission
"*:1024-65335","connect";
};
-----------------------------------------------------------------------
How to run the program
________________________________________
1. compile all java files
javac *.java
2. use RMICompiler to create stubs & skeletons of Implementations
rmic commImp
3. run rmiregistry
start rmiregistry
4. create policy file with following contents
client.policy
~~~~~~~~~~~~~
grant
{
permission java.net.SocketPermission
"*:1024-65335","connect";
};
5. run server
java commServer
6. run client with policy created in step 4.
java -Djava.security.policy=client.policy commClient