![]() |
Cliser Examples: TCP/IP (UDP) Daytime Service |
![]() |
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:
$ java cliser.GUIThis will display the Cliser graphical user-interface, as shown below:
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.
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.
$ javac -deprecation DaytimeUDPClient.java $ javac -deprecation DaytimeIterativeUDPServer.javaThese 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.
$ java DaytimeIterativeUDPServerIf you are not the system administrator, you can still run the server by giving a different port as a command-line argument:
$ java DaytimeIterativeUDPServer 2013This 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.
$ java DaytimeUDPClient ursa.calvin.eduor if your server is listening at port 2013 on myMachine.myDomain:
$ java DaytimeUDPClient myMachine.myDomain 2013Our client's output was as follows:
Mon Jun 19 11:53:34 EDT 2000If this will be used frequently, we recommend that you write a shell script or .BAT file to make this more convenient for your users.