What's that CLI thing?
Can't I have the PHP Server script "running" on my browser?
The examples I've seen they seem to do it by having the Server running on a command line window or something like that, but it seems for linux, How is this exactly done? and can it been done on Windows XP?
For example I found this test code:
Code: Select all
<?php
/* Allow the script to hang around waiting for connections. */
set_time_limit(0);
/* Turn on implicit output flushing so we see what we're getting
* as it comes in. */
ob_implicit_flush();
//$address = '192.168.1.53';
$address = 'localhost';
$port = 1024;
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {
echo "<br>socket_create() failed: reason: " . socket_strerror($sock) . "\n";
}
echo "<br>sock = " . $sock;
if (!@socket_setopt($sock,SOL_SOCKET,SO_REUSEADDR,1)) {
echo "socket_setopt() failed: reason: ".socket_strerror(socket_last_error($sock))."\n";
exit;
}
if (($ret = socket_bind($sock, $address, $port)) < 0) {
echo "<br>socket_bind() failed: reason: " . socket_strerror($ret) . "\n";
}
echo "<br>bind = " . $ret . " sock = " . $sock . " address = " . $address . " port = " . $port;
if (($ret = socket_listen($sock, 5)) < 0) {
echo "socket_listen() failed: reason: " . socket_strerror($ret) . "\n";
}
echo "<br>listen = " . $ret . " sock = " . $sock;
/* Accept incoming requests and handle them as child processes */
$client = socket_accept($sock);
// Read the input from the client – 1024 bytes
//$input = socket_read($client, 1024);
// Strip all white spaces from input
//$output = ereg_replace("ї \t\n\r]","",$input).chr(0);
// Display output back to client
//socket_write($client, $output);
// Close the client (child) socket
//socket_close($client);
/*do {
*/
/* if (($msgsock = socket_accept($sock)) < 0) {
echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n";
break;
}
/* Send instructions. */
/* $msg = "\nWelcome to the PHP Test Server. \n" .
"To quit, type 'quit'. To shut down the server type 'shutdown'.\n";
socket_write($msgsock, $msg, strlen($msg));
do {
if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
echo "socket_read() failed: reason: " . socket_strerror($ret) . "\n";
break 2;
}
if (!$buf = trim($buf)) {
continue;
}
if ($buf == 'quit') {
break;
}
if ($buf == 'shutdown') {
socket_close($msgsock);
break 2;
}
$talkback = "PHP: You said '$buf'.\n";
socket_write($msgsock, $talkback, strlen($talkback));
echo "$buf\n";
} while (true);
socket_close($msgsock);
} while (true);
*/
socket_close($sock);
?>
I found several places with scripts that say that:
before you try to execute the script, make sure you have the right permissions on it:
chmod 755 myfile.php
Also make sure that the first line (#!/usr/local/bin/php -q) of the script points at your PHP binary. Then, start the server:
./myfile.php
Those are linux command line instructions, right? Can I test that script on Windows XP using my browser? how?
When I try the code above with the socket_accept line on, my browser just loads the page forever.. this is because it is waiting fo a client to connect, right?
why do I have to put this
Code: Select all
if (!@socket_setopt($sock,SOL_SOCKET,SO_REUSEADDR,1)) {
echo "socket_setopt() failed: reason: ".socket_strerror(socket_last_error($sock))."\n";
exit;
}
before the socket_bind(), otherwise it tells me that the port is already on use...?