Results 1 to 7 of 7

Thread: [RESOLVED] Time Server Synchronization WITHOUT Winsock???

  1. #1

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Resolved [RESOLVED] Time Server Synchronization WITHOUT Winsock???

    Hello,

    I found this article which permits me to synchronize my computer using Time Servers on the internet.

    http://vbnet.mvps.org/index.html?cod...cksynctime.htm

    Is there a way to do this without using the Winsock control?

    Thanks for your assistance.

  2. #2

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: Time Server Synchronization WITHOUT Winsock???

    Basically I want to reproduce the Internet Time tab in the windows Date and Time properties like this image but with api calls instead of winsock.

    Thanks.
    Attached Images Attached Images  

  3. #3
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    Re: Time Server Synchronization WITHOUT Winsock???


  4. #4

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: Time Server Synchronization WITHOUT Winsock???

    Thanks jeroen79, I could use that as a second option, however I was looking for a way without using winsock. I don't know what protocol windows uses to synchronise the time but I have seen another application that uses HTTP protocol.

  5. #5
    Addicted Member
    Join Date
    Jun 2006
    Posts
    172

    Re: Time Server Synchronization WITHOUT Winsock???

    Quote Originally Posted by Hassan Basri
    Thanks jeroen79, I could use that as a second option, however I was looking for a way without using winsock. I don't know what protocol windows uses to synchronise the time but I have seen another application that uses HTTP protocol.
    You mean, without the winsock control?

    That URL that was given to you shows how to use Winsock API. You need to use some sort of internet communication protocol to get this information.
    • If you found my post to be helpful, please rate me.

  6. #6
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: Time Server Synchronization WITHOUT Winsock???

    Just a thought: You could try using the Microsoft Internet Transfer Control 6.0. It also allows you to access the internet.
    Hey... If you found this post helpful please rate it.

  7. #7

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: Time Server Synchronization WITHOUT Winsock???

    Thanks for everybody's help, after having searched on the internet I found a way using HTTP protocol and VB code. Here is the link:

    http://brigsoft.com/atomic-clock-syn...cVbArticle.htm

    Here is the code from the web page. However it needs some adjusting in parsing the result received from the web page. However it gives the general idea of how to proceed.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Type SYSTEMTIME
    4.     wYear As Integer
    5.     wMonth As Integer
    6.     wDayOfWeek As Integer
    7.     wDay As Integer
    8.     wHour As Integer
    9.     wMinute As Integer
    10.     wSecond As Integer
    11.     wMilliseconds As Integer
    12. End Type
    13.  
    14. Private Type TIME_ZONE_INFORMATION
    15.     Bias As Long
    16.     StandardName As String * 64
    17.     StandardDate As SYSTEMTIME
    18.     StandardBias As Long
    19.     DaylightName As String * 64
    20.     DaylightDate As SYSTEMTIME
    21.     DaylightBias As Long
    22. End Type
    23.  
    24. Private Const INTERNET_OPEN_TYPE_PRECONFIG = 0
    25. Private Const INTERNET_OPEN_TYPE_DIRECT = 1
    26. Private Const INTERNET_OPEN_TYPE_PROXY = 3
    27. Private Const scUserAgent = "VB Project"
    28. Private Const INTERNET_FLAG_RELOAD = &H80000000
    29.  
    30. Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
    31.  
    32. Private Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" (ByVal hOpen As Long, ByVal sUrl As String, ByVal sHeaders As String, ByVal lLength As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
    33.  
    34. Private Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
    35.  
    36. Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
    37.  
    38. Private Declare Function GetTimeZoneInformation Lib "kernel32" (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long
    39.  
    40. Private Declare Sub GetLocalTime Lib "kernel32" (localTime As SYSTEMTIME)
    41.  
    42. Private Declare Sub SetLocalTime Lib "kernel32" (localTime As SYSTEMTIME)
    43.  
    44.  
    45. '=================================================================
    46. ' LocalZoneTime function calculates Local time using Universal time.
    47. ' Time zone information is gotten from Windows using
    48. ' GetTimeZoneInformation API function (see MSDN for mo information).
    49. ' The function support DST time.
    50. Sub LocalZoneTime(tmUniversal As SYSTEMTIME, tmLocal As SYSTEMTIME)
    51.  
    52.     Dim TZI As TIME_ZONE_INFORMATION
    53.  
    54.     Dim retCode As Long
    55.  
    56.     retCode = GetTimeZoneInformation(TZI)
    57.  
    58.     Dim iStandardMonth As Integer
    59.     Dim iDaylightMonth As Integer
    60.  
    61.     iStandardMonth = TZI.StandardDate.wMonth
    62.     iDaylightMonth = TZI.DaylightDate.wMonth
    63.  
    64.     Dim nZoneCorrection As Long
    65.     nZoneCorrection = TZI.Bias
    66.  
    67.     If retCode = 1 Then 'TIME_ZONE_ID_STANDARD
    68.         nZoneCorrection = nZoneCorrection + TZI.StandardBias
    69.     ElseIf retCode = 2 Then ' TIME_ZONE_ID_DAYLIGHT
    70.         nZoneCorrection = nZoneCorrection + TZI.DaylightBias
    71.     Else
    72.         Debug.Assert (0)
    73.     End If
    74.  
    75.     nZoneCorrection = -nZoneCorrection
    76.  
    77.     Dim nTotalMinutes As Integer
    78.  
    79.     nTotalMinutes = tmUniversal.wHour * 60 + tmUniversal.wMinute + nZoneCorrection
    80.  
    81.     If nTotalMinutes < 0 Then
    82.         nTotalMinutes = nTotalMinutes + 24 * 60
    83.     End If
    84.     If nTotalMinutes > 24 * 60 Then
    85.         nTotalMinutes = nTotalMinutes - 24 * 60
    86.     End If
    87.  
    88.     tmLocal.wHour = Int(nTotalMinutes / 60)
    89.     tmLocal.wMinute = nTotalMinutes - tmLocal.wHour * 60
    90.     tmLocal.wSecond = tmUniversal.wSecond
    91.  
    92. End Sub
    93.  
    94.  
    95. '===========================================================
    96. ' OnSyncro is called after SYNCRO dialog button pressing.
    97. ' It opens Internet connection, receives Universal Time,
    98. ' calculates Local Time and set it in Windows.
    99.  
    100. Public Function OnSynchro()
    101.  
    102.     Dim hInternet As Long
    103.     Dim hHttp As Long
    104.     Dim bRet As Boolean
    105.     Dim sBuff As String * 2048
    106.     Dim lNumberOfBytesRead As Long
    107.     Dim sBuffer As String
    108.  
    109.     hInternet = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PRECONFIG, _
    110.     vbNullString, vbNullString, 0)
    111.     If hInternet = 0 Then Debug.Assert (0)
    112.  
    113.     hHttp = InternetOpenUrl(hInternet, "http://tycho.usno.navy.mil/cgi-bin/timer.pl", vbNullString, 0, _
    114.     INTERNET_FLAG_RELOAD, 0)
    115.     If hHttp = 0 Then Debug.Assert (0)
    116.  
    117.     sBuff = vbNullString
    118.     bRet = InternetReadFile(hHttp, sBuff, _
    119.     Len(sBuff), lNumberOfBytesRead)
    120.     sBuffer = sBuffer & Left$(sBuff, _
    121.     lNumberOfBytesRead)
    122.  
    123.     If hHttp <> 0 Then InternetCloseHandle (hHttp)
    124.     If hInternet <> 0 Then InternetCloseHandle (hInternet)
    125.  
    126.     Dim dtOldTime As SYSTEMTIME
    127.     Dim tmLocal As SYSTEMTIME
    128.     Dim tmUniversal As SYSTEMTIME
    129.  
    130.     Call GetLocalTime(tmLocal)
    131.     dtOldTime = tmLocal
    132.  
    133.     Dim buffPos As Integer
    134.     buffPos = InStr(1, sBuff, "UTC")
    135.  
    136.     If Not buffPos > 0 Then
    137.         MsgBox ("Unrecognized communication error!")
    138.         Return
    139.     End If
    140.  
    141.  
    142.     tmUniversal.wHour = Mid$(sBuff, buffPos - 11, 2)
    143.     tmUniversal.wMinute = Mid$(sBuff, buffPos - 8, 2)
    144.     tmUniversal.wSecond = Mid$(sBuff, buffPos - 5, 2)
    145.  
    146.     ' Calculate Local Time using received Universal Time.
    147.     Call LocalZoneTime(tmUniversal, tmLocal)
    148.  
    149.     Call SetLocalTime(tmLocal)
    150.  
    151.     Dim sRep
    152.     sRep = "Successful synchronization!" + Chr$(10) + Chr$(13) + _
    153.     "Time before: " + Str$(dtOldTime.wHour) + ":" + Str$(dtOldTime.wMinute) + ":" + Str$(dtOldTime.wSecond) + Chr$(10) + Chr$(13) + _
    154.     "Time after: " + Str$(tmLocal.wHour) + ":" + Str$(tmLocal.wMinute) + ":" + Str$(tmLocal.wSecond) + "."
    155.  
    156.     MsgBox (sRep)
    157.  
    158. End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width