然後新建客戶端如下圖所示:
注意:為了讓客戶端能實時監控服務端發回來的確定消息,使用了TTimer控件,Interval設置為250毫秒。
代碼如下:
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;
9.
10.type
11. TClIEntForm = class(TForm)
12. Label1: TLabel;
13. Edit1: TEdit;
14. Button1: TButton;
15. Label2: TLabel;
16. Edit2: TEdit;
17. IdUDPClient1: TIdUDPClIEnt;
18. Timer1: TTimer;
19. procedure Button1Click(Sender: TObject);
20. procedure FormCreate(Sender: TObject);
21. procedure Timer1Timer(Sender: TObject);
22. private
23. { Private declarations }
24. public
25. { Public declarations }
26. end;
27.
28.var
29. ClientForm: TClIEntForm;
30.
31.implementation
32.
33.{$R *.dfm}
34.
35.procedure TClIEntForm.Button1Click(Sender: TObject);
36.begin
37. IdUDPClIEnt1.Broadcast(Edit1.Text, 3030);
38. Timer1.Enabled := True;
39.end;
40.
41.procedure TClIEntForm.FormCreate(Sender: TObject);
42.begin
43. Timer1.Enabled := False;
44.
45. Edit2.ReadOnly := True;
46.end;
47.
48.procedure TClIEntForm.Timer1Timer(Sender: TObject);
49.begin
50. Edit2.Text := IdUDPClIEnt1.ReceiveString(-1);
51. Timer1.Enabled := False;
52.end;
53.
54.end.
如此實現簡單的UDP協議下局域網通訊。
===================================================