RMI客户端/服务器 - 无法从客户端调用某些远程方法

时间:2015-03-04 13:57:54

标签: java rmi

我正在制作一个程序,将服务器的问题发送给客户将回答客户的问题。我现在尝试使用sendAnswers()方法发送这些答案,但它不会调用说Client.java:25:错误:找不到符号temp.sendAnswer(userAnswer [i]);即使我做了这个对象。由于某种原因,我也无法调用sendMessage()方法。

服务器代码:

    import java.rmi.*;

public class Server
{

private static final String HOST = "localhost";

public static void main(String[] args) throws Exception
{
    Implement[] account = 
        {new Implement(1,"Q1: (A+B)*(A+B)\n1. A*A+B*B\n2. A*A+A*B+B*B\n3.          A*A+2*A*B+B*B",3),
        new Implement(2,"Q2: (A+B)*(A-B)\n1. A*A+2*B*B\n2. A*A-B*B\n3. A*A-    2*A*B+B*B",2),
        new Implement(3,"sin(x)*sin(x)+cos(x)*cos(x)\n1. 1\n2. 2\n3. 3",1)};

    for (int i=0; i<account.length; i++)
    {
        int questionNum = account[i].getQuestionNum();
        Naming.rebind("//" + HOST + "/account" + questionNum, account[i]);

    }
    System.out.println("Binding complete...\n");
}
}

接口代码:

    import java.rmi.Remote;

public interface Interface extends Remote //becomes a remote interface
{
public int getQuestionNum() throws Exception;
public String getQuestion() throws Exception;
public int getAnswer() throws Exception;
public String sendMessage() throws Exception;
public void sendAnswer(int userAnswer) throws Exception;

}

实施代码:

    import java.rmi.server.*;

public class Implement extends UnicastRemoteObject implements Interface

{
private int questionNo;
private String questionS, msg;
private int answerS, userAnswerS;

public Implement(int questionNum, String question, int answer) throws     Exception //constructor to handle exceptions
{
    questionNo = questionNum;
    questionS = question;
    answerS = answer;
}

public int getQuestionNum() throws Exception
{
    return questionNo;
}

public String getQuestion() throws Exception
{
    return questionS;
}

public int getAnswer() throws Exception
{
    return answerS;
}

public void sendAnswer(int userAnswer) throws Exception
{
    userAnswerS = userAnswer;
}

public String sendMessage() throws Exception
    msg = "Message from server: connection established";
{
    System.out.println("Client connected, sending acknowledgement");
    return msg;
}
}

客户代码:

    import java.rmi.*;
import java.util.Scanner;

public class Client
{
private static final String HOST = "localhost";
private static final int[] questionNum = {1,2,3};
private static int[] userAnswer = {0,0,0};

public static void main(String a[]) throws Exception
{
    try  
    {
        Scanner scan = new Scanner(System.in);
        for (int i=0; i<questionNum.length; i++)
        {
            Interface temp = (Interface)Naming.lookup(
                    "//" + HOST + "/account" + questionNum[i]);

            System.out.println("\nQuestion number: " + 
                                    temp.getQuestionNum()); 
            System.out.println(temp.getQuestion());
            System.out.println("Enter your answer");
            userAnswer[i] = scan.nextInt();
            temp.sendAnswer(userAnswer[i]);

        }

    }
    catch(ConnectException conEx)
    {
        System.out.println("Unable to connect to server!");
        System.exit(1);
    }
    catch(Exception e)
    {
        e.printStackTrace();
        System.exit(1);
    }
}


}

1 个答案:

答案 0 :(得分:0)

坚持 - 我删除了存根文件,重新制作并编译了所有内容并运行,现在工作正常,谢谢!