C++設置體系時光及體系時光收集更新的辦法。本站提示廣大學習愛好者:(C++設置體系時光及體系時光收集更新的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C++設置體系時光及體系時光收集更新的辦法正文
本文實例講述了C++設置體系時光及體系時光收集更新的辦法。分享給年夜家供年夜家參考。詳細完成辦法以下:
//依據前往的時光設置體系時光
void setTimeFromTP(ULONG ulTime)
{
FILETIME ft;
SYSTEMTIME st;
//將基准時光轉換成windows文件時光
st.wYear = 1900;
st.wMonth = 1;
st.wDay = 1;
st.wHour = 0;
st.wMinute = 0;
st.wSecond = 0;
st.wMilliseconds = 0;
SystemTimeToFileTime(&st, &ft);
LONGLONG* pLLong = (LONGLONG*)&ft;
*pLLong += (LONGLONG)10000000*ulTime;
FileTimeToSystemTime(&ft, &st);
::SetSystemTime(&st);
}
void main()
{
CInitSock initSock;
SOCKET s = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET)
{
printf("socket error...");
return;
}
sockaddr_in servAddr = {0};
servAddr.sin_family = AF_INET;
servAddr.sin_port = ::htons(37);
servAddr.sin_addr.S_un.S_addr = ::inet_addr("69.25.96.13"); //別用210.72.145.44這個IP, 這是天朝的 常TMD不前往值
if (SOCKET_ERROR == ::connect(s, (sockaddr*)&servAddr, sizeof(servAddr)))
{
printf("connect error..");
return;
}
//吸收時光
ULONG ulTime = 0;
int nRecv = ::recv(s, (char*)&ulTime, sizeof(ulTime), 0);
if (nRecv > 0) //收到數據
{
ulTime = ::ntohl(ulTime);
setTimeFromTP(ulTime);
}
else
{
printf("recv error...");
return;
}
::closesocket(s);
printf("*******************************");
getchar();
}
願望本文所述對年夜家的C++法式設計有所贊助。