簡體   English   中英

Java RMI-服務器自動調用遠程方法

[英]java RMI - server automatically calling remote methods

我正在嘗試啟動一個RMI程序,該程序將允許服務器向客戶端發送3個問題,客戶端將接受答案並將其發送回服務器。 到目前為止,我已經在服務器上發送了問題並得到了答案,但是服務器會自動調用getAnswer()方法,並且該方法不存儲用戶輸入的內容。

這是運行服務器和客戶端時獲得的控制台結果:

    Server Started
0
0
Client connected, sending acknowledgement
received answer from client: 3

如您所見,它甚至在客戶端連接之前就調用getAnswer()方法,因此盡管未給出答案,但為什么顯示2個值。 我該如何使服務器僅在需要它們時才調用這些方法? 我已經嘗試過循環和if語句,但是我想不出有效的方法!

服務器代碼:

    import java.rmi.*;

public class Server
{

public static void main(String a[]) throws Exception
{
    int answer1, answer2, answer3;

    System.out.println("Server Started");
    try
    {
        Implement obj = new Implement(); 
        Naming.rebind("remote", obj);//register name with registry


        obj.sendQuestion(question1);
        answer1 = obj.getAnswer();

        obj.sendQuestion(question2);
        answer2 = obj.getAnswer();


    } catch(Exception e)
    {
        e.printStackTrace();
    }

}

static String question1 = "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";
static String question2 = "Q2: (A+B)*(A-B)\n1. A*A+2*B*B\n2. A*A-B*B\n3. A*A-2*A*B+B*B";
static String question3 = "";
}

客戶代碼:

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

    public class Client
    {
      public static void main(String a[]) throws Exception
    {

    try {
        Interface obj = (Interface)Naming.lookup("remote"); //looks for object with add in name
        Interface m = (Interface) obj;
        System.out.println(obj.sendMessage()); //retrieve message from server

        System.out.println(obj.receiveQuestion());
        System.out.println("Please enter your answer");

        Scanner scan = new Scanner(System.in);

        int input = scan.nextInt();
        obj.test(input);
        System.out.println(obj.receiveQuestion());

        } catch(Exception e)
        {
            e.printStackTrace();
        }

}

}

接口代碼:

    import java.rmi.Remote;

    public interface Interface extends Remote //becomes a remote interface
    {
    public String sendMessage() throws Exception;
    public void test(int message) throws Exception;
    public void sendQuestion(String message) throws Exception;
    public String receiveQuestion() throws Exception;
    public int getAnswer() throws Exception;

    }

實施代碼:

    import java.rmi.server.*;

public class Implement extends UnicastRemoteObject implements Interface

{
String msg;
int answer;

public Implement() throws Exception //constructor to handle exceptions
{
    super();
}

public String sendMessage()
{
    String msg = "You have connected to the server";
    System.out.println("Client connected, sending acknowledgement");
    return msg;     
}

public void sendQuestion(String message)
{
    msg = message;
}

public String receiveQuestion()
{
    return msg;
}

public void test(int message)
{
    answer = message;
    System.out.println("received answer from client: " +answer);
}

public int getAnswer()
{

    System.out.println(answer);

    return answer;
}
}

你有這個回到前面。 您正在服務器中調用自己的遠程方法。 客戶端應該調用遠程方法。 根據您的描述,您希望服務器是客戶端,而客戶端是服務器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM