Socket Server problem

From: Date: Thu, 15 Apr 2004 19:09:41 +0000
Subject: Socket Server problem
Groups: php.general 
Request: Send a blank email to php-general+get-183548@lists.php.net to get a copy of this message
I'm working through Ori Staub's excellent Socket Server tutorial (http://www.zend.com/zend/tut/tutorial-staub3.php), but can't get the sample server working. I've read the user comments and fixed a couple things, but still, no dice. Here's the error I receive: [Valhalla:~/Sites/test] rene% /usr/local/php/bin/php s9.php PHP Notice: Undefined variable: client in /Users/rene/Sites/test/s9.php on line 30 PHP Notice: Undefined variable: client in /Users/rene/Sites/test/s9.php on line 30 PHP Notice: Undefined variable: client in /Users/rene/Sites/test/s9.php on line 30 PHP Notice: Undefined variable: client in /Users/rene/Sites/test/s9.php on line 30 PHP Notice: Undefined variable: client in /Users/rene/Sites/test/s9.php on line 30 PHP Notice: Undefined variable: client in /Users/rene/Sites/test/s9.php on line 30 PHP Notice: Undefined variable: client in /Users/rene/Sites/test/s9.php on line 30 PHP Notice: Undefined variable: client in /Users/rene/Sites/test/s9.php on line 30 PHP Notice: Undefined variable: client in /Users/rene/Sites/test/s9.php on line 30 PHP Notice: Undefined variable: client in /Users/rene/Sites/test/s9.php on line 30 ...and then it just hangs. Since I'm so new to sockets, and probably don't yet grasp the concepts perfectly, I'm a little lost where to start. Any ideas? Here's my code: <? // VARIABLES set_time_limit(0); $server_ip = "192.168.0.200"; $server_port = "7000"; $max_clients = 10; // Array will hold client info $clients = Array(); // Create TCP stream socket $sock = socket_create(AF_INET,SOCK_STREAM,0); // Bind the socket socket_bind($sock,$server_ip,$server_port) or die('Could not bind to address'); // Start listening... socket_listen($sock); // Loop while (true) { // Setup clients listen socket for reading $read[0] = $sock; for ($i = 0; $i < $max_clients; $i++) { if ($client[$i]['sock'] != null) $read[$i+1] = $client[$i]['sock']; } // Set up a blocking call to socket_select() $ready = socket_select($read,$null=null,$null=null,$null=null); // If a new connection is being made add it to the client array if (in_array($sock,$read)) { $cur_conn = count($client); for ($i = 0; $i < $cur_conn; $i++) { if ($client[$i]['sock'] == null) { $client[$i]['sock'] = socket_accept($sock); break; } elseif ($i == $max_clients - 1) print ("too many clients"); } if (--$ready <= 0) continue; } // end if in_array // If a client is trying to write, deal with it $cur_conn = count($client); for ($i = 0; $i < $cur_conn; $i++) // for each client { if (in_array($client[$i]['sock'],$read)) { $input = socket_read($client[$i]['sock'],1024); if ($input == null) { // Zero length string meaning disconnected unset($client[$i]); } if ($input == 'exit') { // requested disconnect socket_close($client[$i]['sock']); } elseif ($input) { // strip whitespace and writeback to user $output = ereg_replace("[ \t\n\r]","",$input).chr(0); $socket_write($client[$i]['sock'],$output); } } else { // Close the socket socket_close($client[$i]['sock']); unset($client[$i]); } } } // end while // Close the master sockets socket_close($sock); ?>

« previous php.general (#183548) next »