Socket server with multiple connections

From: Date: Fri, 23 Apr 2004 22:26:08 +0000
Subject: Socket server with multiple connections
Groups: php.general 
Request: Send a blank email to php-general+get-184480@lists.php.net to get a copy of this message
I've gone through a few good socket client and server tutorials (such as http://www.devshed.com/c/a/PHP/Socket-Programming-With-PHP/), and have got the samples working. What I'm trying to do now is write a simple Socket Server that will accept multiple connections, do its thing, and keep running. This Zend tutorial (http://www.zend.com/zend/tut/tutorial-staub3.php) does explain the concepts pretty well, but I cannot for the life of me get the sample code to run. The user comments noticed a couple typos, and other errors, which I've fixed, but still... no dice. I'm running PHP Version 4.3.4 on Mac OSX 10.2.8. Here's the error message: ------------------ [Valhalla:~/Sites/sockets/test] rene% /usr/local/php/bin/php s9.php PHP Notice: Undefined offset: 0 in /Users/me/Sites/sockets/test/s9.php on line 20 Notice: Undefined offset: 0 in /Users/me/Sites/sockets/test/s9.php on line 20 PHP Notice: Undefined offset: 1 in /Users/me/Sites/sockets/test/s9.php on line 20 Notice: Undefined offset: 1 in /Users/me/Sites/sockets/test/s9.php on line 20 PHP Notice: Undefined offset: 2 in /Users/me/Sites/sockets/test/s9.php on line 20 Notice: Undefined offset: 2 in /Users/me/Sites/sockets/test/s9.php on line 20 PHP Notice: Undefined offset: 3 in /Users/me/Sites/sockets/test/s9.php on line 20 Notice: Undefined offset: 3 in /Users/me/Sites/sockets/test/s9.php on line 20 PHP Notice: Undefined offset: 4 in /Users/me/Sites/sockets/test/s9.php on line 20 Notice: Undefined offset: 4 in /Users/me/Sites/sockets/test/s9.php on line 20 PHP Notice: Undefined offset: 5 in /Users/me/Sites/sockets/test/s9.php on line 20 Notice: Undefined offset: 5 in /Users/me/Sites/sockets/test/s9.php on line 20 PHP Notice: Undefined offset: 6 in /Users/me/Sites/sockets/test/s9.php on line 20 Notice: Undefined offset: 6 in /Users/me/Sites/sockets/test/s9.php on line 20 PHP Notice: Undefined offset: 7 in /Users/me/Sites/sockets/test/s9.php on line 20 Notice: Undefined offset: 7 in /Users/me/Sites/sockets/test/s9.php on line 20 PHP Notice: Undefined offset: 8 in /Users/me/Sites/sockets/test/s9.php on line 20 Notice: Undefined offset: 8 in /Users/me/Sites/sockets/test/s9.php on line 20 PHP Notice: Undefined offset: 9 in /Users/me/Sites/sockets/test/s9.php on line 20 Notice: Undefined offset: 9 in /Users/me/Sites/sockets/test/s9.php on line 20 ------------------ Here my code: ------------------ <? // VARIABLES set_time_limit(0); $server_ip = "192.168.0.200"; $server_port = date("i")."000"; // create socket $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n"); // bind socket to port $result = socket_bind($socket, $server_ip, $server_port) or die("Could not bind to socket\n"); // start listening for connections $result = socket_listen($socket, 3) or die("Could not set up socket listener\n"); echo "Waiting for connections...\n"; // accept incoming connections // spawn another socket to handle communication $spawn = socket_accept($socket) or die("Could not accept incoming connection\n"); echo "Received connection request\n"; // write a welcome message to the client $welcome = "Welcome to the Server!\n ".chr(0); socket_write($spawn, $welcome, strlen ($welcome)) or die("Could not send connect string\n"); // keep looping and looking for client input do {
        // read client input
        $input = socket_read($spawn, 1024) or die("Could not read input\n");
        if (trim($input) != "")
        {
echo "Received input: $input\n"; // if client requests session end if (trim($input) == "END") { // close the child socket // break out of loop socket_close($spawn); break; } // otherwise... else { // reverse client input and send back $output = strtoupper($input); socket_write($spawn, $output) or die("Could not write output\n"); echo "Sent output: " . trim($output) . "\n"; }
        }
} while (true); // close primary socket socket_close($socket); echo "Socket terminated\n"; ?> ----------------------- Any ideas? ...Rene

« previous php.general (#184480) next »