资源描述
计算机科学与技术学院实验报告
实验题目:验证任意一个大于2的偶数都能分解成两个素数之和。
学号:
日期:2014-10-23
班级:电子商务2014级
姓名:
Email:
实验目的:
服务器端的对象负责对是否是素数进行判断。
客户端负责验证一定范围内的猜想。
服务器端可以定时查询客户端验证的进度。
硬件环境:机房电脑
软件环境:JDK6,Eclipse,RMI插件包
实验步骤:
1.Prime.java
import java.rmi.*;
// RMI 本地接口必须从Remote 接口派生
public interface Prime extends Remote {
// 接口中的具体方法声明,注意必须声明抛出RemoteException
boolean isPrime(int n) throws RemoteException;
void setStart(int n) throws RemoteException;
void setEnd(int n) throws RemoteException;
void setNow(int n) throws RemoteException;
}
2.PrimeClient.java
import java.rmi.*;
import java.util.Arrays;
import javax.rmi.PortableRemoteObject;
public class PrimeImpl extends PortableRemoteObject implements Prime {
private int start = 0, end = 0, now = -1;
/*
* 构造函数
*/
public PrimeImpl() throws RemoteException {
super();
}
public boolean isPrime(int n) throws RemoteException {
int i,c;
i=n;
boolean b=true;
for(c=2;c<=Math.sqrt(i);c++){
if(i%c==0){
b=false;
break;
}
}
return b;
}
public void setStart(int n) throws RemoteException {
start = n;
}
public void setEnd(int n) throws RemoteException {
end = n;
}
public void setNow(int n) throws RemoteException {
now = n;
}
public int getProcess() {
if(now == -1 || end == start) return -1;
return (now - start) * 100 / (end - start);
}
}
3.PrimeImpl.java
import java.rmi.*;
import java.util.Arrays;
import javax.rmi.PortableRemoteObject;
public class PrimeImpl extends PortableRemoteObject implements Prime {
private int start = 0, end = 0, now = -1;
/*
* 构造函数
*/
public PrimeImpl() throws RemoteException {
super();
}
public boolean isPrime(int n) throws RemoteException {
int i,c;
i=n;
boolean b=true;
for(c=2;c<=Math.sqrt(i);c++){
if(i%c==0){
b=false;
break;
}
}
return b;
}
public void setStart(int n) throws RemoteException {
start = n;
}
public void setEnd(int n) throws RemoteException {
end = n;
}
public void setNow(int n) throws RemoteException {
now = n;
}
public int getProcess() {
if(now == -1 || end == start) return -1;
return (now - start) * 100 / (end - start);
}
}
4.PrimeServer.java
import java.rmi.*;
import java.util.Timer;
import java.util.TimerTask;
public class PrimeServer {
public static void main(String[] args) {
new PrimeServer().main2(args);
}
public void main2(String[] args) {
// 在服务器端设置安全机制
/*
* if (System.getSecurityManager() == null) {
* System.setSecurityManager(new RMISecurityManager()); }
*/
try {
System.out.println("开始RMI Server ...");
/*
* 创建远程对象的实现实例
*/
PrimeImpl hImpl = new PrimeImpl();
System.out.println("将实例注册到专有的URL ");
Naming.rebind("PrimeServer", hImpl);
System.out.println("等待RMI 客户端调用...");
System.out.println("");
Timer t = new Timer();
t.schedule(new PrimeServer.getProcess(hImpl), 0, 100);
} catch (Exception e) {
System.out.println("错误: " + e);
}
}
private class getProcess extends TimerTask {
private PrimeImpl hImpl;
public getProcess(PrimeImpl o) {
hImpl = o;
}
public void run() {
int p = hImpl.getProcess();
if(p >= 0)
System.out.println("当前查询进度:" + p + "%");
}
}
}
实验结果截图:
结论分析与体会:
总体来讲实现了要求,
对RMI的机制有了进一步的了解。
展开阅读全文