indy10 UDP实例

UDP就比较简单了,放个按钮,一个TIdUDPServer
TIdUDPServer绑定 0.0.0.0:3820,然后Active设置为True

//发送按钮
procedure TForm1.Button1Click(Sender: TObject);
var
IdUDPClient1: TIdUDPClient; //动态创建TIdUDPClient
begin
IdUDPClient1:=TIdUDPClient.Create;
IdUDPClient1.BroadcastEnabled:=true;

try
IdUDPClient1.Port:=3820;
IdUDPClient1.Host:='127.0.0.1';

IdUDPClient1.Connect; //实际上就这三句
IdUDPClient1.Send('abcdefg.'); //2
IdUDPClient1.Disconnect; //3
finally
IdUDPClient1.Free;
end;
end;

//IdUDPServer1 绑定3820端口
//注意引用 IdSocketHandle, IdGlobal
procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TBytes;
ABinding: TIdSocketHandle);

begin
Memo1.Lines.Add(BytesToString(AData)); //把接收的信息加到Memo1
end;

posted @ 2013-12-06 23:26  白话  阅读(814)  评论(0编辑  收藏  举报