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.

 Answers

82

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;
        }
        break;
    } elseif($chunk == '') {
        $done = true;
        break;
    } else { 
        $buf .= $chunk;
    }
} while(true);
Wednesday, March 31, 2021
 
ojrac
answered 8 Months ago
22

I found out a function in php.net.I 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
 
Sagar
answered 8 Months ago
81

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.

<?php
ob_end_clean();
header("Connection: close");
ignore_user_abort(); // optional
ob_start();
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
include('other_script.php');

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

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 http://stackoverflow.com
firefox-wrapper press-back
...
Saturday, May 29, 2021
 
kwhohasamullet
answered 5 Months ago
27

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
 
Migwell
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 :
 
Share