#! /home/xed/srexx

/*
 * req_time: sends a time request to the S/REXX script 'serv_time'
 *
 *  You can use either the OS/2 like function calls, such as SockSocket()
 *  or the S/REXX more simple function calls, such as socket()
 */

signal on novalue
parse arg host

port = 1521
if host = '' then host = hostname()

sock_out = socket("AF_INET", "SOCK_STREAM", 0)
if sock_out < 0 then
 { say 'req_time: unable to create a socket, errno = 'errno
   exit 1
 }

rc = setsockopt(sock_out, "SOL_SOCKET", "SO_LINGER", '1 2');
if rc < 0 then
 { say 'req_time: unable to setsockopt a socket, errno = 'errno
   exit 1
 }


rc = gethostbyname(host, 'host_add.!')
if rc < 0 then
 { say 'req_time: unable to gethostbyname 'host', h_errno = 'h_errno
   exit 1
 }

address_family = "AF_INET"
address_addr   = host_add.!addr
address_port   = port
rc = connect(sock_out, 'address_')
if rc < 0 then
 { say 'req_time: unable to connect, errno = 'errno
   exit 1
 }

rc = send(sock_out, 'time?')

len = recvfrom(sock_out, 'data', 1024,,'from_')

say 'time received: 'data
