Can PHP do port forwarding?

Posted on 2007-07-20
Last Modified: 2013-12-13
I have a client/server application.  The client application connects to the server on port 2356 like this:  connect  The server also has an IIS web server listening on port 80 with PHP installed.

I would like to create a PHP port forwarder script that will allow my client application to connect using port 80 like this: connect

The portfowarder.php script will act like a standard tcp/ip port fowarder connecting whatever is sent to the server application listening on port 2356.  

I am not an expert PHP person, so please try to be very specific.  Sample working code preferable.
Question by:frankmorrison
    LVL 5

    Expert Comment

    You could try something like this for the portforwarder.php file:

    header("Location: http://".$_SERVER['HTTP_HOST'].":2356");

    It does require the the client application to have full HTTP implementation, but you'll just have to test it to find out.
    LVL 3

    Accepted Solution

    Your client application must send correct http requests on port 80 to the php script so you should send it as a POST request. The php script will relay the data trough fsockopen();

    your php script should do something like this:
    // some validations an restrictions should go here
    $data_to_send = $_POST['data']; //should contain the exact data you need to send to the server

    $fp = fsockopen("localhost", 2356, $errno, $errstr, 30); //connects to the application listening on 2356
    if (!$fp) {
        echo "$errstr ($errno)<br />\n";
    } else {
        $out = $data;

        fwrite($fp, $out); // sends the data
    while (!feof($fp)) {
            echo fgets($fp, 128);

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    761 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

    7 Experts available now in Live!

    Get 1:1 Help Now