To Cliser Home

Cliser Examples: TCP/IP (UDP) Daytime Service

To Cliser Architecture

Daytime is a standard TCP/IP service for determining the date and time on a given machine. The service is defined for both the TCP and UDP communication protocols. In this example, we will build a client and server for the daytime service in Java using UDP.

A daytime server listens for client requests on port 13. When it receives a message from a client, a daytime server replies to that client with its current date and time.

A daytime client thus simply sends a message to a daytime server and then displays its reply, which will be the date and time on that server's host.

To build a client and server for the daytime service, we follow the steps described previously:

1. Use Cliser

1a. Run Cliser.

The nicest interface for Cliser is a graphical user interface written in Java. To run it, enter:
  $ java cliser.GUI
This will display the Cliser graphical user-interface, as shown below:

1b. Enter Parameters.

We then enter the name of our service (Daytime), the port is should use (13), and the protocol we want to use (UDP); indicate what we want to generate ( both a client and a server), specify the language we want to generate (Java), and the kind of server we want (Iterative/single-threaded):

1c. Generate Source Code.

In the GUI interface, we then click the Generate button. This generates the files DaytimeUDPClient.java and DaytimeIterativeUDPServer.java. We then click the Quit button to quit using Cliser.

2. Customize the Source Code

Since we are building both a client and a server, we must customize both of the files generated in the previous step.

2a. Customize the Server

Using our favorite text editor, we open the file DaytimeIterativeUDPServer.java and replace the comments in method interactWithClient() with statements that provide the required behavior. Thanks to Java's Date class, this is quite easy:
  public void interactWithClient()
  {
    String discard = this.receive();
    this.send( ( new Date() ).toString() );
  }
Our server customization is this simple because of the Cliser system architecture which, among other things, provides the easy-to-use, protocol-independent send() and receive() communication primitives.

Note that because the UDP protocol is connection-less, a Cliser UDP server must use receive() to receive the initial request from a client, even if it doesn't use that request directly.

To use Java's Date class, we must also add the line

  import java.util.Date;
to the other import statement at the beginning of the file. The result is the file DaytimeIterativeUDPServer.java.

2b. Customize the Client

Again using our favorite text editor, we open the file DaytimeUDPClient.java and replace the comments in the body of method interactWithServer() with statements that define the required behavior:
  public void interactWithServer()
  {
    this.send("Hi");
    System.out.println( this.receive() );
  }
The result is the file DaytimeUDPClient.java. As was the case with our server, our client customization is quite easy thanks to Cliser's simple send() and receive() communication primitives.

3. Compiling

Once we have customized the files generated by Cliser, we can compile each as we would any other Java file:
  $ javac -deprecation DaytimeUDPClient.java
  $ javac -deprecation DaytimeIterativeUDPServer.java
These commands create the files DaytimeUDPClient.class and DaytimeIterativeUDPServer.class, respectively. Because Cliser-generated clients and servers are derived from classes in the Cliser class library you must have this library installed on your machine for such clients and servers to compile and run correctly.

4. Deploy and Run

To deploy your client and server, copy their .class files to a directory/folder that is in your CLASSPATH.

4a. Running the Server

You can then run the server (as the system administrator) as follows:
  $ java DaytimeIterativeUDPServer
If you are not the system administrator, you can still run the server by giving a different port as a command-line argument:
  $ java DaytimeIterativeUDPServer 2013
This allows servers written by students to be tested without giving them access as system administrators. It also allows a "real" server to be thoroughly tested before it is put into service on the standard TCP/IP port.

4b. Running the Client

You can run the client by specifying the host you want it to contact:
  $ java DaytimeUDPClient ursa.calvin.edu
or if your server is listening at port 2013 on myMachine.myDomain:
  $ java DaytimeUDPClient myMachine.myDomain 2013
Our client's output was as follows:
  Mon Jun 19 11:53:34 EDT 2000
If this will be used frequently, we recommend that you write a shell script or .BAT file to make this more convenient for your users.


Up to the Cliser home page Up to the Examples A TCP Client/Server for the standard Echo Service using C++ over BSD A TCP Client/Server for the standard Echo Service using C++ over WinSock A UDP Client/Server for a Random Quotes Service using C++ over TLI A TCP Client/Server for a Knock-Knock Service using Java


This page maintained by Joel Adams.