Search code examples
indyindy10

Tidtcpserver listening on multiports?


alright i am trying to understand this approach , lets say i have run 2 servers

Server A is on Ip 1.1.1.1 and port 36663 Server B is on ip 2.2.2.2 and port 54223

i am asking to be able to understand this approach

can i make clients on server A be able to communicate with clients on Server B ? as example a client is connecting on Server A and want to send some data to a client who connecting to server B is this can be done using indy tcp server ?

if the answer is yes an example will be much helpful to fully understand this approach .

i have 2 servers on different machine

1 machine have some slow network issue and the other have good network.

the logic here is when the client to serverA that takes more than 20 seconds to connect, during this 20 seconds try to reconnect to other server ip and be able to communicate with the client that already coonnected on serverA


Solution

  • TIdTCPServer has a Bindings property, which is a collection of IP/Port pairs that the server listens on. You can have a single TIdTCPServer object listening on multiple IP/Port pairs, or you can use multiple TIdTCPServer objects listening on different pairs, on the same machine.

    Either way, the connected clients are stored in the TIdTCPServer.Contexts property.

    When a client wants to send data to another client, regardless of which server IP/Port it is connected to, all you have to do is iterate through the Contexts list of the appropriate TIdTCPServer object until you find the TIdContext object of the target client, and then you will have access to its Connection.IOHandler property.


    On the other hand, if you have separate TIdTCPServer objects running on different machines, clients cannot directly communicate with clients on another server. You would have to establish a connection between the two servers and then you can proxy any client-to-client data through that connection as needed.