• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 451
  • Last Modified:

Threaded Port Redirection Code

Hello I am in need of a threaded port redirection program. I saw this code online http://www.overbyte.be/arch/socketspy.zip which uses the ICS internet components and it seems to work alright for what I need it to do but it's not threaded. Can anyone help me with this, if it's easier to use another socket component and achieve the same result that would be basically fine to. I just need the client and the server to be threaded so that my main application's thread is not bogged down. Thanks in advance and I will be very generous with the points when a solution is listed.  I am using Delphi 7.
2 Solutions
MerijnBSr. Software EngineerCommented:
I know there is something similar in Indy (which is threaded): http://www.indyproject.org/docsite/html/frames.html?frmname=topic&frmfile=TIdMappedPortTCP.html

did you take a look at that?
TheRealLokiSenior DeveloperCommented:
What data does your redirector need to handle?
is it HTTP web for example, FTP? or is it your own custom data?
If it's your own data, and you only need to redirect to 1 port, then you can use TIdMappedPortTCP as MerijnB says :-)

Otherwise, read on, and I'll explain an issue I had recently doing my own port redirection. You may find it interesting.

I wrote a port redirector using Indy (I tried native TClientSockety/Server without threads first, but ran into issues...ie.. really needed a thread due to large data),
I made it quite a bit more clever in that I could connect remotely first, and tell it that the next x connections (or x minutes) would be WEB requests that needed to be redirected to my webserver, then tell it that the next x connections (or x minutes) would be VNC (remote control) data to remote my server machine.
The reason I mention this, is that various protocols specify the addres in their requests
eg. if you look at a request in HTTP, you may see the server address and port.
Where this becomes an issue is your client is connecting to
which is redirected to myserver.com:80 (in reality, more likely to be
so a data request will look like this (this is vague, bear with me ;-) )
GET myserver.com:5080/index.html
now, this is redirected, but normally, the data remains intact.
so when my webserver sees GET myserver.com:5080/index.html it says, "well actually, im on port 80, your links are now broken"
There are other ways to handle HTTP, but not necessarily for other protocols.
What I had to do was actually change the data, and replace the host:port with the real server's host:port
The worst case I had of this was for my source version control, which used tcp messages including the server:port name as an ID. this absolutely needed to be changed.
I wrote a base class (in a thread) and wrote custom handlers for the various data types, http, source control, remote control, etc.

I used TIdTCPServer, and in the OnConnect evet, I created an object (with a new TidTCPClient that connected to the ACTUAL server) and assigned this object to the thread's "Data" property.
Then I used short ".ReadTimeout" calls
Connection.ReadFromStack(false, 1000);
and if there was data, I'd relay (or translate depending on the data type)
Check the incoming client and the new client we created, and relay each way.
All in all, it wasn't too dificult to write.

as i said, this may all be of no use to you :-), depending on your situation (you didn't supply your requirements in these things), but I figure too much info is better than not enough.

Forced accept.

EE Admin

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now