Topic Path: Symbol Reference > Classes > TIdTCPServer Class > TIdTCPServer Properties > OnExecute Event
TIdTCPServer.OnExecute Event

Event handler used to execute the task for a client connection.

property OnExecute;

OnExecute is a TIdServerThreadEvents event handler used to execute the task for a client connection to the server. OnExecute is signalled when the Scheduler executes the thread or fiber associated with the client connection. 

OnExecute receives a TIdContext argument that represents the task for the client connection, and contains a TIdTCPConnection instance for the client connection, and the thread or fiber that controls execution of the task. A protected procedure in the server that triggers OnExecute is assigned to the TIdContext, and eventually the thread or fiber acquired from the Scheduler for the server. 

Use OnExecute to control the interaction between the client and the server during the lifetime of the client connection. This includes reading and/or writing commands and/or data supported in exchanges between the client and server. TIdTCPServer does not implement support for any given protocol used for communication exchanges between clients and the server. 

When the thread or fiber is running, the OnExecute event handler is triggered inside a loop that continues to execute the event handler until the client is disconnected. This allows the OnExecute event handler procedure to be written in a straight-forward manner, and still yield to the Scheduler for the server. 

The event handler can be used to interact with the client connection by read a command and/or writing a response dependent on the protocol in use for the session. When the command and/or response is handled, the event handler should be exited to allow the Scheduler to respond other client connections to the server. Using loops or polling inside the OnExecute event handler should be avoided to reduce the chance of interferring with the Scheduler for the server. 

The preferred way to interrupt execution of the looped OnExecute event handler is to call the TIdContext.Connection.Disconnect method for the context instance. 

For example: 


  TMyForm.MyServerExecute(AContext: TIdContext);
    lCmd: string;
    lCmd := Trim(AContext.Connection.IOHandler.ReadLn);
    if AnsiSameText(lCmd, 'HELP') then

    else if AnsiSameText(lCmd, 'QUIT') then

    else if AnsiSameText(lCmd, 'GETTIMESTAMP') then
          FormatDateTime(Now, 'yyyy-mm-ddThh:nn:ss.zzz'));


Exceptions raised in the OnExecute event handler are caught in the executing thread or fiber. If the exception cannot be handled by the thread or fiber, it is re-raised and causes termination of the thread or fiber. 

Use OnConnect to perform actions for the client after it is connected and prior to execution in the OnExecute event handler. 

Use OnDisconnect to perform actions for the client after is has been disconnected, and exited from the OnExecute event handler. 

Assign a TIdServerThreadEvent event handler procedure to OnExecute to respond to the event notification.


Copyright 1993-2006, Chad Z. Hower (aka Kudzu) and the Indy Pit Crew. All rights reserved.
Post feedback to the Indy Docs Newsgroup.