(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组件
最新推荐文章于 2019-02-06 19:24:50 发布