Search code examples
javasocketsrmi

Is it possible to ask server side to execute a method without using RMI?


I couldn't try this out since I don't have a java web server. My idea is to sent a number to the server via sockets. The number sent will be dependent on user's response or requirement.

For example: Consider a tic-tac-toe game. Let's imagine that the user lost the match and I want the user to enter yes to replay or no to exit.

If the user enters yes , the program will write a number (eg. 1) to the outputstream of the socket. Now, the server receives the number using inputstream and stores it in an integer variable. If it finds that the number entered is 1 , the game begins again. There will be methods which handle these numbers received.

Is it possible to communicate with the server and invoke a remote method in this manner?

If yes, why was RMI designed when it can be done by coding (pretty easily)?


Solution

  • Is it possible to communicate with the server

    Yes.

    and invoke a remote method

    No. The server is invoking the method locally. You're just sending it a number telling it what to do. No remote method invocation here.

    why was RMI designed when it can be done by coding (pretty easily)?

    RMI provides a syntax for calling a remote method directly, without having to worry about:

    • connections
    • method numbers
    • wire specifications for request and response
    • marshalling the arguments
    • unmarshalling the response
    • turning some responses into exceptions
    • ensuring that all responses obey exception semantics
    • dynamic class loading
    • ...

    You can certainly reimplement all that yourself, and of course RMI is implemented in Java as well, but don't underestimate the scope, or the difficulty.