Set System Time Network update

Source: Internet
Author: User
Tags socket error

Code:
[Python]
// Set the system time based on the returned Time
Void setTimeFromTP (ULONG ulTime)
{
FILETIME ft;
SYSTEMTIME st;
 
// Convert the reference time to the windows file time
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"); // do not use the 210.72.145.44 IP address, which is a common TMD value not returned by tianchao.
If (SOCKET_ERROR =: connect (s, (sockaddr *) & servAddr, sizeof (servAddr )))
{
Printf ("connect error ..");
Return;
}
 
// Receipt time
ULONG ulTime = 0;
Int nRecv =: recv (s, (char *) & ulTime, sizeof (ulTime), 0 );
If (nRecv> 0) // receives data
{
UlTime =: ntohl (ulTime );
SetTimeFromTP (ulTime );
}
Else
{
Printf ("recv error ...");
Return;
}

: Closesocket (s );
Printf ("*******************************");
Getchar ();
}
Author: xiaocaiju

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.