proc_open and sockets

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I'm trying to build an interface in PHP to open prolog and feed it commands
and return answers without shutting it down. (I think you'd call this
running prolog as a daemon) The code below does two things. It opens prolog,
feeds in commands and pulls out the answers then closes the prolog. It then
opens a socket and listens for commands and prints out the command that was
sent until an 'END'  command is sent. However, if you try to do it in this
order... 

Open prolog
Open socket
while (command not 'END')
   Listen for commands
   Send command to prolog
   Return answers down socket
Close socket
Close prolog

It just hangs, won't open the socket unless prolog is closed. Does anyone
know loads about sockets and processes who have any ideas why that happens?
The below code works but not how I want it to.

<?php

set_time_limit (0);


$descriptorspec = array(
   0 => array("pipe", "r"),  
   1 => array("pipe", "w"),
   2 => array("pipe", "w") 
);

$cwd = null;
$env = array('options'=>'');

$process = proc_open('swipl', $descriptorspec, $pipes, $cwd, $env);

if (is_resource($process)) {

    fwrite($pipes[0], " ['/var/www/dev/tools/e2et'].");
    fflush($pipes[2]);
    fread($pipes[2],1024);
    fwrite($pipes[0], " skip(1).");
    fflush($pipes[2]);
    $data = fread($pipes[2],1024);

    fclose($pipes[0]);
    fclose($pipes[1]);
    fclose($pipes[2]);
    proc_close($process); 

echo $data . "<hr />";

$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create
socket\n");
$result = socket_bind($socket, '127.0.0.1', 9993) or die("Could not bind to
socket\n");
$result = socket_listen($socket, 5) or die("Could not set up socket
listener\n");
$spawn = socket_accept($socket) or die("Could not accept incoming
connection\n");

do {
	$input = socket_read($spawn, 8, PHP_NORMAL_READ) or
die(socket_close($spawn));
	if (trim($input) == "END"){
		socket_close($spawn);
		break;
	}
	else {
		echo $input;
	}

} while (true);
socket_close($socket);

}
?>


I guess you're wondering why I'm bothering - it's just to avoid having to
recompile the prolog code that I want to run each time I want to query it
via a web server.
-- 
View this message in context: http://www.nabble.com/proc_open-and-sockets-tp20305352p20305352.html
Sent from the PHP - General mailing list archive at Nabble.com.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux