socket_listen loops - program termination

Currently there is not such a feature but I’ll try to add it to the new script engine we’re working on.
BTW you can try to call PHP scripts with some AJAX query: they could be run asynchronously.

Thanks again, but one small point, do you know if there is a way of using HEscript to execute another php script stored in the application in the background - an asynchronous execution of a php script ? I know there is a Macro function for executing another executable program, but couldn’t find one for php scripts themselves. If not, if it could be added, it would radically enhance the compiler functionality.

Thanks for the follow-up. I also responded to your PM.

Hi thanks for the reply, and you can ignore the email sent, accept for the possible request of the skinbuilder. I’ll send a PM message about that.

Actually I found a solution, using socket_set_nonblock(). I suspected it wouldn’t work, as nonblock sockets are known to have problems with the normal windows version of PHP, but I was delighted to find out that with Exeoutput for PHP it worked like a charm :slight_smile:

I was initially wrong, the pause issue was at the accepting connection side, but with non blocking, that is no longer an issue.

Thanks again for responding. For notes to other, using HEScript in blocking mode to execute another script to send a packet to the main socket doesn’t appear to work as ExeOutput seems to lock down and prevents the other php from running … so the only solution I have found is to use socket nonblocking and testing to see if the application has been shutdown - if (exo_getglobalvariable(“isappterminated”, “0”) == “1”) { … }

**One possible enhancement for HEscripting side would be a an asynchronous script execution macro function - ie executes a php script in the background ? **
I am assuming the current HEscript macro to execute commands is just for executables and not php scripts, unfortunately manual wasn’t too clear to me.

eg.

// Create a TCP Stream socket

$sock = socket_create(AF_INET, SOCK_STREAM, 0);

// important to prevent activate pauses

socket_set_nonblock($sock);

// Bind the socket to an address/port

decho(“Binding socket to localhost port 9000”);

flush();

socket_bind($sock, $address, $port) or die(‘Could not bind to address’);

// Start listening for connections

socket_listen($sock);

$istop=0;

while($istop==0) {

/* Accept incoming requests - with none blocking there is no pause or waiting to accept client connections */

if ($client = socket_accept($sock) ) {


The problem lies in socket_listen. It blocks the execution of a loop until it receives some data. So until some data is sent, a while() loop in PHP is not executed.
The sole possibility is to terminate the application manually OR to send some data to socket_listen so that it returns to the while() loop.
I have kept some workaround from another customer. Please contact me by email or PM so we can see if it fits for your own program.

I have tried using socket blocking, but doesn’t appear to have any effect, nor are setting socket option time outs.

Ignore this part - I got socket none blocking working (see my response below) - I used the wrong variable - should have been $sock and not $socket :frowning:

Hi, I have developed a simple prototype application using ExeOutput for PHP which will be used to link a client’s commercial reporting package with a remote database using standard socket ports - using localhost on port 9000.

Client reporting package <=> ExeOutput Application 127.0.0.1:9000 <= web api port 80 <= server mysql port 3306

The application itself works fine, accept when I go to terminate the application. It works by having a loop of socket_listen, socket_read and socket_output to respond to client requests, unfortunately when it goes to the socket_read command,and I then terminate the application, in the background it is still listening… and hence doesn’t shutdown correctly (not a loop issue as such). I know there are built in variables for testing if the application has closed, but in this scenario it isn’t of much use as the program waits on one command for response from the client application (which may never come if the client application is shut down as well). In traditional programming, the solution would be a call back routine, but php sockets doesn’t seem to support call backs.

I would be grateful if anyone had an idea how to shutdown the application completely when it is waiting on a socket_listen / socket_read.

One idea of a possible solution, but not sure how to implement it (even if possible), is if I can somehow override the ExeOutput exit command so it calls a php function or script first, just prior to exiting - this is so that a secondary exit php script could send a packet to the main applications socket_read, set a variable that the application has finished to prevent the main application re-looping (in fact already has the global variable test in place for the standard exit, which is why this whole problem isn’t a loop issue), and then exits the application as normal. Is it therefore possible to get ANY exit call to call php scripts/functions first, before actually attempting to shutdown the application ?

Thanks for any help given.

1 Like