Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
21 Mar 2005 1  
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