rmi入门

1、定义远程接口

import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Upper extends Remote{
public String toUpper(String str) throws RemoteException;
}


2、实现远程接口

import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
import java.rmi.*;

public class UpperImpl extends UnicastRemoteObject implements Upper{

public UpperImpl() throws RemoteException{ super(); };

public String toUpper(String str) throws RemoteException{
return str.toUpperCase();
}

public static void main(String[] args){
System.setSecurityManager(new RMISecurityManager());
try{
Upper up=new UpperImpl();
Naming.rebind("rmi://127.0.0.1:1099/UpperServer",up);
}
catch(Exception e){
e.printStackTrace();
}

}

}


3、编写客户端

import java.rmi.*;
import java.net.*;

public class UpperApp{
public static void main(String a[]){
String res=null;
//System.setSecurityManager(new RMISecurityManager());
try{
Upper up=(Upper)Naming.lookup("rmi://127.0.0.1/UpperServer");
res=up.toUpper("fuck you !");

}catch(Exception e){
e.printStackTrace();
}
System.out.println(res);
}
}


4、编写权限文件
RMIpolicy.policy
grant
{
permission java.security.AllPermission;
};

5、生成Sub类和Skeleton类(jdk1.5后默认只生成sub)、编译相关程序
生成Sub类和Skeleton类:rmic UpperImpl

6、运行程序
(1) 启动RMI服务器:
start rmiregistry

(2)启动服务器端程序
start java -Djava.security.policy=RMIpolicy.policy host/UpperImpl

(3)运行客户机端程序:
java UpperApp
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值