0

I've got a script that currently creates a TCP socket via fsockopen() once per loop. Everytime it opens a connection, the entire PHP script pauses until it has been established.

I'm wanting to go down the path of running 5 simultaneous sockets so I can speed up the script.

Is there any way I can "que" an fsockopen connection? Ideally, It would look something like this pseudo code.

$opensocketcount = 0;

while (1) {

    if($opensocketcount < 5) {
        for($i=1;$i<6;$i++) {
            $sockets[$i] = fsockopen("127.0.0.1",80);
            $opensocketcount++;
        }
    }

    //Check to see if socket connection has been established
    for($i=1;$i<6;$i++) {
        if ( has_socket_been_established($sockets[$i]) ) {
            //Post the data.
            $opensocketcount -= 1;
            socket_close($sockets[$i]);
            $sockets[$i] = null;
        }
    }

}
Richard Denton
  • 982
  • 2
  • 7
  • 13

3 Answers3

2

PHP is not asynchronus. That means it pauses until the connection has been established. There are some approaches out there using shell scripts or CURL to multi-thread PHP but all this are not perfect solutions.

If you use this in the frontend you could go with AJAX, since that is asynchronus and call a the script like this "socket.php?con=1" etc. In the PHP script you would have an array with all IP's and the connect to them by the GET value. However this only works if you have this in the frontend. For a backend approach you would have to use sonething like NODE.JS

DAG
  • 6,710
  • 4
  • 39
  • 63
  • Ah thanks, what a shame. I was trying to avoid using CURL as the script is dealing with raw binary data. – Richard Denton May 19 '13 at 12:18
  • No the CURL is just used trigger the php script. In the php script triggerd you have to set the php.ini setting that the script will execute even when the connection is lost. This way you can set the CURL timeout to something really low and trigger 10 socket connection in less then second. But heads up! This will put additional load on the webserver since you are doing HTTP-Requests. – DAG May 19 '13 at 12:24
0

React could do this for you - be sure to check out the examples.

Anthony.

Anthony Sterling
  • 2,451
  • 16
  • 10
0

Since PHP does one thing at the time, for a similar task (pinging) I created a batch script that calls multiple php jobs using arguments.

So the loop is in the batch file. This also distributes the load on multiple CPUs, since jobs are spread. The batch read information for scanning from a CVS file that has 10 items per row (to run 10 processes at the time).

It is running on a windows server, but same can be applied on any.

here is the content of my batch file as an example:


@Echo Off For /F "usebackq tokens=1-10 delims=," %%a in ("D:\php_run\all_locations_10.cvs") Do (
echo Scanning: %%a
start "Scanning %%a" "C:\Program Files (x86)\PHP\v5.3\php.exe" "-f" "D:\php_run\wkstn_scan.php" "%%a"
echo Scanning: %%b start "Scanning %%b" "C:\Program Files (x86)\PHP\v5.3\php.exe" "-f" "D:\php_run\wkstn_scan.php" "%%b"
echo Scanning: %%c
start "Scanning %%c" "C:\Program Files (x86)\PHP\v5.3\php.exe" "-f" "D:\php_run\wkstn_scan.php" "%%c"
echo Scanning: %%d
start "Scanning %%d" "C:\Program Files (x86)\PHP\v5.3\php.exe" "-f" "D:\php_run\wkstn_scan.php" "%%d"
echo Scanning: %%e
start "Scanning %%e" "C:\Program Files (x86)\PHP\v5.3\php.exe" "-f" "D:\php_run\wkstn_scan.php" "%%e"
echo Scanning: %%f
start "Scanning %%f" "C:\Program Files (x86)\PHP\v5.3\php.exe" "-f" "D:\php_run\wkstn_scan.php" "%%f"
echo Scanning: %%g
start "Scanning %%g" "C:\Program Files (x86)\PHP\v5.3\php.exe" "-f" "D:\php_run\wkstn_scan.php" "%%g"
echo Scanning: %%h
start "Scanning %%h" "C:\Program Files (x86)\PHP\v5.3\php.exe" "-f" "D:\php_run\wkstn_scan.php" "%%h"
echo Scanning: %%i
start "Scanning %%i" "C:\Program Files (x86)\PHP\v5.3\php.exe" "-f" "D:\php_run\wkstn_scan.php" "%%i"
echo Scanning: %%j
start /WAIT "Scanning %%j" "C:\Program Files (x86)\PHP\v5.3\php.exe" "-f" "D:\php_run\wkstn_scan.php" "%%j"

)

Eki
  • 75
  • 9