<b>Step 1: Implements the interface of Remote Server as SimpleCounterServer.java</b>
public interface SimpleCounterServer extends java.rmi.Remote
{
public int getCount() throws java.rmi.RemoteException;
}
Compile it with javac SimpleCounterServer.java
<b>Step 2: Produce the implement file SimpleCounterServerImpl.java as</b>
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
//
//
// SimpleCounterServerImpl
//
//
public class SimpleCounterServerImpl
extends UnicastRemoteObject
implements SimpleCounterServer
{
private int iCount;
public SimpleCounterServerImpl() throws java.rmi.RemoteException
{
iCount = 0;
}
public int getCount() throws RemoteException
{
return ++iCount;
}
public static void main(String args[])
{
System.setSecurityManager(new RMISecurityManager());
try
{
SimpleCounterServerImpl server = new SimpleCounterServerImpl();
System.out.println("SimpleCounterServer created");
Naming.rebind("SimpleCounterServer",server);
System.out.println("SimpleCounterServer registered");
}
catch(RemoteException x)
{
x.printStackTrace();
}
catch(Exception x)
{
x.printStackTrace();
}
}
}
Complile it with javac SimpleCounterServerImpl.java
<b>Step 3: Produce skeleton and stub file with rmic SimpleCounterServerImpl</b>
You will get two class files:
1.SimpleCounterServerImpl_Stub.class
2.SimpleCounterServerImpl_Skel.class
<b>Step 4: start rmiregistry </b>
<b>Step 5: java SimpleCounterServerImpl</b>
<b>Step 6: Implements the Client Applet to invoke the Remote method</b>
File :SimpleCounterApplet.java as
import java.applet.Applet;
import java.rmi.*;
import java.awt.*;
//
//
// SimpleCounterApplet
//
//
public class SimpleCounterApplet extends Applet
{
String message="";
String message1 = "";
public void init()
{
setBackground(Color.white);
try
{
SimpleCounterServer sever = (SimpleCounterServer)
Naming.lookup("//"+getCodeBase().getHost()+"/SimpleCounterServer");
message1 = "//"+getCodeBase().getHost()+"/SimpleCounterServer";
message = String.valueOf(sever.getCount());
}
catch(Exception x)
{
x.printStackTrace();
message = x.toString();
}
}
public void paint(Graphics g)
{
g.drawString("Number is "+message,10,10);
g.drawString("Number is "+message1,10,30);
}
}
<b>step 7 create an Html File rmi.htm : </b>
< html>
< body>
< applet code="SimpleCounterApplet.class" width="500" height="150">
< /applet>
< /body>
< /html>
评论0