0
내 rmi 프로그램의 문제점은 echo 메시지를 보낼 때 null 값을 얻지 만 모든 값을 초기화한다는 것입니다. 그것은 그것을 위해 넣은 값을 등록하지 않는 것 같습니다.값을 등록하는 RMI
서버
package server;
import interfaces.Compute;
import interfaces.Pi;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
public class ComputeEngine extends UnicastRemoteObject implements Compute {
protected ComputeEngine() throws RemoteException {
super();
}
public String executeTask(Pi t){
System.out.println(t.message);
return t.execute();
}
public String executeTask2(Pi t) {
return t.execute2();
}
public static void main(String[] args) {
try {
LocateRegistry.createRegistry(1099);
String name = "Echo";
Compute engine = new ComputeEngine();
Naming.rebind(name, engine);
name = "Compute";
Naming.bind(name, engine);
System.out.println("System bound");
} catch (Exception e) {
System.err.println("ComputeEngine exception:");
e.printStackTrace();
}
}
}
클라이언트
package client;
import interfaces.Compute;
import interfaces.Pi;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Scanner;
public class Client {
Scanner scan = new Scanner(System.in);
int v1 = 0, v2 = 0;
String echoMessage = null;
public static void main(String args[]) {
Client e = new Client();
String name, output;
Registry registry;
Compute comp;
Pi task;
//System.setProperty("java.security.policy","file:./security.policy");
//if (System.getSecurityManager() == null) {
// System.setSecurityManager(new SecurityManager());
//}
System.out.println("Enter something: ");
String input = e.scan.nextLine();
int temp = e.processInput(input);
try {
switch(temp){
case 0:
name = "Echo";
registry = LocateRegistry.getRegistry();
comp = (Compute) registry.lookup(name);
task = new Pi(e.echoMessage);
output = comp.executeTask(task);
System.out.println(output);
break;
case 1:
name = "Compute";
registry = LocateRegistry.getRegistry();
comp = (Compute) registry.lookup(name);
task = new Pi(e.v1,e.v2);
output = comp.executeTask2(task);
System.out.println(output);
break;
default:
break;
}
} catch (Exception e1) {
System.err.println("ComputePi exception:");
e1.printStackTrace();
}
}
private int processInput(String input){
String temp = input.toUpperCase();
String arr[];
if(temp.startsWith("ECHO")){
arr = input.split(" ", 2);
echoMessage = arr[1];
return 0;
}
else if(Character.isDigit(temp.charAt(0))){
arr = input.split(" ");
v1 = Integer.parseInt(arr[0]);
v2 = Integer.parseInt(arr[2]);
return 1;
}
return -1;
}
}
인터페이스
package interfaces;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Compute extends Remote {
String executeTask(Pi t) throws RemoteException;
String executeTask2(Pi t) throws RemoteException;
}
원격 객체
package interfaces;
import java.io.Serializable;
public class Pi implements Serializable {
private static final long serialVersionUID = 227L;
public static String message = null;
private static int value1 = 0, value2= 0;
public Pi(String mess) {
this.message = mess;
}
public Pi(int v1, int v2) {
this.value1 = v1;
this.value2 = v2;
}
public String execute() {
return returnEcho(message);
}
public String execute2(){
return addNumbers(value1, value2);
}
public static String addNumbers(int v1, int v2){
int total = v1 + v2;
return Integer.toString(total);
}
public static String returnEcho(String n1) {
return n1;
}
}
당신은 null 값을 어디서 얻을 수 있습니까? 클라이언트 측에 – EJP
출력 = comp.executeTask (작업); 작업에 값이 있지만 서버 측 작업이 null 또는 0으로 바뀜 – user3629420
서버에서 executeTask()에 도착하면 't'매개 변수가 null입니다. – EJP