首页 > OOP语言 > Delphi > 正文

Delphi2009的Indy全接触之UDP篇
2012-02-22 00:00:00   来源:www.bianceng.cn   评论:0 点击:

暂时无描述信息

首先新建服务端。

如下图所示建立工程:

\

代码如下:

1.unit ServerUnit;
2.
3.interface
4.
5.uses
6. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7. Dialogs, IdBaseComponent, IdComponent, IdUDPBase, IdUDPServer, StdCtrls,
8. IdSocketHandle, IdGlobal;
9.
10.type
11. TServerForm = class(TForm)
12. Label1: TLabel;
13. Edit1: TEdit;
14. Label2: TLabel;
15. Edit2: TEdit;
16. Label3: TLabel;
17. Edit3: TEdit;
18. IdUDPServer1: TIdUDPServer;
19. procedure IdUDPServer1UDPRead(AThread: TIdUDPListenerThread; AData: TBytes;
20. ABinding: TIdSocketHandle);
21. procedure FormCreate(Sender: TObject);
22. private
23. { Private declarations }
24. public
25. { Public declarations }
26. end;
27.
28.var
29. ServerForm: TServerForm;
30.
31.implementation
32.
33.{$R *.dfm}
34.
35.procedure TServerForm.FormCreate(Sender: TObject);
36.begin
37. IdUDPServer1.DefaultPort := 3030;
38. IdUDPServer1.Active := True;
39.
40. Edit1.ReadOnly := True;
41. Edit2.ReadOnly := True;
42. Edit3.ReadOnly := True;
43.end;
44.
45.procedure TServerForm.IdUDPServer1UDPRead(AThread: TIdUDPListenerThread;
46. AData: TBytes; ABinding: TIdSocketHandle);
47.begin
48. Edit1.Text := ABinding.PeerIP;
49. Edit2.Text := IntToStr(ABinding.PeerPort);
50. Edit3.Text := BytesToString(AData);
51.
52. IdUDPServer1.Send(ABinding.PeerIP, ABinding.PeerPort, TimeToStr(Time) + ' => Server received the message!');
53.end;
54.
55.end.

相关热词搜索:

上一篇:Delphi2009的Indy全接触之TCP篇
下一篇:Delphi2009中使用dbExpress组件访问MySql(登陆页面)

分享到: 收藏