Seeking very simple TIdCommandHandler conditional response example

Go To StackoverFlow.com

2

Delphi XE2, so Indy 10.

My client sends a command which is processed by a TIdCommandHandler of my TIdCmdTCPServer.

I want to be able to perform some logic and return either a success or fail response and check for that back at the client.

Can someone please point me at a few lines of code as an example? Thanks in advance.

2012-04-05 00:18
by Mawg


2

Well, here's the simplest demo.

enter image description here

Add an IdCmdTCPServer to your form, and add one command, set its name in the Command property, I originally thought I should handle Response in OnCommand event like this:

procedure TForm1.IdCmdTCPServer1CommandHandlers0Command(ASender: TIdCommand);
begin
   //ASender.Response.Add('Hello');  // wrong way
   ASender.Reply.SetReply(0,'HELLO');

end;

Update Remy pointed out I shouldn't be using Response.

So you want to return success or failure, it's traditional to use a numeric result followed by the string value. Each string in the response strings list has an implied end-of-line transmitted back to the client:

procedure TForm1.IdCmdTCPServer1CommandHandlers0Command(ASender: TIdCommand);
begin
  if DoSomething then
    ASender.Reply.SetReply(0,'OK')
  else
    ASender.Reply.SetReply(999,'ERROR');

end;

The idea with the IdCommandHandler and a CmdTCPServer/Client is that you follow the "RFC" style of protocols, which are ANSI/ASCII text-based. An RFC-style internet protocol's reply is typically encoded over the wire as text with both a numeric and string value. ASender.Response could be used if you needed to take the content of a string list and return that as the response.

As for the client, a question here suggests that TIdCmdTcpClient is not the most natural way to build the client for this server. From their names, you'd have thought they were made for each other, but it's not exactly. For most simple TIdCMDTCPServers that you could build, you would find that a plain-vanilla TIdTCPClient is the simplest building block to start your client with.

2012-04-05 02:32
by Warren P
ASender.Response is the wrong property to use. Use ASender.Reply instead, eg: ASender.Reply.SetReply(123, 'FAILURE'). Sender.Response is meant for sending multi-line text that supplements the ASender.Reply. Then on the client side, use the TIdTCPClient.SendCmd() method and the TIdTCPClient.LastCmdResult property to send commands and retrieve the responses - Remy Lebeau 2012-04-05 04:24
+1 to both. Just to be 100% clear ... if I set ASender.Reply then that overrides the "normalResponse" ? And the client checks for it ... how? Thanks so much for the help, guys .. - Mawg 2012-04-05 06:01
oops, sorry, I see no that you already answered that. Thanks .. and the answe - Mawg 2012-04-05 07:00
Thanks Remy. Fixed - Warren P 2012-04-05 18:05
You know it would be cool if Delphi could auto-generate a skeleton handler for these events.... Wouldn't it? (VCL Event Handler Templates). - Warren P 2012-04-05 19:03
what a great idea ! - Mawg 2012-04-27 03:12
Ads