Simple TCP/IP time server

This program demonstrates a simple TCP/IP server. It will accept a connection from a client application, and echo the current time to it.

Programming Issues

This program illustrates the classic process for a TCP/IP server program. Summarised, it is as follows:

Note that it's present form, the server enters an infinite loop and has no mechanism for closing itself. We have to use a command such as kill to terminate it.

Usage

Once launched in the background, the server can be used either with a dedicated time client, or simply by telnetting to it. For the latter approach, the following is a sample session using Linux:

[paul@localhost paul]$ ./timeserv 3357 &
[paul@localhost paul]$ telnet localhost 3357
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Sat Oct 16 19:26:17 1999

Connection closed by foreign host.
[paul@localhost paul]$

After executing telnet and receiving some initialisation output, we the server process echoing the current time to us, then closing the connection.

You can specify the port number to listen to as a command line argument. The program will default to TIME_PORT (#defined as 2002) if one is not supplied.

Source and Downloads