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

uses IdUDPServer, IdGlobal, SysUtils;

const
  SERVER_PORT = 12345;
  MAX_SIZE = 100; (* 1000 not valid *)

var
  srv : TIdUDPServer;
  buf1, buf2 : TIdBytes;
  from : string;
  fromport : word;
  j, len : integer;

begin
  srv := TIdUDPServer.Create;
  srv.DefaultPort := SERVER_PORT;
  while true do begin
    (* receive packet *)
    SetLength(buf1, MAX_SIZE);
    from := '0.0.0.0';
    fromport := 0;
    len := srv.ReceiveBuffer(buf1, from, fromport);
    (* work on data *)
    SetLength(buf2, len);
    for j := 0 to len - 1 do begin
      buf2[j] := buf1[j] + 1;
    end;
    (* send packet *)
    srv.SendBuffer(from, fromport, buf2);
  end;
  srv.Free;
end.

