本人想实现自己的系统能跟网站的时间同步。而不是跟本地计算机同步,这样可以预防别人通过修改本地计算机的时间来达到某种目的。究竟怎么样实现呢?谢谢!

解决方案 »

  1.   

    转贴
    -----form------   
      unit   Unit1;   
        
      interface   
        
      uses   
          Windows,   Messages,   SysUtils,   Variants,   Classes,   Graphics,   Controls,   Forms,   
          Dialogs,   StdCtrls,   IdTime,   ExtCtrls,   IniFiles;   
        
      type   
          TForm1   =   class(TForm)   
              procedure   FormCreate(Sender:   TObject);   
              procedure   FormCloseQuery(Sender:   TObject;   var   CanClose:   Boolean);   
          private   
          public   
          end;   
        
          TSyncTime   =   class(TThread)   
          private   
              FIdTimeObj   :TIdTime;   
              FHostIndex:   Integer;   
              function   SyncLocalTime:   Boolean;   
              constructor   Create(Suspend:   Boolean);   
          public   
              destructor   Destroy;   override;   
              procedure   Execute;   override;   
          end;   
        
      var   
          Form1:   TForm1;   
          SyncTime1:   TSyncTime;   
          FHostList:   TStringList;   
          FSychTime:   Integer;   
        
      implementation   
        
        
      {$R   *.dfm}   
        
      procedure   TForm1.FormCreate(Sender:   TObject);   
      var   
          Ini:   TIniFile;   
          I:   Integer;   
          S:   string;   
      begin   
          FHostList   :=   TStringList.Create;   
          Ini   :=   TIniFile.Create(ExtractFilePath(Application.ExeName)   +   'Setting.ini');   
          try   
              Ini.ReadSectionValues('Host',   FHostList);   
              FSychTime   :=   Ini.ReadInteger('Default',   'SychTimeSeq',   300);   
              for   I   :=   0   to   FHostList.Count   -   1   do   
              begin   
                  S   :=   FHostList[I];   
                  Delete(S,   1,   Pos('=',   FHostList[I]));   
                  FHostList[I]   :=   S;   
              end;   
              Unit1.SyncTime1   :=   TSyncTime.Create(False);   
          finally   
              Ini.Free;   
          end;   
      end;   
        
      procedure   TForm1.FormCloseQuery(Sender:   TObject;   var   CanClose:   Boolean);   
      begin   
          TerminateThread(SyncTime1.Handle,   0);   
          SyncTime1.Free;   
          FHostList.Free;   
          ExitThread(0);   
      end;   
        
      {   TSyncTime   }   
        
      procedure   TSyncTime.Execute;   
      begin   
          while   not   Terminated   do   
          begin   
              while   not   SyncLocalTime()   do   
              begin   
                  Inc(FHostIndex);   
                  if   FHostIndex   >=   FHostList.Count   then   FHostIndex   :=   0;   
                  Sleep(10000);   
              end;   
              Sleep(FSychTime   *   1000);   
          end;   
      end;   
        
      constructor   TSyncTime.Create(Suspend:   Boolean);   
      begin   
          inherited;   
          FHostIndex   :=   0;   
          FIdTimeObj   :=   TIdTime.Create(nil);   
      end;   
        
      destructor   TSyncTime.Destroy;   
      begin   
          Self.FIdTimeObj.Free;   
          inherited;   
      end;   
        
      function   TSyncTime.SyncLocalTime:   Boolean;   
      begin   
          Result   :=   False;   
          try   
              if   FHostList.Count   =   0   then   Exit;   
              Result   :=   True;   
              FIdTimeObj.Host   :=   FHostList[FHostIndex];   
              FIdTimeObj.SyncTime();   
          except   
              Result   :=   false;   
          end;   
      end;   
        
      end.   
      ---------setting.ini-------   
      [Default]   
      SychTimeSeq=300   
        
      [Host]   
      1=time.windows.com   
      2=time.nist.gov   
      3=time-nw.nist.gov   
      4=time-a.nist.gov   
      5=time-b.nist.gov   
      

  2.   

    转贴 或者使用Indy的这个也不错
    ------------------------
    使用TIdTime,就是如此简单:   
      unit   Unit1;   
        
      interface   
        
      uses   
          Windows,   Messages,   SysUtils,   Variants,   Classes,   Graphics,   Controls,   Forms,   
          Dialogs,   IdBaseComponent,   IdComponent,   IdTCPConnection,   IdTCPClient,   
          IdTime,   StdCtrls,   ExtCtrls;   
        
      type   
          TForm1   =   class(TForm)   
              IdTime1:   TIdTime;   
              Button1:   TButton;   
              Button2:   TButton;   
              Panel1:   TPanel;   
              Label1:   TLabel;   
              Label2:   TLabel;   
              Button3:   TButton;   
              Label3:   TLabel;   
              Edit1:   TEdit;   
              procedure   Button1Click(Sender:   TObject);   
              procedure   Button2Click(Sender:   TObject);   
              procedure   Button3Click(Sender:   TObject);   
          private   
              {   Private   declarations   }   
          public   
              {   Public   declarations   }   
          end;   
        
      var   
          Form1:   TForm1;   
        
      implementation   
        
      {$R   *.dfm}   
      //获取时间   
      procedure   TForm1.Button1Click(Sender:   TObject);   
      begin   
          IdTime1.Host:=Edit1.Text;   
          Label2.Caption:='网络时间:'+DatetimeToStr(IdTime1.DateTime);   
          Label1.Caption:='系统时间:'+DatetimeToStr(now);   
      end;   
      //校对时间   
      procedure   TForm1.Button2Click(Sender:   TObject);   
      begin   
          IdTime1.SyncTime;   
      end;   
        
      procedure   TForm1.Button3Click(Sender:   TObject);   
      begin   
          Close;   
      end;   
        
      end.
      

  3.   

    第二种办法我试过,总是提醒connecting timed out,还有你能不能给个大概用到组件是什么?
      

  4.   

    Edit1.Text??里面的内容是什么?我添加是time.windows.com ,但是连接不上。
      

  5.   

                             怎么老是提示超时,有谁知道怎么解决吗?或者告诉我连接什么网站好?我用的是time.windows.com?
      

  6.   

     '192.43.244.18',  // Time.nist.gov(NIST物理学实验室)
     '210.0.235.14',   // stdtime.gov.hk(香港)
     '207.46.130.100', // time.windows.com
     '203.117.180.36', // UTC(新加坡)
     '131.107.1.10',   // UTC(NIST)
     '64.236.96.53');  // time.asia.apple.com
      

  7.   

    谁可以帮我弄个工程发到我的QQ邮箱吗?因为无论我怎么修改都弄不出来.收到能运行即给分数!谢谢你们咯!邮箱:[email protected]
      

  8.   

    Indy 的官方 Demo ,我作了小小修改。
    已经发送了。
      

  9.   

    那个 DEMO 已经加上 香港 的服务器了,它也很快的,
    香港也是用北京时间的。
    关于北京方面有什么服务器以及IP地址我也不知道,网上搜索了几个也是不能用的。
    关于超时,有可能是你的网速问题,也有可能跟防火墙和网关有关的,这很难帮你解决,
    试一下尽量设置长一点的超时时间。
      

  10.   

    IdDemoTime.Timeout 尽量设置大一点,如600000 一分钟。
    如果这样还会超时,那很大可能是你网络的问题了。