We help IT Professionals succeed at work.

Can PHP do port forwarding?

Medium Priority
6,914 Views
Last Modified: 2013-12-13
I have a client/server application.  The client application connects to the server on port 2356 like this:  connect http://www.myserver.com:2356.  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 http://www.myserver.com/portfowarder.php 

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.
Comment
Watch Question

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

<?php
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.
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);
    }
    fclose($fp);
}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.