Use Java RMI to write a simple client-server application to play a Lottery. The player should select 6 distinct numbers in the range 1-50 and wins if any one of those numbers matches the single number generated at random on the server.

The objectives of this coursework are to:

  • Write a program to play Lotto according to the rules above.
  • Understand the invocation of methods on remote objects.
  • Understand the production of distributed Java objects.

Summary of the Work

You will have to:

  • Define the remote methods to allow the client to send the numbers to the server.
  • Implement these methods as a remote object.
  • Write a client to invoke the remote methods.
  • Use the rmic program to produce a stub and a skeleton.
  • Use the rmiregistry tool to register the server.
  • Programming
  • Defining the Interface

In this coursework, firstly you define the server methods. The Lotto server receives and reads the list of numbers selected by the client. Then it checks that this list is valid (distinct numbers in the range 1-50):

  • If not, return an error message to the client.
  • If yes, make the lottery draw and see if the client has any winning numbers. Return the result to the client.

The server is comprised of an interface and a class. The interface provides the definition for the methods that can be called from the client. Essentially the interface defines the client's view of the remote object. The class provides the implementation. Define these methods in a Java LottoInfo interface. Derive the interface from the Remote class or from java.io.Serializable interface (The interface must extend from java.rmi.Remote. All the method declarations must include the throws java.rmi.RemoteException clause).

Implementing the Remote Objects on the Server

Write the LottoInfoImpl class that implements the LottoInfo interface and the LottoServer class that creates an object and registers it on the server.

  • Write the LottoInfoImpl class that contains the actual methods to receive the numbers.
  • Derive the class from the UnicastRemoteObject class
  • Create a LottoServer class with a method that creates a object. Register this object with the RMI remote object registry, name and bind it to a port using the Naming class and the rebind() method.

Programming the Client

Produce the LottoClient class that locates the server and invokes the remote methods. The command to start the client will be $ java LottoClient rmi://machine/remote_object Where the URL refers to the registry(usually on the same machine that the server runs on, probably localhost). Retrieve the remote object using the Naming class, by passing the use of the registry to the lookup() method. Then make the remote method call on this object. Producing the Distributed Objects. Finally, compile and run the distributed objects.

  • Compile all the classes you have written.
  • Your RMI java programs are only a part of the application. It uses two other pieces of code called the stub and the skeleton. These reformat the method parameters and encapsulate them into a RMI data unit. This process is called marshalling. Run the command rmic LottoInfoImpl to generate the stub and the skeleton.
  • Launch the registry rmiregistry. Be sure to run the registry in the RMI server directory that contains the stub and skeleton.
  • Run the server and the client on a same machine and directory. Then try to run the client and the server on two different machines.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.