Asked  8 Months ago    Answers:  5   Viewed   34 times

Typical PHP socket functionality is synchronous, and halts the thread when waiting for incoming connections and data. (eg. socket_read and socket_listen)

How do I do the same asynchronously? so I can respond to data in a data received event, instead of polling for data, etc.



Yup, that's what socket_set_nonblock() is for. Your socket interaction code will need to be written differently, taking into account the special meanings that error codes 11, EWOULDBLOCK, and 115, EINPROGRESS, assume.

Here's some somewhat-fictionalized sample code from a PHP sync socket polling loop, as requested:

$buf = '';
$done = false;
do {
    $chunk = socket_read($sock, 4096);
    if($chunk === false) {
        $error = socket_last_error($sock);
        if($error != 11 && $error != 115) {
            my_error_handler(socket_strerror($error), $error);
            $done = true;
    } elseif($chunk == '') {
        $done = true;
    } else { 
        $buf .= $chunk;
} while(true);
Wednesday, March 31, 2021
answered 8 Months ago

I found out a function in think this works for you.

socket_getsockname($socket, $ip, $port);
echo "ip: ".$ip;
echo "port: ".$port;

Here $ip gives you the local addr and $port gives you a local port no.

Wednesday, March 31, 2021
answered 8 Months ago

Here's how to do it. You tell the browser to read in the first N characters of output and then close the connection, while your script keeps running until it's done.

header("Connection: close");
ignore_user_abort(); // optional
echo ('Text the user will see');
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush();     // Will not work
flush();            // Unless both are called !

// At this point, the browser has closed connection to the web server

// Do processing here

echo('Text user will never see');
Saturday, May 29, 2021
answered 5 Months ago

You can definitely do it, but it would take a bit of hackery. One way to do it would be to write a Windows command-line app that would send button clicks and such to the Windows GUI app. You then call this command-line app using PHP and the exec and system calls.

For example, if you were passing it say, Firefox, you would have a firefox-wrapper app that could be called like this:

firefox-wrapper open-url
firefox-wrapper press-back
Saturday, May 29, 2021
answered 5 Months ago

Find the folder you installed PHPUnit in. There should be a file called phpunit.bat. It should have a line that reads something like

set PHPBIN="C:phpphp.exe"
%PHPBIN% "C:phpphpunit" %*

Change it to read

set PHPBIN="C:xamppphpphp.exe"
%PHPBIN% "C:xamppphpphpunit" %*

Or whatever the path to your PHP executable is

Friday, August 13, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :