Using the Command Handler Method in Delphi - Implementing the Second and Third Commands
(Page 2 of 5 )
Now, we need to implement the second command. So repeat what you did before except this time, add "Adate" to the command property. Then, to the Oncommand event, add the following code:
procedure TForm1.IdCmdTCPServer1CommandHandlers1Command(
ASender: TIdCommand);
var
AFormat: string;
begin
AFormat := 'yyyy-mm-dd hh:nn:ss';
ASender.Context.Connection.IOHandler.WriteLn('Today''s date is:
'+FormatDateTime(AFormat, Now));
end;
Again, this is a straightforward procedure that sends today's date to the requesting client. The AFormat variable formats the date so that it is displayed in the form of year-month-day hour:mins:secs. The "Now" word in the line
ASender.Context.Connection.IOHandler.WriteLn('Today''s date is:
'+FormatDateTime(AFormat, Now));
gets the current date and time.
Repeat the process and let's implement the final command, which is to close down the connection. Add "quit" to the command property and to the Oncommand event add the following code:
procedure TForm1.IdCmdTCPServer1CommandHandlers2Command(
ASender: TIdCommand);
begin
ASender.Context.Connection.Disconnect;
end;
This procedure closes down the connection with this particular client. The other connected clients will still be connected. If you remember our previous discussions, you will recall that we used the term "context" a lot. This is because each client is dealt with, within its own context. Hence the "ASender.Context.Connection.Disconnect;".
Next: Setting the Greeting >>
More Delphi-Kylix Articles
More By Leidago