• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6088
  • 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);
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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