简体   繁体   English

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

[英]RMI client/server - cannot call some remote methods from client

I'm making a program which sends questions from a server to a cient where the client will answer them. 我正在制作一个程序,将服务器中的问题发送给客户将回答客户的问题。 I'm now trying to send these answers using sendAnswers() method, but it won't call saying Client.java:25: error: cannot find symbol temp.sendAnswer(userAnswer[i]); 我现在尝试使用sendAnswers()方法发送这些答案,但它不会调用说Client.java:25:错误:找不到符号temp.sendAnswer(userAnswer [i]); even though I've made the object. 即使我已经制作了这个对象。 I also cannot call sendMessage() method for some reason. 由于某种原因我也无法调用sendMessage()方法。

Server code: 服务器代码:

    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");
}
}

Interface code: 接口代码:

    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;

}

Implementation code: 实施代码:

    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;
}
}

Client code: 客户代码:

    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);
    }
}


}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM