staload "libc/SATS/stdio.sats"
staload "libc/sys/SATS/socket.sats"
staload "libc/sys/SATS/sockaddr.sats"
staload "libc/netinet/SATS/in.sats"
staload "libc/sys/SATS/socket_in.sats"
staload "libc/arpa/SATS/inet.sats"
#define MAXLINE 1024
#define TIME_SERVER_NAME_DEFAULT "192.43.244.18"
#define TIME_SERVER_PORT_DEFAULT 13
implement main (argc, argv) = let
val servname = (
if argc > 1 then argv.[1] else TIME_SERVER_NAME_DEFAULT
) : string val () = (print "servname = "; print servname; print_newline ())
val nport = (
if argc > 2 then int_of_string (argv.[2]) else TIME_SERVER_PORT_DEFAULT
) : int val () = (print "nport = "; print nport; print_newline ())
val servport = in_port_nbo_of_int (nport)
var inp: in_addr_struct val () = inet_aton_exn (servname, inp)
var servaddr: sockaddr_in_struct val () = sockaddr_in_init
(servaddr, AF_INET, in_addr_struct_get_s_addr inp, servport)
val [fd:int] (pfskt | sockfd) = socket_family_type_exn (AF_INET, SOCK_STREAM)
val () = connect_in_exn (pfskt | sockfd, servaddr)
typedef buf_t = @[byte][MAXLINE]
val b0 = byte_of_int (0)
var !p_buf = @[byte][MAXLINE](b0) val () = loop (pfskt | !p_buf) where {
fun loop (pfskt: !socket_v (fd, conn) | buf: &buf_t)
:<cloref1> void = let
val n = socket_read_exn (pfskt | sockfd, buf, MAXLINE)
in
if n > 0 then let
val (pf_stdout | p_stdout) = stdout_get ()
val () = fwrite_byte_exn (file_mode_lte_w_w | buf, n, !p_stdout)
val () = stdout_view_set (pf_stdout | )
in
loop (pfskt | buf)
end else begin
end end } val () = shutdown_exn (pfskt | sockfd, SHUT_RDWR) in
end