Running PHP from inetd
| From: | Colin Viebrock | Date: | Tue, 18 Apr 2000 17:20:40 +0000 |
| Subject: | Running PHP from inetd | ||
| Groups: | php.version4 | ||
| Request: | Send a blank email to php-version4+get-13682@lists.php.net to get a copy of this message | ||
Apologies if you've seen this question before, but I *know* someone has an
answer. :)
I am trying to use the CGI version of PHP to act as a kind of "server",
listening on a particular port, then firing up a script on input.
(I'm using PHP4, but I think the problem exists under PHP3 also).
First, here is the appropriate line from /etc/services:
php_listen 56789/tcp
And from /etc/inetd.conf:
php_listen stream tcp nowait nobody.tty /home/cmv/listen.phx
Finally, the script /home/cmv/listen.phx is at the end of this email.
Basically, everything works except that the output (writing to STDOUT) is
buffered, so the server responses aren't visible immediately. I've tried
using set_file_buffer(), but that generates a PHP error. I've also tried
fopen()-ing and fclose()-ing STDOUT for each write, but that generates more
PHP
errors. Finally, I've tried turning on implicit_flush in php.ini ... still
no go.
The script works great from the command line, so I'm thinking that maybe
there
is some problem with where PHP things STDOUT is?. Or maybe there is an
environment variable that I need to export? I don't know.
Has anyone else used PHP like this? Or can anyone give any suggestions on
how to get this working?
__________________________________________________________________
Colin Viebrock easyDNS Technologies
control your domain
http://www.easyDNS.com
/home/cmv/listen.phx:
#!/usr/local/bin/php -q
<?
$STDIN = fopen('php://stdin', 'r');
$STDOUT = fopen('php://stdout', 'w');
function sread(&$string) {
global $STDIN;
$x = $string = '';
while ($x!="\n") {
$x = fread($STDIN, 1);
$string.=$x;
}
$string = trim($string);
}
function swrite($string) {
global $STDOUT;
fwrite($STDOUT, $string);
if (substr($string,-1)!="\n") {
fwrite($STDOUT, "\n");
}
flush($STDOUT);
}
swrite('Hello!');
swrite('Say "bye" to quit.');
$input = '';
$loop = true;
while ($loop) {
sread($input);
switch (strtolower($input)) {
case 'bye':
$loop = false;
break;
default:
swrite ('You said "' . $input . '"!');
swrite ('Say something else');
break;
}
}
swrite ('Bye!');
fclose($STDIN);
fclose($STDOUT);
?>