欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c語(yǔ)言實(shí)現(xiàn)系統(tǒng)時(shí)間校正工具代碼分享

 更新時(shí)間:2014年01月24日 15:54:31   作者:  
這篇文章主要介紹了c語(yǔ)言實(shí)現(xiàn)系統(tǒng)時(shí)間校正工具,大家參考使用吧

復(fù)制代碼 代碼如下:

//*******************************************************************
//Time Protocol是一種非常簡(jiǎn)單的應(yīng)用層協(xié)議。它返回一個(gè)未格式化的32位二進(jìn)制數(shù)字,
 //這個(gè)數(shù)字描述了從1900年1月1日午夜到現(xiàn)在的秒數(shù)。服務(wù)器在端口37監(jiān)聽(tīng)協(xié)議請(qǐng)求,以
 //TCP/IP或者UDP/IP格式返回響應(yīng)。將服務(wù)器的返回值轉(zhuǎn)化為本地時(shí)間是客戶端程序的責(zé)任。
 //這里使用的時(shí)間服務(wù)器是129.132.2.21,更多的服務(wù)器地址在“http://tf.nist.gov/service/time-servers.html
 //網(wǎng)站列出。
 //*******************************************************************

#include<iostream>
#include<WinSock2.h>

usingnamespacestd;

#pragmacomment(lib,"ws2_32")

voidSetTimeFromTP(ULONGulTime)
{
//Windows文件時(shí)間是一個(gè)64位的值,它是從1601年1月1日中午12:00到現(xiàn)在的時(shí)間間隔,
//單位是1/10000000秒,即1000萬(wàn)分之1秒
FILETIMEft;
SYSTEMTIMEst;

st.wYear=1900;
st.wMonth=1;
st.wDay=1;
st.wHour=0;
st.wMinute=0;
st.wSecond=0;
st.wMilliseconds=0;

SystemTimeToFileTime(&st,&ft);
//然后將TimeProtocol使用的基準(zhǔn)時(shí)間加上已經(jīng)逝去的時(shí)間,即ulTime
LONGLONG*pllLong=(PLONGLONG)&ft;
//注意文件時(shí)間單位是1/10000000秒,即1000萬(wàn)分之1秒
*pllLong+=(LONGLONG)10000000*ulTime;
//再將時(shí)間轉(zhuǎn)化回來(lái),更新系統(tǒng)時(shí)間
FileTimeToSystemTime(&ft,&st);
SetSystemTime(&st);
}

intmain(intargc,char**argv)
{
WSADATAwsaData;
WSAStartup(WINSOCK_VERSION,&wsaData);

SOCKETs=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(INVALID_SOCKET==s)
{
cout<<"socketerror:"<<GetLastError()<<endl;
return0;
}

SOCKADDR_INservAddr;
servAddr.sin_family=AF_INET;
servAddr.sin_port=htons(37);
servAddr.sin_addr.S_un.S_addr=inet_addr("129.132.2.21");

if(-1==connect(s,(PSOCKADDR)&servAddr,sizeof(servAddr)))
{
cout<<"connecterror:"<<WSAGetLastError()<<endl;
return0;
}

//等待接收時(shí)間協(xié)議返回,最好使用異步IO,以便設(shè)置超時(shí)
ULONGulTime=0;
intiRecv=recv(s,(char*)&ulTime,sizeof(ulTime),0);
if(iRecv>0)
{
ulTime=ntohl(ulTime);
SetTimeFromTP(ulTime);
cout<<"成功與服務(wù)器時(shí)間同步!"<<endl;
}
else
{
cout<<"時(shí)間服務(wù)器不能確定當(dāng)前時(shí)間!"<<endl;
}

shutdown(s,SD_RECEIVE);
closesocket(s);

WSACleanup();
return0;
}

相關(guān)文章

最新評(píng)論