Do I still CLI version of PHP [message #177081] |
Sun, 19 February 2012 07:13 |
sl@exabyte
Messages: 16 Registered: March 2011
Karma:
|
Junior Member |
|
|
On my webserver:
*PHP version is 5.3.6
*server API: CGI/FastCGI
I need to run a socket server. A sample snippet is below:
#!/usr/local/bin/php -q
<?php
//file name: server_socket.php
......
while (true) { // Loop continuously
// Setup clients listen socket for reading
$read[0] = $sock;
$ready = socket_select($read,null,null,null);
// If a client is trying to write - handle it now
for ($i = 0; $i < $max_clients; $i++) // for each client
{
if (in_array($client[$i]['sock'] , $read))
{
$input = socket_read($client[$i]['sock'] , 1024);
if ($input == null)
unset($client[$i]); // Zero length string meaning disconnected
$n = trim($input);
if ($input == 'exit') {
socket_close($client[$i]['sock']); // requested disconnect
} elseif ($input) {
// strip white spaces and write back to user
$output = ereg_replace("[ \t\n\r]","",$input).chr(0);
socket_write($client[$i]['sock'],$output);
}
} else {
socket_close($client[$i]['sock']);
unset($client[$i]);
}
}
} // end while
socket_close($sock);
?>
Questions:
1. Do still need the line '#!/usr/local/bin/php -q' is I call the file via
http, eg http://domain/server_socket.php, because the program is looping ?
2. If the answer to Q1is yes, how do I run my server program (I think the
version on my server is CGI) ?
Thanks
|
|
|