note 35247 deleted from ref.sockets by didou
| From: | didou@php.net | Date: | Tue, 16 Dec 2003 15:05:30 +0000 |
| Subject: | note 35247 deleted from ref.sockets by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-62061@lists.php.net to get a copy of this message | ||
Note Submitter: K_[nospaamatall]JR@sol.dk
----
The 'simple talkback server' didnt work for me, so I re-made it.
Still simple, and it works. There must be more beginners out there
that could use this :)
My config:
WinXP Pro + Apache 2.0.46 + PHP 4.3.2 + Zend Engine v1.3.0
<?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 = '0.0.0.0';
$port = 10000;
if (($sock = socket_create(AF_INET, SOCK_STREAM, 0)) === false)
{
echo "socket_create() failed: reason: " . socket_strerror($sock) . "\n";
}
if (($ret = socket_bind($sock, $address, $port)) === false)
{
echo "socket_bind() failed: reason: " . socket_strerror($ret) . "\n";
}
if (($ret = socket_listen($sock, 10)) === false)
{
echo "socket_listen() failed: reason: " . socket_strerror($ret) . "\n";
}
do
{
if (($msgsock = socket_accept($sock)) === false)
{
echo "socket_accept() failed: reason: " . socket_strerror($msgsock) .
"\n";
break;
}
/* Send instructions. */
$msg = "\n Welcome to the PHP Test Server. \n\r" .
"To quit, type 'quit'. To shut down the server type
'shutdown'.\n\r";
socket_write($msgsock, $msg, strlen($msg));
$cmd = "";
do {
if (false === ($buf = socket_read($msgsock, 4096)))
{
echo "socket_read() failed: reason: " . socket_strerror($ret) .
"\n";
break 2;
}
if ($buf == "\r\n")
{
if ($cmd == 'm00')
{
socket_write($msgsock, "PHP replies: Must be a cow out there...\n\r",
strlen ("PHP replies: Must be a cow out there... "));
}
if ($cmd == 'quit')
{
socket_write($msgsock, "\n\rClosing connection ...", strlen (" Closing
connection ..."));
break;
}
if ($cmd == 'shutdown')
{
socket_write($msgsock, "\n\rShutdown initiated...", strlen (" Shutdown
initiated..."));
socket_close ($msgsock);
break 2;
}
$cmd = "";
}
else
{
$cmd .= $buf;
}
} while (true);
socket_close ($msgsock);
} while (true);
socket_close ($sock);
?>