Simple Network Programming Application (Update Time)

Source: Internet
Author: User

# Include <iostream>
# Include <stdio. h>
# Include <windows. h>
# Include <string. h>

Using namespace STD;

# Pragma comment (Lib, "ws2_32.lib ")

Class cinitsock
{
Public:
Cinitsock (INT version01 = 2, int versiont02 = 2)
{
Word sockversion = makeword (2, 2 );
Wsadata = {0 };

If (wsastartup (sockversion, & wsadata )! = 0)
{
Cout <"initlization failed! "<Endl;

Exit (0 );
}

}

~ Cinitsock ()
{
Wsacleanup ();
}
};

Cinitsock mysock;

Void settimefromtp (ulong ultime)
{
Filetime ft;
Systemtime st;

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 );
}

Int main ()
{
Socket S = socket (af_inet, sock_stream, ipproto_tcp );

If (S = invalid_socket)
{
Cout <"faied socket" <Endl;

Return 0;
}

Sockaddr_in sin;

Sin. sin_addr.s_un.s_addr = inet_addr ("129.6.15.28 ");
Sin. sin_family = af_inet;
Sin. sin_port = htons (37 );

If (connect (S, (sockaddr *) & sin, sizeof (SIN) =-1)
{
Cout <"failed connect..." <Endl;
Return 0;
}

Ulong ultime = 0;

Int nrecv = Recv (S, (char *) & ultime, sizeof (ultime), 0 );

If (nrecv> 0)
{
Ultime = ntohl (ultime );
Settimefromtp (ultime );

Cout <"Update time successfully! "<Endl;
}
Else
{
Cout <"can't update time..." <Endl;
}

Closesocket (s );

Return 0;
}

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.