(* Collection of code snippets by Arne Vajhøj *)
(* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) *)
program UdpClient;

uses IdUDPClient, IdGlobal, SysUtils;

const
  SERVER = 'localhost';
  SERVER_PORT = 12345;
  CLIENT_PORT = 12346;
  SIZE = 7;
  MAX_SIZE = 100; (* 1000 not valid *)

var
  cli : TIdUDPClient;
  buf1, buf2 : TIdBytes;
  i, j, len : integer;

begin
  cli := TIdUDPClient.Create;
  cli.BoundPort := CLIENT_PORT;
  for i := 0 to 2 do begin
    (* setup data *)
    SetLength(buf1, SIZE);
    for j := 0 to Length(buf1) - 1 do begin
      buf1[j] := i + j;
    end;
    (* send packet *)
    cli.SendBuffer(SERVER, SERVER_PORT, buf1);
    (* receive packet *)
    SetLength(buf2, MAX_SIZE);
    len := cli.ReceiveBuffer(buf2);
    (* print data *)
    for j := 0 to len - 1 do begin
      write(' ' + IntToStr(buf2[j]));
    end;
    writeln;
  end;
  cli.Free;
end.

