This article illustrates the method of setting up system time and System time network update in C + +. Share to everyone for your reference. The implementation methods are as follows:
Copy Code code as follows:
Set the system time according to the time returned
void Settimefromtp (ULONG ultime)
{
FILETIME ft;
SYSTEMTIME St;
Convert base time to 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"); Don't use 210.72.145.44 This IP, this is celestial's often TMD does not return a value
if (Socket_error =:: Connect (S, (sockaddr*) &servaddr, sizeof (SERVADDR))
{
printf ("Connect error ...");
Return
}
Received Time
ULONG ultime = 0;
int nrecv =:: Recv (S, (char*) &ultime, sizeof (Ultime), 0);
if (Nrecv > 0)//Receive data
{
Ultime =:: Ntohl (Ultime);
SETTIMEFROMTP (Ultime);
}
Else
{
printf ("recv error ...");
Return
}
:: Closesocket (s);
printf ("*******************************");
GetChar ();
}
I hope this article will help you with the C + + program design.