Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

Can PHP do port forwarding?

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.
1 Solution
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.
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);

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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