Threaded Port Redirection Code

Posted on 2007-10-01
Last Modified: 2012-06-27
Hello I am in need of a threaded port redirection program. I saw this code online 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.
Question by:OPDelphiCoder
    LVL 19

    Accepted Solution

    I know there is something similar in Indy (which is threaded):

    did you take a look at that?
    LVL 17

    Assisted Solution

    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 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 (in reality, more likely to be
    so a data request will look like this (this is vague, bear with me ;-) )
    now, this is redirected, but normally, the data remains intact.
    so when my webserver sees GET 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.

    LVL 1

    Expert Comment

    Forced accept.

    EE Admin

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
    In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now