I am creating a client-server application to run only on Windows OS, and each Windows user will connect to server at a time. The LAN uses DHCP, so IP is not fixed.
I need to send a string message from IdTCPServer to a specific IdTCPClient. In the beginning I was using a Listbox, so I added the hostname to the listbox when connected and remove when disconnect. At that time, Remy Lebeau give me this tip:
procedure TfrmMain.sendButtonClick(Sender: TObject);
var
Index: Integer;
Ctx: TIdContext;
begin
Index := ListBox.ItemIndex;
if Index = -1 then Exit;
Context := TIdContext(ListBox.Items.Objects[Index]);
// use Context as needed...
end;
But now I am using a ListView, with pre-added hostnames. So I just change the listbox item image status when clients connect or disconnect. Now I am trying something like this:
procedure TfrmMain.TCPServerConnect(AContext: TIdContext);
begin
TThread.Queue(nil,
procedure
var
Host: String;
LItem: TListItem;
begin
Host := UpperCase(GStack.HostByAddress(Ctxt.Binding.PeerIP));
LItem := lvwPCList.FindCaption(0, Host, False, True, False);
if (LItem <> nil) then LItem.Data := AContext.Data;
end
);
end;
And once I linked the Listview Item with the Context data, I am trying to send the message direct to client:
procedure TfrmMain.SendMessage(const Item: TListItem; const Msg: String);
var
Ctx: TIdContext;
begin
if (Trim(Msg) = '') then Exit;
Ctx := TIdContext(Item.Data);
try
Ctx.Connection.IOHandler.WriteLn(Msg);
except
end;
end;
I can compile this code, but the message never reachs client. Please, what I am doing wrong?
Thanks!
TIdServerContext. - Jerry DodgeTClientobject (which can uniquely identify a client connection). I think it's time to just give that object some sort of unique session identifier. - TLama