Normally in a client/server setup, the client initiates contact and the server responds. Using the events exposed by the IdTCPServer this is always context (connection) specific so you wouldn't have to do anything special.
To initiate contact from the server to the client, you would have to keep track of connected clients and use the connection of the desired client to send it a message. To do so you need a list in which to hold the connected clients and need to implement handlers for the OnConnect and OnDisconnect events.
type
TForm1 = class(TForm)
private
FClients: TThreadList;
procedure TForm1.HandleClientConnect(aThread: TIDContext);
begin
FClients.Add(aThread);
end;
procedure TForm1.HandleClientDisconnect(aThread: TIDContext);
begin
FClients.Remove(aThread);
end;
When you want to send data to a specific client, you can do that using the normal methods for sending data over a TCP connection. But first you will need to find the specific client you need in your FClients list.
How you will identify specific clients is entirely up to you. It will depend entirely on the information you exchange between client and server when a client first connects and identifies itself. Having said that the mechanism will be the same regardless of that information.
TIDContext is the ancestor of the TIdServerContext class used by Indy to hold the connection details. You can descend from TIdServerContext to have a place where you can store your own details for a connection.
type
TMyContext = class(TIdServerContext)
private
// MyInterestingUserDetails...
Tell Indy to use your own TIdServerContext descendant using its ContextClass
property. You would of course need to do this before activating your server, for example in the OnCreate.
procedure TForm1.HandleTcpServerCreate(Sender: TObject);
begin
FIdTcpServer1.ContectClass = TMyContext;
end;
And then you can use your own class everywhere you have a TIdContext parameter by casting it to your own class:
procedure TForm1.HandleClientConnect(aThread: TIDContext);
var
MyContext: TMyContext;
begin
MyContext := aThread as TMyContext;
end;
Finding the connection of a specific client then becomes a matter of iterating over your FClients list and checking whether the TMyContext it contains is the one you want:
function TForm1.FindContextFor(aClientDetails: string): TMyContext;
var
LockedList: TList;
idx: integer;
begin
Result := nil;
LockedList := FClients.LockList;
try
for idx := 0 to LockedList.Count - 1 do
begin
MyContext := LockedList.Items(idx) as TMyContext;
if SameText(MyContext.ClientDetails, aClientDetails) then
begin
Result := MyContext;
Break;
end;
end;
finally
FClients.UnlockList;
end;
Edit: As Remy points out in comments: for thread safety you should keep the list locked while writing to the client (which is not such a good thing for throughput and performance) or, in Remy's words:
"A better option is to give TMyContext its own TIdThreadSafeStringList for outbound data, and then have that client's OnExecute event write that list to the client when it is safe to do so. Other clients' OnExecute events can then push data into that list when needed."