简体   繁体   中英

java RMI - server automatically calling remote methods

I'm trying to start an RMI program which will allow a server to send a client 3 questions and the client will take in answers and send them back to the server. So far on the server I send the question and get the answer, but the server automatically calls the getAnswer() method and that method doesn't store what the user enters.

This is the console results I get when I run server and client:

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

As you can see it calls the getAnswer() method before the client even connected, hence why there are 2 values shown despite answers not being given. How can I make the server only call those methods when I want them to? I've tried loops and if statements but nothing I can think of works!

Server code:

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

Client code:

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

}

}

Interface code:

    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;

    }

Implementation code:

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

You have this back to front. You're calling your own remote methods in the server. It is the client who should be calling the remote methods. From your description, you want your server to be a client, and your client a server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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