PingTimeServers

Another network client, that asks a number of network time servers, listed on the command line, for current time. Here is a sample run:

examples> ./PingTimeServers time.nist.gov time-a.nist.gov time.ien.it dummy localhost
dummy: Name lookup error
localhost: Connection failed
time-a.nist.gov: 
54786 08-11-16 16:51:48 00 0 0 254.7 UTC(NIST) * 

time.ien.it: Sun Nov 16 17:51:47 2008

time.nist.gov: no response
examples> 

The program connects to servers that use the old Daytime Protocol, listening on TCP port 13. In this protocol, a time server will immediately when connection has been established send current time and close the connection. The three first arguments above are existing time servers. Two of these responded quickly with time in this run, while time.nist.gov did not respond within two seconds. The fourth argument is a nonsense host that the name server will not identify and the final argument, localhost, is a valid host which will probably not run a time server.

Here is the program:


module PingTimeServers where

import POSIX
import Data.Functional.List

port = Port 13  -- standard port for time servers

client neterror report sock = class

    established = action
       sock.inFile.installR report

    close = request result ()

    result Connection {..}
 

root env = class

    args = [1..size env.argv-1] 
    print i mess = env.stdout.write
                      (env.argv!i ++ ": "++ mess ++ "\n")

    outstanding := args

    report i mess = action
       outstanding := delete i outstanding
       print i mess
       if (null outstanding) then env.exit 0
    
    result action
       forall i <- args do
          env.inet.tcp.connect (Host (env.argv!i))
                               port
                               (client (report i) (report i))
       after (sec 2) action
          forall i <- outstanding do 
             print i "no response"
          env.exit 0


Comments to the code: