none
Get internet atomic time RRS feed

  • Question

  • Hi,
    in my .net 3.5 application, how can i query and get internet time?
    I don't want to synchronize the system clock and i don't want to use the local system clock as users might manually change it.

    Thanks,
    -srinivas yelamanchili

    Tuesday, May 6, 2008 10:52 PM

Answers

  • Got to the NIST site and find a server near you.  Replace the Server in this code with your server:

    Code Snippet

    Public Class NTC

      Private NTPData(47) As Byte

      Private Const OffsetTransmitTime As Byte = 40

      Private Srvr As String = "nist.expertsmi.com"

      Private Prt As Integer = 123

      Private ReceiveTime As DateTime

      Friend Sub New()

      End Sub

      Friend Sub New(ByVal Server As String)

        Me.Srvr = Server

      End Sub

      Friend Sub New(ByVal Server As String, ByVal Port As Integer)

        Me.Srvr = Server

        Me.Prt = Port

      End Sub

      Friend Function GetTime() As ULong

        Dim UdpSocket As New Sockets.UdpClient

        Dim Address As IPHostEntry = Dns.GetHostEntry(Me.Srvr)

        Dim EndPt As New IPEndPoint(Address.AddressList(0), Me.Prt)

        UdpSocket.Connect(EndPt)

        NTPData(0) = &H1B

        UdpSocket.Send(NTPData, NTPData.Length)

        NTPData = UdpSocket.Receive(EndPt)

        ReceiveTime = Now

        Dim IntPart As ULong

        Dim FractPart As ULong

        Dim I, J As Integer

        J = 4

        For I = 0 To 3

          IntPart <<= 8

          IntPart += NTPData(OffsetTransmitTime + I)

          FractPart <<= 8

          FractPart += NTPData(OffsetTransmitTime + J)

          J += 1

        Next

        IntPart <<= 32

        IntPart += FractPart

        Return IntPart

      End Function

    End Class

    Also look for the KissOfDeath caveat on the NIST site.

     

    Wednesday, May 7, 2008 5:30 AM

All replies

  • Windows does this already for you, time.windows.com.  No support in .NET framework accordingly, look for the RFC for the NTP protocol and implement it with the Socket class.
    Wednesday, May 7, 2008 12:35 AM
    Moderator
  • Got to the NIST site and find a server near you.  Replace the Server in this code with your server:

    Code Snippet

    Public Class NTC

      Private NTPData(47) As Byte

      Private Const OffsetTransmitTime As Byte = 40

      Private Srvr As String = "nist.expertsmi.com"

      Private Prt As Integer = 123

      Private ReceiveTime As DateTime

      Friend Sub New()

      End Sub

      Friend Sub New(ByVal Server As String)

        Me.Srvr = Server

      End Sub

      Friend Sub New(ByVal Server As String, ByVal Port As Integer)

        Me.Srvr = Server

        Me.Prt = Port

      End Sub

      Friend Function GetTime() As ULong

        Dim UdpSocket As New Sockets.UdpClient

        Dim Address As IPHostEntry = Dns.GetHostEntry(Me.Srvr)

        Dim EndPt As New IPEndPoint(Address.AddressList(0), Me.Prt)

        UdpSocket.Connect(EndPt)

        NTPData(0) = &H1B

        UdpSocket.Send(NTPData, NTPData.Length)

        NTPData = UdpSocket.Receive(EndPt)

        ReceiveTime = Now

        Dim IntPart As ULong

        Dim FractPart As ULong

        Dim I, J As Integer

        J = 4

        For I = 0 To 3

          IntPart <<= 8

          IntPart += NTPData(OffsetTransmitTime + I)

          FractPart <<= 8

          FractPart += NTPData(OffsetTransmitTime + J)

          J += 1

        Next

        IntPart <<= 32

        IntPart += FractPart

        Return IntPart

      End Function

    End Class

    Also look for the KissOfDeath caveat on the NIST site.

     

    Wednesday, May 7, 2008 5:30 AM