
不要走开,下面是完整的源代码:
// Set time limit to indefinite execution set_time_limit (0);// Set the ip and port we will listen on $address = "localhost"; $port = 10000; $max_clients = 10;// Array that will hold client information $client = Array();// Create a TCP Stream socket $sock = socket_create(AF_INET, SOCK_STREAM, 0); // Bind the socket to an address/port socket_bind($sock, $address, $port) or die("Could not bind to address"); // Start listening for connections socket_listen($sock);echo "Waiting for connections...
";// Loop continuously while (true) { // Setup clients listen socket for reading $read[0] = $sock; for ($i = 0; $i < $max_clients; $i++) {if (isset($client[$i]["sock"])) $read[$i + 1] = $client[$i]["sock"]; } // Set up a blocking call to socket_select() if (socket_select($read, $write = NULL, $except = NULL, $tv_sec = 5) < 1)continue; /* if a new connection is being made add it to the client array */ if (in_array($sock, $read)) {for ($i = 0; $i < $max_clients; $i++) { if (empty($client[$i]["sock"])) { $client[$i]["sock"] = socket_accept($sock); echo "New client connected $i
"; break; } elseif ($i == $max_clients - 1) echo "Too many clients...
";} } // end if in_array// If a client is trying to write - handle it now for ($i = 0; $i < $max_clients; $i++) { // for each clientif (isset($client[$i]["sock"])) { if (in_array($client[$i]["sock"], $read)) { $input = socket_read($client[$i]["sock"], 1024); if ($input == null) {echo "Client disconnecting $i
";// Zero length string meaning disconnectedunset($client[$i]); } else {echo "New input received $i
";// send it to the other clientsfor ($j = 0; $j < $max_clients; $j++) {if (isset($client[$j]["sock"]) && $j != $i) { echo "Writing "$input" to client $j
"; socket_write($client[$j]["sock"], $input, strlen($input));}}if ($input == "exit") {// requested disconnectsocket_close($client[$i]["sock"]);} } } else { echo "Client disconnected $i
"; // Close the socket socket_close($client[$i]["sock"]); unset($client[$i]); }} } } // end while // Close the master sockets socket_close($sock); 哎呀,乍一看这似乎是一个大工程,但是我们可以先将它分解为几个较小的部分。