Contact Info

Crumbtrail

ActiveXperts.com » Network Component » Network Component Objects » NTP

ActiveXperts Network Component Ntp Object

The Network Time Protocol (NTP) has been developed to address the issue of providing accurate and synchronized time across the Internet. The protocol itself has been in development since 1985 and has therefore reached a fairly mature status. The latest stable version of NTP is version 3 and described in RFC-1305 the latest testing version is NTP version 4.

The Network Time Protocol aims to synchronize a systems clock to Coordinated Universal Time (UTC). UTC is a development of Greenwich Mean Time (GMT). GMT is based on the rotation of the earth while UTC is based on the standard length of a second determined from quantum phenomena, but both use the Greenwich time zone as the zero offset.

NTP uses a augmented hierarchical client-server topology. A small number of servers distribute time to a larger number of clients which can simultaneously act as servers to further clients. The top level clients are referred to as stratum 1 servers and their clients are termed as being at stratum 2. Clients of a stratum 2 server are at stratum 3 and so on. The stratum 1 servers are synchronized to radio clocks which receive time signals from national time standards such as the MSF signal provided by the NPL broadcast from Rugby or the GPS time signal provided by the Global Positioning System. The radio clocks themselves are termed stratum 0 servers.

The Network Component Ntp object enables you to add Ntp functionality to your program.

The Ntp object is part of the Network Component. Overview of all Network Component objects:

DnsServer & DnsRecord - Ftp & FtpFile - Http - Icmp - IPtoCountry - Msn - Ntp - Radius - Rsh - Scp - SFtp - Ssh - SnmpManager - SnmpTrapManager - SnmpMibBrowser - Tcp - Tftp - TraceRoute - Udp - Xen - Wake-on-LAN - Xen (Citrix)


Ntp Sample code

Visual Basic sample: Retrieve date and time settings from an NTP server on the internet

Public asObj As AxNetwork.Ntp

   Private Sub BTN_RETRIEVETIME_Click()
     asObj.GetTime (CB_TIMESERVERS.List(CB_TIMESERVERS.ListIndex))
     If (asObj.LastError = 0) Then
       TXT_RESULT = "SUCCESS"
       TXT_DATETIME = asObj.Year & "/" & _
        asObj.Month & "/" & _
          asObj.Day & " " & _
          asObj.Hour & ":" & _
          asObj.Minute & ":" & _
          asObj.Second
       TXT_OFFSET = asObj.LocalOffsetSeconds
     Else
       TXT_RESULT = "ERROR #" & asObj.LastError
       TXT_DATETIME = "n/a"
       TXT_OFFSET = "n/a"
     End If
   End Sub

   Private Sub Form_Load()
     Set asObj = CreateObject("AxNetwork.Ntp")
     CB_TIMESERVERS.AddItem ("fartein.ifi.uio.no")
     CB_TIMESERVERS.AddItem ("ntp0.coreng.com.au")
     CB_TIMESERVERS.AddItem ("ntp1.rnp.br")
     CB_TIMESERVERS.AddItem ("142.3.100.2")
     CB_TIMESERVERS.ListIndex = 0
   End Sub

Visual C++ sample: Retrieve date and time settings from an NTP server on the internet

   int main(int argc, char* argv[])
   {
     INtp     *pNtp = NULL;
     HRESULT   hr;
     LONG      lYr, lMon, lDay, lHr, lMn, lSc;
     LONG      lLastError, lOffset;

     CoInitialize(NULL);     // Initialize COM

     hr = CoCreateInstance( CLSID_Ntp, NULL, CLSCTX_INPROC_SERVER, IID_INtp, (void**) &pNtp );
     
     if( ! SUCCEEDED( hr ) ) 
     {
        goto _EndMain;
     }
     
     pNtp->GetTime( _bstr_t( "fartein.ifi.uio.no" ) );
     pNtp->get_LastError( &lLastError );
     
     if( lLastError != asERR_SUCCESS )
       goto _EndMain;
     
     pNtp->get_Year( &lYr ); pNtp->get_Month( &lMon ); pNtp->get_Day( &lDay );
     pNtp->get_Hour( &lHr ); pNtp->get_Minute( &lMn ); pNtp->get_Second( &lSc );
     pNtp->get_LocalOffsetSeconds( &lOffset );
     
     printf( "Date:%04d/%02d/%02d\n", lYr, lMon, lDay );
     printf( "Time:%02d:%02d:%02d\n", lHr, lMn, lSc );
     printf( "Offset: %02d min and %02d sec\n", lOffset, lOffset%60 );

   _EndMain:
     if( pNtp != NULL ) {
        pNtp->Release();
        pNtp = NULL;
     }
     return 0;
   }

On ftp.activexperts-lab.com, you can find a lot of Network Component samples. These samples are also part of the Network Component installation.

Visit ftp.activexperts-lab.com