TicTacToe: Java RMI with serialized objects and callbacks

This site illustrates using Java RMI with a serialized object and with callbacks. Note: this code requires the server job to be on a separate computer from the client.

The files:

The pieces that are needed for supporting callbacks (that is, the server sending data to the client whenever there is a change) are

Thus when any one client sends an update operation to the server, all clients see the new board state.

To compile and run the example, do the following:

  1. Download the files to the machine that will be your host.
  2. On the host machine, edit ServiceName and ServicePort in TTTServiceImpl.java to use unique values.
  3. Still on the host machine, compile the service by typing
            javac TTTServiceImpl.java
    
  4. Recompile the code on the host:
            javac TTTServiceImpl.java
    
  5. Copy the files to your client side - making sure the copy has the same entry for ServiceName and ServicePort - and compile them:
            javac TicTacToe.java
    
  6. Switch back to the host and type
            rmiregistry xxxx
    
    where xxxx is the port number you used for ServicePort. For example, I type "rmiregistry 10000".
  7. Start up another command-line prompt window on the host and type
            java TTTServiceImpl
    
  8. Back on the client, run the game:
            java TicTacToe
    
    You should now be able to select squares and generate a winner. You could try running TicTacToe on another PC as well (as another client), but you'll quickly see that things don't work well.