(1) unit U_UDPTime_Client;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdUDPBase, IdUDPClient, IdTimeUDP,
StdCtrls;
type
TF_UDPTime_Client = class(TForm)
Button1: TButton;
Combobox1: TComboBox;
lblTimeServer: TLabel;
Label1: TLabel;
Edit1: TEdit;
IdTimeUDP1: TIdTimeUDP;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
F_UDPTime_Client: TF_UDPTime_Client;
implementation
{$R *.dfm}
procedure TF_UDPTime_Client.Button1Click(Sender: TObject);
begin
try
//设置Time服务器的地址
IdTimeUDP1.Host := Combobox1.Text;
//显示时间
Edit1.Text := DateTimeToStr ( IdTimeUDP1.DateTime );
except
Showmessage('获取时间操作失败,请检查Time服务器是否活跃!');
end;
end;
end.
(2)unit U_UDPTime_Server;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdUDPBase, IdUDPServer,
IdTimeUDPServer, StdCtrls,IdSocketHandle;
type
T_UDPTime_Server = class(TForm)
Label1: TLabel;
Label2: TLabel;
IdTimeUDPServer1: TIdTimeUDPServer;
procedure FormActivate(Sender: TObject);
procedure IdTimeUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
private
{ Private declarations }
public
{ Public declarations }
end;
var
_UDPTime_Server: T_UDPTime_Server;
AccessTimes:integer;
implementation
{$R *.dfm}
procedure T_UDPTime_Server.FormActivate(Sender: TObject);
begin
try
//激活TIdECHOServer组件
IdTimeUDPServer1.Active := True;
AccessTimes :=0;
except
ShowMessage('拒绝连接!由于安全的原因无法绑定到预定的端口!');
Application.Terminate;
end;
end;
procedure T_UDPTime_Server.IdTimeUDPServer1UDPRead(Sender: TObject;
AData: TStream; ABinding: TIdSocketHandle);
begin
AccessTimes :=AccessTimes +1;
Label2.Caption :='['+inttostr(AccessTimes)+' 次]';
end;
end.
应用IdTimeUDPServer和TIdTimeUDP组件
最新推荐文章于 2018-10-06 14:24:24 发布
本文介绍了一个基于UDP协议的时间同步系统实现,包括客户端与服务器两端的设计与开发过程。客户端能够从服务器获取精确时间,并更新本地时间显示;服务器端则记录时间请求次数。
759

被折叠的 条评论
为什么被折叠?



