How can i resolve hanging my N+ when i want to sync my Netduino with a time server.
Function call:
Utility.SetLocalTime(NTPTime("time-a.nist.gov"))
"time-a.nist.gov" : ntp ok, time,daytime busy, not recommended
He will sometimes hang when receive :
s.Receive(ntpData)
I can choose out of many servers but that is for now not the point.
http://tf.nist.gov/tf-cgi/servers.cgi
How to resolve?
Thanks in advance, Ellen
Private Shared Function NTPTime(ByVal TimeServer As [String]) As DateTime Dim ep As New IPEndPoint(Dns.GetHostEntry(TimeServer).AddressList(0), 123) Dim s As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp) Dim ntpData As Byte() = New Byte(47) {} Array.Clear(ntpData, 0, 48) ntpData(0) = &H1B s.SendTimeout = 5000 s.SendTo(ntpData, ep) s.Receive(ntpData) 'with debugging, HERE he will stop.' Dim offsetTransmitTime As Byte = 40 Dim intpart As Long = 0 Dim fractpart As Long = 0 For i As Integer = 0 To 3 intpart = (intpart << 8) Or ntpData(offsetTransmitTime + i) Next For i As Integer = 4 To 7 fractpart = (fractpart << 8) Or ntpData(offsetTransmitTime + i) Next Dim milliseconds As Long = CLng(intpart * 1000 + (fractpart * 1000) / &H100000000L) s.Close() Dim timeSpan__1 As TimeSpan = TimeSpan.FromTicks(CLng(milliseconds) * TimeSpan.TicksPerMillisecond) Dim dT As New DateTime(1900, 1, 1) dT += timeSpan__1 'daylight saving' If dT.Month < 3 OrElse dT.Month > 10 Then Return dT.AddHours(1) End If If dT.Month > 3 AndAlso dT.Month < 10 Then Return dT.AddHours(2) End If If dT.Month = 3 Then ' in march' If (dT.Day - CInt(dT.DayOfWeek)) < 25 Then Return dT.AddHours(1) End If If (CInt(dT.DayOfWeek) = 0) AndAlso (dT.Hour < 2) Then Return dT.AddHours(1) End If Return dT.AddHours(2) End If ' in october' If (dT.Day - CInt(dT.DayOfWeek)) < 25 Then Return dT.AddHours(2) End If If (CInt(dT.DayOfWeek) = 0) AndAlso (dT.Hour < 2) Then Return dT.AddHours(2) End If Return dT.AddHours(1) End Function