日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ntp时间获取

發(fā)布時間:2025/3/20 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ntp时间获取 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

//ntp時間獲取

char szIP[512]={0};

BOOL bF = HostNameToIP("www.google.com",szIP);

if (bF)

{

printf("%s/n",szIP);

}

?

SYSTEMTIME time;

BOOL bFlag=GetCurTime(time,"61.153.197.226");

?

char szUrl[512]={0};

BOOL bFl=IPToHostName("74.125.153.99",szUrl);

if(bFl){

printf("%s/n",szUrl);

}

?

//ntp.h

///

BOOL GetCurTime(SYSTEMTIME& time,char* ip);

?

bool HostNameToIP(char* HostName,char* ip);

bool IPToHostName(char* ip,char* HostName);?

?

?

//ntp.cpp

///

#include "StdAfx.h" //預處理頭

?

#include "windows.h"

#include "time.h"

#include <winsock.h>

#pragma comment(lib,"ws2_32.lib")

?

struct ? NTP_Packet{

int ? Control_Word; ??

int ? root_delay; ??

int ? root_dispersion; ??

int ? reference_identifier; ??

__int64 ? reference_timestamp; ??

__int64 ? originate_timestamp; ??

__int64 ? receive_timestamp; ??

int ? transmit_timestamp_seconds; ??

int ? transmit_timestamp_fractions; ??

};

?

/** 本地時間 */

BOOL GetCurTime(SYSTEMTIME& time,char* ip)

{

WORD wVersionRequested;

WSADATA wsaData;

wVersionRequested = MAKEWORD( 1, 1 );

if (0!=WSAStartup(wVersionRequested, &wsaData))?

{

WSACleanup();

return 0;

}

if (LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!=1)?

{

WSACleanup( );

return 0;?

}

SOCKET soc=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);

struct sockaddr_in addrSrv;

addrSrv.sin_addr.S_un.S_addr=inet_addr(ip);//inet_addr("192.43.244.18");

// addrSrv.sin_addr.S_un.S_addr=inet_addr("210.72.145.44");

addrSrv.sin_family=AF_INET;

addrSrv.sin_port=htons(123);

NTP_Packet NTP_Send,NTP_Recv;?

NTP_Send.Control_Word ? = ? htonl(0x0B000000); ??

NTP_Send.root_delay ? = ? 0; ??

NTP_Send.root_dispersion ? = ? 0; ??

NTP_Send.reference_identifier ? = ? 0; ??

NTP_Send.reference_timestamp ? = ? 0; ??

NTP_Send.originate_timestamp ? = ? 0; ??

NTP_Send.receive_timestamp ? = ? 0; ??

NTP_Send.transmit_timestamp_seconds ? = ? 0; ??

NTP_Send.transmit_timestamp_fractions ? = ? 0;?

if(SOCKET_ERROR==sendto(soc,(const char*)&NTP_Send,sizeof(NTP_Send),

0,(struct sockaddr*)&addrSrv,sizeof(addrSrv)))

{

closesocket(soc);

return 0;

}

int sockaddr_Size =sizeof(addrSrv);

if(SOCKET_ERROR==recvfrom(soc,(char*)&NTP_Recv,sizeof(NTP_Recv),

0,(struct sockaddr*)&addrSrv,&sockaddr_Size))

{

closesocket(soc);

return 0;

}

closesocket(soc);

WSACleanup();

SYSTEMTIME newtime;

struct tm *lpLocalTime;

float Splitseconds;?

time_t ntp_time=ntohl(NTP_Recv.transmit_timestamp_seconds)-2208988800;

lpLocalTime=localtime(&ntp_time);?

if(lpLocalTime==NULL)

{

return 0;

}

newtime.wYear ? ? ?=lpLocalTime->tm_year+1900; ??

newtime.wMonth ? ? =lpLocalTime->tm_mon+1;

newtime.wDayOfWeek =lpLocalTime->tm_wday;

newtime.wDay ? ? ? =lpLocalTime->tm_mday; ??

newtime.wHour ? ? ?=lpLocalTime->tm_hour; ??

newtime.wMinute ? ?=lpLocalTime->tm_min; ??

newtime.wSecond ? ?=lpLocalTime->tm_sec;?

Splitseconds=(float)ntohl(NTP_Recv.transmit_timestamp_fractions); ??

Splitseconds=(float)0.000000000200 * Splitseconds; ??

Splitseconds=(float)1000.0 * Splitseconds; ??

newtime.wMilliseconds ? = ? (unsigned ? short)Splitseconds; ??

time= newtime;

return TRUE;

}

?

?

?

/** 主機名解析為ip */

bool HostNameToIP(char* HostName,char* ip)

{

WORD wVersionRequested;

WSADATA wsaData;

wVersionRequested = MAKEWORD( 1, 1 );

if (0!=WSAStartup(wVersionRequested, &wsaData))?

{

WSACleanup();

return false;

}

if (LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!=1)?

{

WSACleanup( );

return false;?

}

char *pURL=HostName; ??

struct hostent *hp;

char **p;

hp = gethostbyname(pURL);

if(hp==NULL) {return false; }

for (p = hp->h_addr_list; *p ; p++) //循環(huán)顯示所有IP

{

char *sIP;

sIP = inet_ntoa(*(struct in_addr*)*p);

CString strIP = sIP;

// printf("%s/r/n",strIP.GetBuffer(0));

?

int pos=strlen(ip);

strcat(ip,strIP.GetBuffer(0));

strcat(ip,",");

}

?

WSACleanup();

return true;

}

?

/** ip地址解析為主機名 */

bool IPToHostName(char* ip,char* url)?

{

WORD wVersionRequested;

WSADATA wsaData;

wVersionRequested = MAKEWORD( 1, 1 );

if (0!=WSAStartup(wVersionRequested, &wsaData))?

{

WSACleanup();

return false;

}

if (LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!=1)?

{

WSACleanup( );

return false;?

}

?

//--------------------轉換過程--------------------------[

u_long addr;

struct hostent *hp;

char **p;

?

if ((int)(addr = inet_addr(ip)) == -1) {

return false;

}

?

hp = gethostbyaddr((char *)&addr, sizeof (addr), AF_INET);

if (hp == NULL) {

return false;

}

?

strcpy(url,hp->h_name);

//--------------------轉換過程--------------------------]

WSACleanup();

?

return true;

}

?

總結

以上是生活随笔為你收集整理的ntp时间获取的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。