Skip to main content

[archive] Getting the time from a NTP server

  • June 17, 2012
  • 0 replies
  • 0 views

[Migrated content. Thread originally posted on 30 June 2009]

Ever wanted to get a time from a NTP server?
This example provided by my colleague Claudio shows how to accomplish this using C$SOCKET. Cudos Claudio!

       identification division.
       program-id.  getNTP-via-socket.
       Environment Division.
       Configuration Section.
       Special-Names.   
           Decimal-Point Is Comma.
       Input-Output Section.
       File-Control.
       Data Division.
       File Section.
       Working-Storage Section.
      *
           Copy "socket.Def".
      *
      *these are the values used by the socket routine to establish a
      *connection with the local machine.
       01 unix-server-address          pic x(15) value spaces.
       01 unix-server-port             pic x(6) value spaces.
       01 unix-receive-command         pic x(50) value spaces.
       01 unix-receive-cmd-length      pic 9(3).
       77 unix-connection-handle       usage handle.   
      *
      *these are the socket buffer variables.
       01 byte-hold                    pic x value space.
       01 end-cmd-byte                 pic x value x"0A".
       01 end-data-cmd-byte            pic x value x"5E".
       01 num-bytes-read               pic 9(3) value 0.
       01 max-cmd-length               pic 9(3) value 0.
      *       
       Procedure Division.
      *
       Acu-Main-Logic.
      *
      * [URL]www.inrim.it/.../URL]
            move "13" to unix-server-port.
            move "193.204.114.233" to unix-server-address.
            call "c$socket" using ags-create-client,
                                  unix-server-port,
                                  unix-server-address
                 giving unix-connection-handle.
       
            if unix-connection-handle = 0
               display message box
               "Unable to connect to the local server.  There may be a p
      -        "roblem with your Internet connection.  Contact your supp
      -        "ort administrator."                   
               title "Socket Error"
               type 1
               icon 2
               go to end-prog   
            end-if.   
            initialize unix-receive-command byte-hold.
            move 1   to unix-receive-cmd-length.     
            move 500 to max-cmd-length.             
           
            perform until byte-hold = end-cmd-byte or
                    unix-receive-cmd-length > max-cmd-length
               move 1 to num-bytes-read
               call "c$socket" using ags-read, unix-connection-handle,
                    byte-hold, num-bytes-read
               string byte-hold delimited by size into
                      unix-receive-command with pointer
                      unix-receive-cmd-length
            end-perform.
           
            display "======================================"
            display "Received timestamp: "           
            display unix-receive-command
            display "======================================"
            accept omitted           
           
            call "c$socket" using ags-close, unix-connection-handle
            .
           
       end-prog.
            stop run.

0 replies

Be the first to reply!