第一个Android程序RMI

特别注意:要运行这个Android的RMI程序,要把类放在跟目录下

  1. 接口
import java.rmi.Remote;
import java.rmi.RemoteException;
/**
 * 注意:这个接口继承自Remote,每一个定义的方法都必须抛出一个RemoteException异常对象
 */
public interface Calculator extends Remote {
    public long add(long a, long b) throws RemoteException;
    public long sub(long a, long b) throws RemoteException;
    public long mul(long a, long b) throws RemoteException;
    public long div(long a, long b) throws RemoteException;
}
[root@localhost]#javac Calculator.java
  1. 接口的具体实现
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

/**
 * 注意:这个类使用了UnicastRemoteObject 去连接RMI系统
 */
public class CalculatorImpl extends UnicastRemoteObject implements Calculator {
    public CalculatorImpl() throws RemoteException { super(); }
    public long add(long a, long b) throws RemoteException { return a + b; }
    public long sub(long a, long b) throws RemoteException { return a - b; }
    public long mul(long a, long b) throws RemoteException { return a * b; }
    public long div(long a, long b) throws RemoteException { return a / b; }
}
[root@localhost]# javac CalculatorImpl
  1. 桩(Stubs)和框架(Skeletons)
[root@localhost]# rmic CalculatorImpl
  1. 主机服务器
import java.rmi.Naming;
import java.rmi.RemoteException;

public class CalculatorServer {
    public CalculatorServer(){
        try {
            Calculator c = new CalculatorImpl();
            Naming.rebind("rmi://192.168.0.183:1099/CalculatorService", c);//1099是默认的端口号CalculatorService是随便取得 
        } catch (Exception ex) {
            System.out.println("Trouble:"+ex); 
        }
    }

    public static void main(String[]args){
        new CalculatorServer();
    }
}
[root@localhost]# javac CalculatorServer
  1. 客户端
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

public class CalculatorClient {
    public static void main(String[] args) {
        try {
            Calculator c = (Calculator)Naming.lookup("rmi://192.168.0.183/CalculatorService");//CalculatorService要跟服务端的一样 
            System.out.println(c.sub(4, 3)); 
            System.out.println(c.add(4, 5)); 
            System.out.println(c.mul(3, 6)); 
            System.out.println(c.div(9, 3)); 
        } catch (NotBoundException ex) {
            System.out.println();
            System.out.println("NotBoundException");
            System.out.println(ex);
        } catch (MalformedURLException ex) {
            System.out.println(); 
            System.out.println("MalformedURLException"); 
            System.out.println(ex); 
        } catch (RemoteException ex) { 
            System.out.println(); 
            System.out.println("RemoteException");
            System.out.println(ex);
        }
    } 
}
[root@localhost]#javac CalculatorClient
  1. 运行RMI系统 我们是在命令控制台下运行这个系统的,你必须启动三个控制台窗口,一个运行服务器,一个运行客户端,还有一个运行RMIRegistry;
    有一个运行 RMIRegistry.(首先运行注册程序RMIRegistry。你必须在那个运行的目录下注册程序)

a.rmiregistry
b.java CalculatorServer
c.java CalculatorClient


» 本文链接:https://blog.apires.cn/archives/221.html
» 转载请注明来源:Java地带  » 《第一个Android程序RMI》

» 本文章为Java地带整理创作,欢迎转载!转载请注明本文地址,谢谢!
» 部分内容收集整理自网络,如有侵权请联系我删除!

» 订阅本站:https://blog.apires.cn/feed/

标签: Java, Android, RMI程序

添加新评论