Search code examples
javarmi

how does the content of that class get printed that i have not started on CMD


This is a question related to RMI. i have 4 .class files an interface,it's implementation,a class named server and a class named client.

After starting the rmiregistry i start the server which is like :

import java.rmi.Naming;


public class Server {
  public static void main(String args[]) {
      try {
          to_Server_Impl toServer = new to_Server_Impl();
          Naming.rebind("Server", toServer);
      } catch(Exception exc) {
          System.out.println(exc);
        }
  }
}

Then i start the client whose code is :

import java.rmi.Naming;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;


 public class Client {
   public static void main(String args[]) {
       try {
            to_Server_Intf s_Intf = (to_Server_Intf)Naming.lookup("rmi://127.0.0.1/Server");
          do {  
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String message_to_send = null;
            System.out.println("===Enter your message===");
            message_to_send = br.readLine();
            // sending a message to the server
            String reply = s_Intf.send(message_to_send);
            System.out.println(reply);
          }while(true);
       }catch(Exception exc) {
           System.out.println(exc);
       }
   }
}

Using the reference of the remote object i call the function send of that class, whose code is like:

import java.rmi.*;
import java.rmi.server.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class to_Server_Impl extends UnicastRemoteObject implements to_Server_Intf{
    public to_Server_Impl() throws Exception{ }

@Override
public String send(String str){ // this function receives the message from the Client.
    // this method indirectly replies,by calling reply and then returning the replies string
    String receivedString = str;        
    System.out.println(receivedString);
    System.out.println();
    String reply = reply();

    return reply;
}

@Override
public String reply() { // this function replies the Client
    String replyString = null;
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            replyString = br.readLine();
        }catch(Exception exc) {
            System.out.println(exc);
        }
    return replyString;
 }
}

When i have called the method of the above class the print statements of the above class ,print the messages on the "server" window. How is that possible ?

Client Window:(client is sending the message)

enter image description here

Server Window :(the message gets printed on the server window(the call was directed to the function of class to_Server_Impl). HOW ?)

enter image description here


Solution

  • I don't understand your question. In the send method (which is on the server, i.e. it is called by the client remotely on the server), you are printing the received message. Hence, it appears in the server console.

    Edit: Essentially this is how RMI works:

    The server application publishes an interface with the available methods to be called. In your case this is to_Server_Intf. The server also has an implementation of the interface, which is to_Server_Impl. The server publishes the remote object by Naming.bind (or Naming.rebind) to a registry.

    The clients only know about the interface that is also on the server, but not about the implementation. They can do a lookup on the registry and find the remote object by name with Naming.lookup. Now the client has a remote object instance and can call methods on it. This object (s_intf in your client code) is actually a local proxy for the interface implementation present on the server. When the client calls a method on this proxy, the call is actually sent to the object on the server, which is why you are seeing the string in the server console.

    What actually happens when the client calls the method on the local stub is that the parameters (if any) are serialized and a TCP socket message (or whatever the underlying implementation is) is created, which is sent to the server who is listening for incoming requests. On the server side, the message is received, the parameters are deserialized and the correct method is identified from the message and called. After the method returns, again, the result is serialized, a TCP message is created which is sent back to the client.