程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> Delphi2009的Indy全接觸之UDP篇(4)

Delphi2009的Indy全接觸之UDP篇(4)

編輯:Delphi

事實上,上述客戶端的TTimer控件是完全沒有必要使用的。

當我們對IdUDPClient使用Broadcast方法後,一旦與服務端通訊成功,IdUDPClIEnt自動會得到來自服務端的返回值。

如果通訊失敗,在IdUDPClIEnt.ReceiveTimeout所設定的事件後終止響應,所以我們可以吧延遲事件稍微設長一點。

於是客戶端代碼可以簡單的寫成:

1.unit ClIEntUnit;
2.
3.interface
4.
5.uses
6. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7. Dialogs, StdCtrls, ExtCtrls, IDBaseComponent, IdComponent, IdUDPBase,
8. IdUDPClIEnt, IdAntiFreezeBase, IdAntiFreeze;
9.
10.type11. TClIEntForm = class(TForm)
12. Label1: TLabel;
13. Edit1: TEdit;
14. Button1: TButton;
15. Label2: TLabel;
16. Edit2: TEdit;
17. IdUDPClient1: TIdUDPClIEnt;
18. IdAntiFreeze1: TIdAntiFreeze;
19. procedure Button1Click(Sender: TObject);
20. procedure FormCreate(Sender: TObject);
21. private
22. { Private declarations }
23. public
24. { Public declarations }
25. end;
26.
27.var
28. ClientForm: TClIEntForm;
29.
30.implementation
31.
32.{$R *.dfm}
33.
34.procedure TClIEntForm.Button1Click(Sender: TObject);
35.begin
36. IdUDPClIEnt1.Broadcast(Edit1.Text, 3030);
37. Edit2.Text := IdUDPClIEnt1.ReceiveString(-1);
38.end;
39.
40.procedure TClIEntForm.FormCreate(Sender: TObject);
41.begin
42. IdUDPClIEnt1.ReceiveTimeout := 5000;
43. Edit2.ReadOnly := True;
44.end;
45.
46.end.

如此即可完成與服務端的通訊。

=======================================================================

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved