Click here to Skip to main content
Licence 
First Posted 21 Mar 2005
Views 37,085
Bookmarked 25 times

How to use Winsock to set system clock via Internet Time Servers

By | 21 Mar 2005 | Article
Using Winsock UDP to set system clock.

Sample Image

Introduction

This document describes the methodology used by a program to query Internet servers for the correct time, and this time reporting can be used to set your computer's time clock. The resolution of the time servers using the RFC868 protocol is one second. It is very accurate, but unless you query a server just after the server's clock turns over to the next second, you will probably be off in the setting of your clock by as much as one second. (Using this to set your clock it will always be slow, by as much as--but no longer than--a full second.)

Place the program in whatever folder on your computer you wish. There is only one file in the program, RFC868.EXE. The name comes from the document called RFC868 (Request For Comment #868).

Background

This program was designed to solve a problem I have had in conjunction with the construction of another program of mine, a digital clock. I wanted to query an Internet time server to set the system clock. RFC1305 and, in particular, the more relevant SNTP looked like what I wanted, but unfortunately they are beyond my understanding at the present time. Trying to find Winsock code on the net that implements this far simpler RFC868 wasn't easy, so I finally converted the UA_TIME.C program found in the book Windows Sockets Network Programming (coded for the dial-up RFC867) to run using the RFC868 format. My hope is that others out there in a similar situation find this code useful. Anyone out there knowing how to implement an SNTP client is hereby implored to inform me on the method. Thanks.

Using the code

How to include the Winsock library into the compilation of the project

  1. Click on the Class View icon on the toolbar.
  2. Right click on the icon for the project.
  3. Choose Properties.
  4. In the Property Pages dialog, select Linker.
  5. Choose Command Line.
  6. Type "/DEFAULTLIB:Ws2_32.lib" (without the quotes) into the "Additional Options" box.
  7. You will have to do this for both the debug and release versions.

Points of Interest

Here is where I get the chance to tell if there was something interesting or fun about the project. Well yes, there was something; rather serendipitous I would say. For some time now, I have been wondering how APPs launch the default browser to visit Internet sites and such. The answer? ShellExecute(). Curiously, the documentation on this function doesn't say much about the wonderful things it can do. I just wonder how robust it is. Example from the code:

case ID_HELP_GNULICENSEAGREEMENT:
  nRet = (int)ShellExecute(hWnd, "open", 
              "http://www.gnu.org/copyleft/gpl.html", 
              NULL, NULL, SW_SHOW);
  if (nRet < 33)
      MessageBox(hWnd, "Sorry, couldn't open the GNU web page.", 
                      "RFC868 Client", MB_OK | MB_ICONQUESTION);
  break;

Also there was something annoying. I don't like warnings and like to eliminate them whenever possible, but here in this very example, so far I've met with no success. The (int) cast in front of ShellExecute() would seem to be necessary since the function returns an HINSTANCE handle. Nevertheless, even with the cast, the warnings persist.

History

Version 1.00.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Charles Lazo



United States United States

Member

I worked as a developer from 1990 to 1997 (lead programmer for the software installation program called INSTALIT--company bankrupt) and since then as a store clerk.
 
My interests include science and mathematics, reading, movies and (mostly) coding interesting stuff. Website: 4nax.com. Fornax, a constellation seen in the Southern Hemisphere, is a laboratory furnace.


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionhow will your UDP packets traverse a Proxy ? Pinsussandre_k11:24 22 Aug '05  
GeneralShellExecute warning correction PinmemberSteve Mayfield19:42 21 Mar '05  
GeneralRe: ShellExecute warning correction PinmemberCharles Lazo22:45 21 Mar '05  
GeneralRe: ShellExecute warning correction PinmemberCharles Lazo3:02 30 Mar '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 21 Mar 2005
Article Copyright 2005 by Charles Lazo
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid