pcntl_fork() question!

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I wish to write a little server script. It receives binary data and sends it  
back. Communication goes back and forth, so using apache is not possible.

My little script will be a shell script in php. I am thinking about having  
it invoked by xinetd instead of being a server running all the time. I  
looked at the source code of nanoweb, but couldn't really figure out how  
xinetd interacts with my script, in particular how it passes the socket on  
to my script.

A minimal script could be this:

#/usr/bin/php -q


$port = 9000;

$s_server = socket_create_listen($port);
$s_client = socket_accept($s_server);
while(true) {
    $rcv = socket_read($s_client, 1024);
    socket_write($s_client, 'echo: '.$rcv);
    if (substr($rcv, 0, 4) == 'quit')


The code above allows client to connect, type something (in reality it will  
be binary and the response will be something else) and get the data echoed,  
until the client types 'quit'.

This is single thread, and as long as the client is connected, no other  
client can connect.

the pcntl_fork() function is not available for me and recompiling it has  
proven difficult because it breaks current configuration (redhat 7.3, php as  
apache module and --enable-pcntl doesn't allow this in php 4.1.2, which is  
version that comes with redhat 7.3).

So, my questions are:

1) how can I make the above code work with xinetd? In particular, how does  
xinetd pass my server the connected socket?
2) can I make the code above accept multiple clients 'simultaneously'  
without the use of pcntl_fork() ?


Re: pcntl_fork() question!

Lisa Pearlson wrote:

Quoted text here. Click to load it

The socket is passed as the stdin/stdout file handles. Just open stdin://
from inside your script to read the data coming in.

Quoted text here. Click to load it

Easiest way is to just let xinetd startup new instances (wait=no) NB this
will only work with TCP (not UDP) connections. Trying to do anything else
with PHP using xinetd will turn ugly.

If you really want to handle the listening part / do strange ioctls on the
sockets then write your server to run standalone (i.e. not dependent on


Site Timeline