note 18107 added to ref.sockets
| From: | jaime at chavarriaga dot com | Date: | Thu, 10 Jan 2002 18:24:56 +0000 |
| Subject: | note 18107 added to ref.sockets | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-24632@lists.php.net to get a copy of this message | ||
the server sample is wrong... the socket_read is wrong, and the error detection too...
a correct sample is... (the extensions must be loaded)
<?php
/* TCP/IP Server Test
sample for PHP >= 4.1.0
*/
echo "TCP/IP Connection - Server Test\n\n";
/* Allow the script to hang around waiting for connections. */
set_time_limit (0);
/* server configuration */
$address = 'localhost';
$service_port = 10000;
echo "Creating socket...\n";
if (($sock = socket_create (AF_INET, SOCK_STREAM, 0)) < 0) {
echo "socket_create() failed: reason: " . socket_strerror ($sock) . "\n";
}
echo "Binding socket to ".$address.":".$service_port."\n";
if (($ret = socket_bind ($sock, $address, $service_port)) < 0) {
echo "socket_bind() failed: reason: " . socket_strerror ($ret) . "\n";
}
echo "Listening in the socket....\n";
if (($ret = socket_listen ($sock, 5)) < 0) {
echo "socket_listen() failed: reason: " . socket_strerror ($ret) . "\n";
}
do {
echo "Waiting for connection...\n";
if (($msgsock = socket_accept($sock)) < 0) {
echo "socket_accept() failed: reason: " . socket_strerror ($msgsock) .
"\n";
break;
}
do {
$buf = '';
echo "Reading socket...\n";
$buf = socket_read ($msgsock, 2048);
$buf = trim ($buf);
if (strlen($buf) > 0) {
if ($buf == 'quit') {
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); /* close incoming socket */
} while (true);
socket_close ($sock); /* close server socket */
?>
--
http://www.php.net/manual/en/ref.sockets.php
http://master.php.net/manage/user-notes.php?action=edit+18107
http://master.php.net/manage/user-notes.php?action=delete+18107
http://master.php.net/manage/user-notes.php?action=reject+18107