Ace code example: a small program that implements Computer Network Timing

Source: Internet
Author: User
Tags dns entry

Text/Jiang Tao

Application description

 

This code implements the RFC 868 client. Start the program and establish a TCP connection to the given time service. The time service returns a four-byte integer, which is the number of seconds from on January 1, January 1, 1900 to the past time, of course, it is in the order of network bytes. After the network bytes are converted to host bytes, we can read this number.

Currently, this program only supports windows. Because the modification time in Linux is inconsistent with that in Windows, you can modify the code on your own.

 

 

// Timeclient. cpp
// Platform: Win32

# Include "ACE/log_msg.h"
# Include "ACE/time_value.h"
# Include "ACE/OS. H"
# Include "ACE/date_time.h"
# Include "ACE/streams. H"
# Include "ACE/inet_addr.h"
# Include "ACE/sock_connector.h"
# Include "ACE/sock_stream.h"
# Include "ACE/get_opt.h"
# Include <string>
Using namespace STD;
# Ifdef Win32
# Include <windows. h>
# Endif
# If 0
// Some available server addresses
# Define numsrv 15
Char * serv_ip [numsrv] = {"64.236.96.53",/* nist1.aol-va.truetime.com */
"128.138.140.44",/* utcnist.colorado.edu */
"207.200.81.113",/* nist1.aol-ca.truetime.com */
"216.200.93.8",/* nist1-dc.glassey.com */
"63.149.208.50",/* nist1.datum.com */
"208.184.49.9",/* nist1-ny.glassey.com */
"207.126.103.204",/* nist1-sj.glassey.com */
"129.6.15.28",/* time-a.nist.gov */
"129.6.15.29",/* time-b.nist.gov */
"132133.4.101",/* time-a.timefreq.bldrdoc.gov */
"132133.4.102",/* time-b.timefreq.bldrdoc.gov */
"132133.4.103",/* time-c.timefreq.bldrdoc.gov */
"192.43.244.18",/* time.nist.gov */
"131.107.1.10",/* time-nw.nist.gov */
"Utcnist.colorado.edu"/* DNS entry = 128.138.140.44 */
};
# Endif

# Ifdef Win32
Void timettosystemtime (time_t T, lpsystemtime PST)
{
Filetime ft;
Longlong LL = int32x32to64 (T, 10000000) + 116444736000000000;
Ft. dwlowdatetime = (DWORD) ll;
Ft. dwhighdatetime = (DWORD) (LL> 32 );

Filetimetosystemtime (& ft, Pst );
}

Int setsystetime (const time_t & T)
{
Systemtime st;
Memset (& St, 0, sizeof (ST ));
Timettosystemtime (T, & St );
If (: setsystemtime (& St ))
{
: Sendmessage (hwnd_broadcast, wm_timechange, 0, 0 );
Return 0;
}
Return-1;

}
# Endif
Int ace_tmain (INT argc, ace_tchar * argv [])
{
Ace_sock_stream peer;
Ace_sock_connector conn;
Ace_uint32 tempvalue = 0;
Ace_inet_addr address;
// (37 ,);
String Server = "nist1-la.ustiming.org ";
Ace_get_opt get_opts (argc, argv, ace_lib_text ("s :? "));
Int ich;
While (ICH = get_opts ())! = EOF ){
Switch (ICH ){
Case/'s/':/* u specifies that UDP shoshould be used */
Server = get_opts.opt_arg ();
Break;
Case /'? /':/* T specifies that zero copy read shocould be used */
Cout <"Usage: timeclient-S //" nist1-la.ustiming.org // "" <Endl;
Return-1;

Default:/* no parameters */
Break;
}
}

Address. Set (37, server. c_str ());

If (-1 = conn. Connect (peer, address ))
{
Ace_error_return (lm_error,
"% P ",
"Connect"),-1 );
}
Ssize_t n = peer. Recv (char *) & tempvalue, 4 );
If (n> 0)
{
// Convert bytes from network to host

Tempvalue = ntohl (tempvalue );
Tempvalue-= 2208988800l; // * subtract 1970.0-1900.0 */
Time_t T (tempvalue );
If (setsystetime (t) = 0)
{
Cout <"adjtime OK." <Endl;
}
Else
{
Cout <"adjtime error" <Endl;
}
}

Peer. Close ();

Time_t lt;
Lt = Time (null );
Char * Ss = ctime (& lt );
Cout <"the system time" <SS <Endl;
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.