Question

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;
        }
    }

}
Was it helpful?

Solution

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

OTHER TIPS

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

Anthony.

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"

)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top