Link to home
Start Free TrialLog in
Avatar of Jebtrix
Jebtrix

asked on

2 Indy TCPServer Instances: I need to forward data from one to the other

-Using Delphi 7-
Level: Novice

2 TCPServer Instances in one app. Two different computers will be connected to each TCPServer.
The big picture is I want to forward all commands/data from one client to the other. Unfortunately these components work on command/response (OnExecute event). How would I just pass the client commands between TCPServer instances. The response is givin by another application, so I need this to be a two way dumb 'tunnel'. ANY input would be greatly appreciated.
Avatar of JimMcKeeth
JimMcKeeth

Would the TIdMappedPortTCP or TIdTunnelMaster & TIdTunnelSlave combo be what you are looking for?  

Is there going to be more then one connection to each server?  If so then set the MaxConnections to 1 and you can use some code like this:

var
  FConnection1: TidTCPServerConnection;
  FConnection2: TidTCPServerConnection;

procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
  FConnection1 := AThread.Connection;
end;

procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
  s: String;
begin
  s := AThread.Connection.ReadLn;
  FConnection2.writeln(s);
end;

procedure TForm1.IdTCPServer1Disconnect(AThread: TIdPeerThread);
begin
  FConnection1 := nil;
end;

For each TCPServer.  Notice that the Execute for 1 writes to the connection #2.

That is untested, but if you have trouble, and this is in fact what you are looking for just let me know.
Avatar of Jebtrix

ASKER

Works like a charm. However multi client support is something I also need. I need all multiple clients commands to be forwarded down the other 1 TCPServer connection. Because of this I'll have to add some kind of identifier to each client commands so I can parse it out and respond accordingly.

On the other side of this I have another app with a TCPClient. This connects to the TCPServer on the previous app. It will receieve all forwarded client commands. When it sees a new client cmd identifier, I need it to dynamically create a new TCPClient instance, which connects to a 3rd (local)commercial server.

Basically its a server app that funnels client sessions to another connected app, which in turn spawns appropriate client sessions and forwards to a 3rd (local) commercial app.

Thanx for your time :)
Would you like a code example of keeping track of the two connections and keeping them linked together?
Avatar of Jebtrix

ASKER

Yes please. :)
Just so I understand exactly what you want before I write the code:

There are two server sockets, the 1st socket will have only one connection.  The 2nd socket will have many connections and anything that comes in those connections will be sent back down the 1st socket.  And anything received on the 2nd socket will be broadcast to all the connections on the first socket?

How do you want it to behave if a connection comes in on the second socket and there is no connection on the first socket?  Should the second socket only start listening when the first socket has a connection and then stop if the first socket ever looses it's connection?  
Avatar of Jebtrix

ASKER

Q:
..anything received on the 2nd socket will be broadcast to all the connections on the first socket?

A:
Not all the connections on the first socket. An identifier tag will need to be added so on my client app it can parse each clients' commands and forward to appropriate invoked instance. This also works in reverse, the 1st server socket must parse responses and forward to appropriate client sockets.

I made a diagram that should clarify my intentions:

http://users.rcn.com/nemizis/AppDiagram.jpg

-Thanx
Avatar of Jebtrix

ASKER

Almost forgot, yes the 2nd socket will only listen when the 1st socket is connected. And for now yes if connection is lost 2nd socket will disconnect all clients. I will add some client cmd caching and some kind of 1st socket connection recover scheme later.
Ok, I think I have something that will work for you.  It is an object that keeps a list of TIdTCPServerConnection and string identifiers.  Just add them to the list object and then you can get the matching one later.  You can add just one (like if you only have a connection and no identifier yet (like on the OnConnect event).  So then you later call the Update method and it will sync those up.  It is pretty simple I hope.  No comments, those cost extra ;-) but if you have questions feel free to ask.

ASKER CERTIFIED SOLUTION
Avatar of JimMcKeeth
JimMcKeeth

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jebtrix

ASKER

Very nice work thank you. :) Only thing left is to figure out why disconnect method sometimes locks the application, and delphi for that matter; disconnectsocket works fine though.