Search:
Assignment #1: Sockets and RPC

Introduction

Socket and RMI are two techniques in Java which could be used to establish two-way communication between  two running programs (typically a client and a server) running on the network. But in principal, they work in quite different ways. A socket is just a way to send data (only data, not methods) on a port to a different host, it's up to you to define your own protocol. RMI is a technology in which the methods of remote Java objects can be invoked from other Java virtual machines running on the different hosts. 

In this assignment, you will implement client-server communication programs based on both Java Socket and RMI techniques.   

In Part I we'll use plain sockets, while in Part II we'll make use of RMI (remote method invocation).

Due date: Tuesday 09.10.2007

Part 1: Using Sockets

Learning about sockets

Read the following resource carefully to learn about sockets:


http://java.sun.com/docs/books/tutorial/networking/sockets/index.html


If you never did any network programming in Java, it may be a good idea to read some more:

http://java.sun.com/docs/books/tutorial/networking/index.html

You may want to have a look at other information:


http://de.wikipedia.org/wiki/Socket
http://www.javaworld.com/javaworld/jw-12-1996/jw-12-sockets.html
 

Task

You will write a client program and a server program. 

Your client should be able to

  1. send your student number to the server program 
  2. send your student name to the server program

 Your server should be able to

  1. ask the client program to send the student number
  2. ask the client program to send the student name
  3. send the client program a message to indicate the communication succeeded or aborted.  

 Note: How to design the server-client communication protocal is up to you.

Implementation

You have to implement 4 java classes:

  • SocketClient.java: This class connects to the server and communicates with it according to the ClientProtocol
  • ClientProtocol.java: This class describes how your client reacts to your servers messages (see the “Knock-Knock-Protocol in the Java Tutorial)
  • SocketServer.java: This class sets up a socket listening for your client to connect - and communicates with your client
  • ServerProtocol.java: This class describes how your server reacts to the messages of your client


Additional Information:

You are only required to test your server and client communication locally. When you create a socket, use localhost instead of IP address and port number.

We encourage you to run your server program and client program on different machines.

Part 2: RPC

 

Learning about RPC / RMI

RMI is a Java implementation for RPC.
Read java.sun.com/docs/books/tutorial/rmi/index.html

We recommend to use eclipse with the RMI plugin from genady:
http://www.genady.net/rmi/

The plugin is commercial, but free for a test period of 14 days.
It will help you to avoid some sources of error and contains a handy registry inspector and more.
Have a look at the plugin tutorials and read
http://www.tutorials.de/forum/java/187420-eclipse-und-rmi.html
to get started.

Of course, you can do it all by hand, without using the plugin.

Task

You will build a simple distributed application called fruit service engine.

The fruit service engine is a remote object on the server that keeps a fruit-price table to record the different kinds of fruit's prices, takes the clients' tasks, querys the fruit-price table, does the calculation and returns all results.

You will  define four types of client tasks.

  1. A client task to add a new fruit-price entity into the fruit-price table.
  2. A client task to update a fruit-price entity in the fruit-price table.
  3. A client task to delete a fruit-price entity in the fruit-price table.
  4. A client task to give a fruit quantity, query the fruit price and calculate the fruit cost.

Implementation

First of all, you'll need some interface definitions we built. Download them here.

Implement six classes:

  • FruitComputeEngine.java - this class extends UnicastRemoteObject, implements the compute interface, and creates an object in the RMI Registry.  It implements a method to execute 4 types of client tasks.  
  • FruitComputeTaskRegistry.java - this class looks for the fruit compute engine, creates a client task, and runs the client task on the fruit compute engine. 
  • AddFruitPrice.java - this class implements the Task interface. It implements the execute() method to add a new fruit-price entity to the fruit-price table.
  • UpdateFruitPrice.jave - this class implements the Task interface. It implements the execute() method to upade a fruit-price entity in the fruit-price table.
  • DeleteFruitPrice.java - this class implements the Task interface. It implements the execute() method to delete a fruit-price entity from the fruit-price table.
  • CalFruitCost.java - this class implements the Task interface. It implements the execute() methods to query a fruit price and calculate the fruit cost. 

Note: Make sure you start your RMI registry server at port 1099 (the default port). 

Additional Information:

You are only required to test your RMI application locally. Please use localhost instead of IP address and Port Number.

We encourage you to test your RMI application by running your client and server programs in the different computers.

 

What to hand in

You have to hand in your java source code via email (li(at)ifi.uzh.ch). Please make sure your code contains meaningful comments – you don't have to write full javadoc, just give some short hints what the code is doing.