现在的位置: 首页 > 综合 > 正文

ACE代码示例:实现计算机网络授时的小程序

2013年01月10日 ⁄ 综合 ⁄ 共 2923字 ⁄ 字号 评论关闭

文/江涛

应用说明

 

本代码实现了RFC 868 客户端。启动程序,向给定的时间服务建立TCP连接,时间服务返回4个字节的整数,这个数定是从1900年1月1日零点到当前时间过去的秒数,当然它是网络字节的顺序的,通过网络字节到主机字节转换后,我们就可以读出这个数字。

本程序暂只支持Windows,因为linux下的修改系统时间和Windows的接口不一致,linux下的同学可以自行修改代码。

 

 

//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
//可用的部分服务器地址
#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*/
"132.163.4.101"  ,/*time-a.timefreq.bldrdoc.gov*/
"132.163.4.102"  ,/*time-b.timefreq.bldrdoc.gov*/
"132.163.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 should be used */
              server  = get_opts.opt_arg();
              break;
          case /'?/': /* t specifies that zero copy read should 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)
    {
        //字节序从网络转为主机

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

抱歉!评论已关闭.