Skip to content

Instantly share code, notes, and snippets.

@omonien
Created April 13, 2023 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omonien/4f1fb4897651038809e8c83d65742bdf to your computer and use it in GitHub Desktop.
Save omonien/4f1fb4897651038809e8c83d65742bdf to your computer and use it in GitHub Desktop.
Indy TidTCPServer OnExecute sample that shows the idea how to process an incoming XML file
procedure TFormMain.ProcessXml(const AXml: string);
begin
var
LXmlDoc := TXMLDocument.Create(nil);
try
LXmlDoc.DOMVendor := DOMVendors.Find(OmniXML4Factory.Description);
LXmlDoc.LoadFromXML(AXml);
// if AXml is invalid, then an exception will be thrown
// Do further processing here
finally
FreeAndNil(LXmlDoc);
end;
end;
procedure TFormMain.TCPServerExecute(AContext: TIdContext);
begin
var
LXML := AContext.Connection.IOHandler.Readln;
try
ProcessXml(LXML);
AContext.Connection.IOHandler.WriteLn('ok');
except
on e: Exception do
begin
AContext.Connection.IOHandler.WriteLn('Error: No valid XML received - disconnecting!');
end;
end;
// Processing done
// Disconnect the client, and try to notify it.
// If you just "exit" here, then the connection will be keept - which is useful only if you
// want to send some feedback to the client at a later point in time, or if you expect the
// client to re-use the existing connection for further commands.
// This will disconnect us, but there is no way to tell if the client recognizes the disconnect.
AContext.Connection.Disconnect;
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment