note 35247 added to ref.sockets
| From: | K_ at rack1 dot php dot net | Date: | Mon, 25 Aug 2003 11:42:05 +0000 |
| Subject: | note 35247 added to ref.sockets | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-55030@lists.php.net to get a copy of this message | ||
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);
?>
----
Manual Page -- http://www.php.net/manual/en/ref.sockets.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35247
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35247&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35247&report=yes