17-10-13, 05:48 PM
(آخر تعديل لهذه المشاركة : 18-10-13, 04:12 PM {2} بواسطة Omar Mekkawy.)
السلام عليكم ورحمة الله وبركاته
اريد حد يشرحلى الكود ده او يوضح الفكرة العامة له
اريد حد يشرحلى الكود ده او يوضح الفكرة العامة له
كود :
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(GetFastestNISTDate().ToLocalTime)
End Sub
Public Function GetFastestNISTDate() As DateTime
Dim result = DateTime.MinValue
' Initialize the list of NIST time servers
' http://tf.nist.gov/tf-cgi/servers.cgi
Dim servers As String() = New String() _
{"nist1-ny.ustiming.org", "nist1-nj.ustiming.org", "nist1-pa.ustiming.org", "time-a.nist.gov", "time-b.nist.gov", "nist1.aol-va.symmetricom.com", _
"nist1.columbiacountyga.gov", "nist1-chi.ustiming.org", "nist.expertsmi.com", "nist.netservicesgroup.com"}
' Try 5 servers in random order to spread the load
Dim rnd As New Random()
For Each server As String In servers.OrderBy(Function(s) rnd.NextDouble()).Take(5)
Try
' Connect to the server (at port 13) and get the response
Dim serverResponse As String = String.Empty
Using reader = New IO.StreamReader(New System.Net.Sockets.TcpClient(server, 13).GetStream())
serverResponse = reader.ReadToEnd()
End Using
' If a response was received
If Not String.IsNullOrEmpty(serverResponse) Then
' Split the response string ("55596 11-02-14 13:54:11 00 0 0 478.1 UTC(NIST) *")
Dim tokens As String() = serverResponse.Split(" "c)
' Check the number of tokens
If tokens.Length >= 6 Then
' Check the health status
Dim health As String = tokens(5)
If health = "0" Then
' Get date and time parts from the server response
Dim dateParts As String() = tokens(1).Split("-"c)
Dim timeParts As String() = tokens(2).Split(":"c)
' Create a DateTime instance
Dim utcDateTime As New DateTime(Convert.ToInt32(dateParts(0)) + 2000, Convert.ToInt32(dateParts(1)), Convert.ToInt32(dateParts(2)), Convert.ToInt32(timeParts(0)), Convert.ToInt32(timeParts(1)), Convert.ToInt32(timeParts(2)))
' Convert received (UTC) DateTime value to the local timezone
result = utcDateTime '.ToLocalTime()
' Response successfully received; exit the loop
Return result
End If
End If
End If
' Ignore exception and try the next server
Catch
End Try
Next
Return result
End Function
End Class