首页 > 学院 > 开发设计 > 正文

Java RMI编程

2019-11-18 11:01:23
字体:
来源:转载
供稿:网友

<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();
        }        

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表