Network Programming - UDP messages
Sending broadcast messages with the IdUDPClient object
begin
sendString := Edit1.text;
setlength(sendData,length(sendString));
for i := 1 to length(sendString) do
begin
sendData[i-1] := lo(ord(sendString[i]));
end;
IdUDPClient1.Host := '255.255.255.255';
IdUDPClient1.Active := true;
IdUDPClient1.BroadcastEnabled := True;
IdUDPClient1.SendBuffer(sendData);
IdUDPClient1.BroadcastEnabled := False;
end;
Binary Array to ASCII String utility
function bintoAscii(const bin: array of byte): AnsiString;
var i: integer;
begin
SetLength(Result, Length(bin));
for i := 0 to Length(bin)-1 do
Result[1+i] := AnsiChar(bin[i]);
end;
Reading and displaying message contents using IdUDPServer
procedure TForm1.OnRead(AThread: TIdUDPListenerThread; const AData: TIdBytes;
ABinding: TIdSocketHandle);
var
receivedstring : string;
i : integer;
begin
receivedstring := '';
for i := 0 to length(Adata) do
begin
receivedstring := bintoascii(Adata);
end;
label1.Caption := (inttostr(length(Adata))+' received something:'+receivedstring+'<');
end;