delphiindytcpserverduplex

Send and receive simultaneously with TIdTCPServer


It is always advised to perform all the sending / receiving tasks in OnExecute event handler of TIdTCPServer, but I do not understand following:

How to wait for a specific sequence on input and at the same time send some data to the same client? I need not a command-response sequence, but I need to:

For example, if we are waiting for CR-LF:

procedure TSocketServer._serverExecute(AContext: TIdContext);
var
  msg: string;
begin
  msg := AContext.Connection.IOHandler.ReadLn();
  //Here we are only if CRLF was detected.
  //How to send while we are waiting?
  _log(msg);
end;

Solution

  • It is important that when sending unsolicited data and response data using the same connection, don't overlap the outgoing messages, or else you will corrupt your protocol. It is best to have only 1 thread do all of the sending so that one message gets sent in full before another message is sent. Just make sure you design your protocol to allow unsolicited data to be sent after the client sends a command and before it receives a response. Each message should describe what kind of message it is, in such a way that the client can detect a response and match it to an earlier command, while handling unsolicited data as-is.

    There is a few different ways you can handle the sending: