|
Posted by Lisa Pearlson on 11/15/78 11:24
Hi,
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
<?php
$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')
break;
}
socket_close($s_client);
socket_close($s_server);
?>
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() ?
Lisa
Navigation:
[Reply to this message]
|