Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create a "TCP/IP relay" server?
(04-24-2021, 01:42 PM)BosseB Wrote: So now I wonder if there is a way to use Indy to make some kind of "relaying server" that could run on a Linux box with public IP to which the RPi4 remote server can connect and to which our client could also connect and specify which system it wants to talk to, whereupon the relaying server channels all traffic from the client over to the connected remote server and vice versa (if it is actually connected) so that both can start talking to each other using our already established specific protocol?

If the RPi4's IP is dynamic, you can accomplish this by using a combination of TIdMappedPortTCP with either TIdTCPServer (if you need multiple RPi4's) or TIdSimpleServer (if you need only 1 RPi4) on the relay server, and then having the RPi4 use a client socket to connect to the relay, and a server socket to accept clients.

Each RPi4 client would connect to the relay's TIdTCPServer/TIdSimpleServer so the relay can see the RPi4's current IP (in the AContext.Binding.PeerIP or TIdSimpleServer.Binding.PeerIP property, respectively) and save it somewhere for later use. The RPi4 can then disconnect at this point, and reconnect whenever its IP changes.

Then, outside clients can connect to the relay's TIdMappedPortTCP. You can use its OnConnect event to determine (such as by communicating with the client) which saved IP to forward to, and then assign that IP to the TIdTCPClient(TIdMappedPortContext(AContext).OutboundClient).Host property. When the event handler exits, TIdMappedPortTCP will then connect to the assigned IP, and forward data back and forth between the outside client and RPi4 for the lifetime of those connections.

On the other hand, if the RPi4's IP is static (or the RPi4 can publish its current IP somewhere the relay can read it from), then you don't really need the TIdTCPServer/TIdSimpleServer at all. Just provide your relay with the necessary IP(s) up front using some kind of configuration setup in the relay's code (or make the relay read the published info periodically).

(04-25-2021, 08:46 PM)BosseB Wrote: Can a socket server connect to this component and then use the other end of it as its own listening socket?

No. They would be independent listening sockets. The TIdMappedPortTCP would first accept an incoming connection from an outside client, and then create its own client socket to connect to the RPi4's server, and then pass data back and forth between the 2 connections.


Messages In This Thread
RE: How to create a "TCP/IP relay" server? - by rlebeau - 04-26-2021, 05:16 PM

Forum Jump:

Users browsing this thread: 2 Guest(s)