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 dummy localhost
dummy: Name lookup error
localhost: Connection failed 
54786 08-11-16 16:51:48 00 0 0 254.7 UTC(NIST) * Sun Nov 16 17:51:47 2008 no response

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 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))
                               (client (report i) (report i))
       after (sec 2) action
          forall i <- outstanding do 
             print i "no response"
          env.exit 0

Comments to the code: