Program help:
Typedef struct _ systemtime {// st
Word wyear;
Word wmonth;
Word wdayofweek;
Word wday;
Word whour;
Word wminute;
Word wsecond;
Word wmilliseconds;
} Systemtime;
TheFiletimeStructure is a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601.
typedef struct _FILETIME { // ft
DWORD dwLowDateTime;
DWORD dwHighDateTime;
} FILETIME
TheFiletimetosystemtimeFunction converts a 64-bit file time to system time format.
BOOL FileTimeToSystemTime( CONST FILETIME *lpFileTime, // pointer to file time to convert LPSYSTEMTIME lpSystemTime // pointer to structure to receive // system time);
// Source program
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <TCHAR.H>
#include <winsock.h>
#pragma comment (lib,"Ws2_32")
#define UNICODE
#define _UNICODE
#define _countof(array) (sizeof(array)/sizeof(array[0]))
#define HIGHTIME 21968699 // 21968708 // Jan 1, 1900 FILETIME.highTime
#define LOWTIME 4259332096 // 1604626432 // Jan 1, 1900 FILETIME.lowtime
Using namespace STD;// NTP server list
Struct nistsvr {
Int key; // number
In_addr ADDR; // ip address
Lpctstr server; // Domain Name
Lpctstr Info; // Information
} Nistsvrsary [] = {
{0, {0, 0, 0}, null, null },
{1, {129,6, 15,28}, _ T ("time-a.nist.gov"), _ T ("NIST, gaishi fort, Maryland ")},
{2, {129,6, 15,29}, _ T ("time-b.nist.gov"), _ T ("NIST, gaishi fort, Maryland ")},
{3, {132,163, 4,101}, _ T ("time-a.timefreq.bldrdoc.gov"), _ T ("NIST, boerde, Colorado ")},
{4, {132,163, 4,102}, _ T ("time-b.timefreq.bldrdoc.gov"), _ T ("NIST, boerde, Colorado ")},
{5, {132,163, 4,103}, _ T ("time-c.timefreq.bldrdoc.gov"), _ T ("NIST, boerde, Colorado ")},
{6, {128,138,140, 44}, _ T ("tutcnist.colorado.edu"), _ T ("University of Colorado, Boulder ")},
{7, {192,43, 244,18}, _ T ("time.nist.gov"), _ T ("NCAR, bo'er city, Colorado ")},
{8, {131,107,}, _ T ("time-nw.nist.gov"), _ T ("Microsoft, Raymond, Washington ")},
{9, {208,184, 49,129}, _ T ("nist1.nyc.certifiedtime.com"), _ T ("abovnet, New York City ")},
};
// Selected NTP server
Static int choice = 0;
Void usage ()
{
Printf ("/n/T ------------------------------------------------/N ");
Printf ("/T = * = Time getter V1.0/t (qsilence@sina.com) = * =/N ");
Printf ("/T ------------------------------------------------/n ");
Int L = _ countof (nistsvrsary );
For (INT I = 1; I <L; I ++ ){
Printf ("% d. % s [% s] % S/R/N", nistsvrsary [I]. Key, nistsvrsary [I]. server,
Inet_ntoa (nistsvrsary [I]. ADDR), nistsvrsary [I]. info );
}
}
Bool updatesystime (DWORD dwtime)
{
Uint64 uicurtime, uibasetime, uiresult;
Systemtime st;
Uibasetime = (uint64) hightime <32) + lowtime;
Uicurtime = (uint64) dwtime * (uint64) 10000000; 1/10 million
Uiresult = uibasetime + uicurtime;
Filetimetosystemtime (lpfiletime) & uiresult, & St );
Return setsystemtime (& St );
}
Bool gettimefromserver (DWORD * lpdwtime)
{
* Lpdwtime = 0;
Bool breturn = false;
Socket ssock = socket (af_inet, sock_stream, 0 );
If (invalid_socket! = Ssock)
{
Struct sockaddr_in sin;
Memcpy (& sin. sin_addr, & nistsvrsary [choice]. ADDR, sizeof (in_addr ));
Sin. sin_family = af_inet;
Sin. sin_port = htons (ipport_timeserver );
Printf ("/n... waiting connection.../N ");
If (0 = connect (ssock, (struct sockaddr *) & sin, sizeof (struct sockaddr_in )))
{
Printf ("*** connected ***/N ");
Int iresult, Iread;
For (Iread = 0; Iread <4; Iread + = iresult)
{
Iresult = Recv (ssock, (char *) lpdwtime + Iread, 4-Iread, 0 );
If (iresult <1)
Break;
}
If (4 = Iread)
{
* Lpdwtime = ntohl (* lpdwtime );
Breturn = true;
}
Else
Printf ("error getting time! /N ");
}
Else
Printf ("*** Connection Failed ***/N ");
Closesocket (ssock );
}
Return breturn;
}
Int main (INT argc, char * argv [])
{
Wsadata;
If (wsastartup (makeword (1,1), & wsadata )! = 0)
{
Printf ("wsastartup failed./N ");
Wsacleanup ();
Exit (1 );
}
If (argc <2)
{
Usage ();
While (choice <= 0 | choice> 9)
Cin> choice;
}
Else
Choice = atoi (& argv [1] [strlen (argv [1])-1]);
DWORD dwtime;
// Obtain the server time
If (gettimefromserver (& dwtime ))
{
// Update the system time
If (updatesystime (dwtime ))
{
Printf ("*** system clock successfully updated ***/N ");
} Else {
Printf ("*** error setting the system clock ***/N ");
}
}
Wsacleanup ();
Return 0;
}
// NTP server list
Struct nistsvr {
Int key; // number
In_addr ADDR; // ip address
Lpctstr server; // Domain Name
Lpctstr Info; // Information
} Nistsvrsary [] = {
{0, {0, 0, 0}, null, null },
{1, {129,6, 15,28}, _ T ("time-a.nist.gov"), _ T ("NIST, gaishi fort, Maryland ")},
{2, {129,6, 15,29}, _ T ("time-b.nist.gov"), _ T ("NIST, gaishi fort, Maryland ")},
{3, {132,163, 4,101}, _ T ("time-a.timefreq.bldrdoc.gov"), _ T ("NIST, boerde, Colorado ")},
{4, {132,163, 4,102}, _ T ("time-b.timefreq.bldrdoc.gov"), _ T ("NIST, boerde, Colorado ")},
{5, {132,163, 4,103}, _ T ("time-c.timefreq.bldrdoc.gov"), _ T ("NIST, boerde, Colorado ")},
{6, {128,138,140, 44}, _ T ("tutcnist.colorado.edu"), _ T ("University of Colorado, Boulder ")},
{7, {192,43, 244,18}, _ T ("time.nist.gov"), _ T ("NCAR, bo'er city, Colorado ")},
{8, {131,107,}, _ T ("time-nw.nist.gov"), _ T ("Microsoft, Raymond, Washington ")},
{9, {208,184, 49,129}, _ T ("nist1.nyc.certifiedtime.com"), _ T ("abovnet, New York City ")},
};
// Selected NTP server
Static int choice = 0;
Void usage ()
{
Printf ("/n/T ------------------------------------------------/N ");
Printf ("/T = * = Time getter V1.0/t (qsilence@sina.com) = * =/N ");
Printf ("/T ------------------------------------------------/n ");
Int L = _ countof (nistsvrsary );
For (INT I = 1; I <L; I ++ ){
Printf ("% d. % s [% s] % S/R/N", nistsvrsary [I]. Key, nistsvrsary [I]. server,
Inet_ntoa (nistsvrsary [I]. ADDR), nistsvrsary [I]. info );
}
}
Bool updatesystime (DWORD dwtime)
{
Uint64 uicurtime, uibasetime, uiresult;
Systemtime st;
Uibasetime = (uint64) hightime <32) + lowtime;
Uicurtime = (uint64) dwtime * (uint64) 10000000; 1/10 million
Uiresult = uibasetime + uicurtime;
Filetimetosystemtime (lpfiletime) & uiresult, & St );
Return setsystemtime (& St );
}
Bool gettimefromserver (DWORD * lpdwtime)
{
* Lpdwtime = 0;
Bool breturn = false;
Socket ssock = socket (af_inet, sock_stream, 0 );
If (invalid_socket! = Ssock)
{
Struct sockaddr_in sin;
Memcpy (& sin. sin_addr, & nistsvrsary [choice]. ADDR, sizeof (in_addr ));
Sin. sin_family = af_inet;
Sin. sin_port = htons (ipport_timeserver );
Printf ("/n... waiting connection.../N ");
If (0 = connect (ssock, (struct sockaddr *) & sin, sizeof (struct sockaddr_in )))
{
Printf ("*** connected ***/N ");
Int iresult, Iread;
For (Iread = 0; Iread <4; Iread + = iresult)
{
Iresult = Recv (ssock, (char *) lpdwtime + Iread, 4-Iread, 0 );
If (iresult <1)
Break;
}
If (4 = Iread)
{
* Lpdwtime = ntohl (* lpdwtime );
Breturn = true;
}
Else
Printf ("error getting time! /N ");
}
Else
Printf ("*** Connection Failed ***/N ");
Closesocket (ssock );
}
Return breturn;
}
Int main (INT argc, char * argv [])
{
Wsadata;
If (wsastartup (makeword (1,1), & wsadata )! = 0)
{
Printf ("wsastartup failed./N ");
Wsacleanup ();
Exit (1 );
}
If (argc <2)
{
Usage ();
While (choice <= 0 | choice> 9)
Cin> choice;
}
Else
Choice = atoi (& argv [1] [strlen (argv [1])-1]);
DWORD dwtime;
// Obtain the server time
If (gettimefromserver (& dwtime ))
{
// Update the system time
If (updatesystime (dwtime ))
{
Printf ("*** system clock successfully updated ***/N ");
} Else {
Printf ("*** error setting the system clock ***/N ");
}
}
Wsacleanup ();
Return 0;
}