VB Code:
Private Function SyncClockViaInternet() As Boolean
Dim iret As Integer
Dim rBuff(1023) As Byte
Dim sBuff As String
Dim tBuff() As String
Dim nDate As Date
Dim clientSocket As Socket
Dim d As Date
Try
clientSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
clientSocket.Connect(New IPEndPoint(Dns.Resolve("time.nist.gov").AddressList(0), 13))
iret = clientSocket.Receive(rBuff)
sBuff = Encoding.ASCII.GetString(rBuff, 0, iret - 1)
'format: 53865 06-05-10 15:16:44 50 0 0 253.9 UTC(NIST) *
sBuff = Replace(sBuff, Chr(10), "")
tBuff = Split(sBuff, " ")
nDate = "20" & Replace(tBuff(1), "-", "/") & " " & tBuff(2)
d = "#" & Format(TimeZone.CurrentTimeZone.ToLocalTime(nDate), "MM/dd/yyyy") & " " & Format(TimeZone.CurrentTimeZone.ToLocalTime(nDate), "HH:mm:ss") & "#"
OpenNETCF.Win32.DateTimeEx.SetLocalTime(d)
clientSocket.Close()
Return True
Catch ex As Exception
Try
clientSocket.Close()
Catch
End Try
Return False
End Try
End Function