網路對時程式

來源:互聯網
上載者:User

程式協助:

typedef struct _SYSTEMTIME {  // st
    WORD wYear;
    WORD wMonth;
    WORD wDayOfWeek;
    WORD wDay;
    WORD wHour;
    WORD wMinute;
    WORD wSecond;
    WORD wMilliseconds;
} SYSTEMTIME;

The FILETIME structure 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

The FileTimeToSystemTime function 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);
//來源程式
#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伺服器列表
struct NISTSVR{
 int     key; //編號
 in_addr addr; //IP地址
 LPCTSTR server; //網域名稱
 LPCTSTR info; //資訊
} NISTSVRSARY[] = {
 { 0, {0,0,0,0}, NULL, NULL},
 { 1, {129,6,15,28}, _T("time-a.nist.gov"),_T("NIST,蓋士堡,馬里蘭州") },
 { 2, {129,6,15,29}, _T("time-b.nist.gov"),_T("NIST,蓋士堡,馬里蘭州") },
 { 3, {132,163,4,101}, _T("time-a.timefreq.bldrdoc.gov"),_T("NIST,博耳德市,科羅拉多州") },
 { 4, {132,163,4,102}, _T("time-b.timefreq.bldrdoc.gov"),_T("NIST,博耳德市,科羅拉多州") },
 { 5, {132,163,4,103}, _T("time-c.timefreq.bldrdoc.gov"),_T("NIST,博耳德市,科羅拉多州") },
 { 6, {128,138,140,44}, _T("tutcnist.colorado.edu"),_T("科羅拉多大學,博耳德市") },
 { 7, {192,43,244,18}, _T("time.nist.gov"),_T("NCAR,博耳德市,科羅拉多州") },
 { 8, {131,107,1,10}, _T("time-nw.nist.gov"),_T("Microsoft,雷蒙德,華盛頓州") },
 { 9, {208,184,49,129}, _T("nist1.nyc.certifiedtime.com"),_T("Abovnet,紐約市") },
};

//所選擇的NTP伺服器
static int choice = 0;

void usage()
{
 printf( "/n/t----------------------------------------------/n" );
 printf( "/t=*= Time Getter v1.0 /t(qsilence@sina.com) =*=/n" );
 printf( "/t----------------------------------------------/n/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/一千萬
 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 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;
 //取伺服器時間
 if (GetTimeFromServer(&dwTime))
 {
  //更新系統時間
  if (UpdateSysTime(dwTime))
  {
   printf( "    ***System Clock Successfully Updated***/n");
  }else{
   printf( "    ***Error setting the System Clock***/n");
  }
 }

WSACleanup();
 
 return 0;
}

//NTP伺服器列表
struct NISTSVR{
 int     key; //編號
 in_addr addr; //IP地址
 LPCTSTR server; //網域名稱
 LPCTSTR info; //資訊
} NISTSVRSARY[] = {
 { 0, {0,0,0,0}, NULL, NULL},
 { 1, {129,6,15,28}, _T("time-a.nist.gov"),_T("NIST,蓋士堡,馬里蘭州") },
 { 2, {129,6,15,29}, _T("time-b.nist.gov"),_T("NIST,蓋士堡,馬里蘭州") },
 { 3, {132,163,4,101}, _T("time-a.timefreq.bldrdoc.gov"),_T("NIST,博耳德市,科羅拉多州") },
 { 4, {132,163,4,102}, _T("time-b.timefreq.bldrdoc.gov"),_T("NIST,博耳德市,科羅拉多州") },
 { 5, {132,163,4,103}, _T("time-c.timefreq.bldrdoc.gov"),_T("NIST,博耳德市,科羅拉多州") },
 { 6, {128,138,140,44}, _T("tutcnist.colorado.edu"),_T("科羅拉多大學,博耳德市") },
 { 7, {192,43,244,18}, _T("time.nist.gov"),_T("NCAR,博耳德市,科羅拉多州") },
 { 8, {131,107,1,10}, _T("time-nw.nist.gov"),_T("Microsoft,雷蒙德,華盛頓州") },
 { 9, {208,184,49,129}, _T("nist1.nyc.certifiedtime.com"),_T("Abovnet,紐約市") },
};

//所選擇的NTP伺服器
static int choice = 0;

void usage()
{
 printf( "/n/t----------------------------------------------/n" );
 printf( "/t=*= Time Getter v1.0 /t(qsilence@sina.com) =*=/n" );
 printf( "/t----------------------------------------------/n/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/一千萬
 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 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;
 //取伺服器時間
 if (GetTimeFromServer(&dwTime))
 {
  //更新系統時間
  if (UpdateSysTime(dwTime))
  {
   printf( "    ***System Clock Successfully Updated***/n");
  }else{
   printf( "    ***Error setting the System Clock***/n");
  }
 }

WSACleanup();
 
 return 0;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.