I am trying to make a socket server that serves the sandbox security file back to flash. But I am having problems. I can’t get the socket server to send back a message when it receives the string “<policy-file-request/>”. Below I have code that should write back “Blah Blah blah” back to the server, but nothing is happening. It does disconnect when the code gets “<policy-file-request/>” but it doesn’t send any message.
Any idea what I’m doing wrong.
#!/usr/local/bin/php -q
<?php
error_reporting(E_ALL);
/* 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 = 'localhost';
$port = 7777;
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "
";
}
if (socket_bind($sock, $address, $port) === false) {
echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "
";
}
if (socket_listen($sock, 5) === false) {
echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "
";
}
do {
if (($msgsock = socket_accept($sock)) === false) {
echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "
";
break;
}
else
{/* Send instructions. */
$msg = "
Welcome to the PHP Test Server.
" .
"To quit, type 'quit'. To shut down the server type 'shutdown'.
";
socket_write($msgsock, $msg, strlen($msg));
}
do {
if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "
";
break 2;
}
if (!$buf = trim($buf)) {
continue;
}
if ($buf == 'quit') {
break;
}
if ($buf == 'shutdown') {
socket_close($msgsock);
break 2;
}
if ($buf == '<policy-file-request/>'){
$talkback = "Blah Blah blah.
";
echo $talkback;
socket_write($msgsock, $talkback);
break;
}
// $talkback = "PHP: You said '$buf'.
";
//socket_write($msgsock, $talkback, strlen($talkback));
echo "$buf
";
} while (true);
socket_close($msgsock);
} while (true);
socket_close($sock);
?>