I have a mobile client that will connect via TCP to a server. I'd like to set up a broker arrangement, where the client connects to the server on port 12345; the server authenticates the client and sends the client a socket to a server that handles that user's needs and tells the client to reconnect to the received socket. So instead of the client staying connected to 1.2.3.4:12345, I now want the client to connect to 1.2.3.4:23456.
Is there a way to do this within the TCP protocol? Or do I need to do custom programming on both sides of the connection?
Using TIdMappedPortTCP, I tried this:
procedure TForm3.idMapPortBeforeConnect(AContext: TIdContext);
begin
if Pos(AContext.Binding.PeerIP,'192.168.0.21') > 0 then
AContext.Binding.SetPeer('192.168.0.89',12345);
end;
This successfully changes the peer port to the desired port, but doesn't tell the client to reconnect. So the port mapping is still used.
There is no way to do what you want at the TCP layer. Once a connection is established, its endpoints cannot be changed. To do what you want, the server needs to send a message to the client telling it the new IP/port, then the client needs to make a new TCP socket and connect it to that IP/port