title: PHP Sockets - Accept multiple connections
body: <p>I'm trying to create a simple client/server application and thus I am experimenting with sockets in PHP.</p>
  <p>Now I have a simple client in C# which connects to the server well, but i can only connect one client at once to this server (I found this code sample online and tweaked it a bit for testing purposes).</p>
  <p>Funny enough I found the same question, based on the same example here: <a href="https://stackoverflow.com/questions/10318023/php-socket-connections-cant-handle-multiple-connection">https://stackoverflow.com/questions/10318023/php-socket-connections-cant-handle-multiple-connection</a></p>
  <p>I tried to understand every part of it and I'm close to seeing how it works in detail, but for some reason, when I connect a 2nd client, the first one gets disconnected / crashes.</p>
  <p>Can anyone give me some wild ideas or a pointer to where I should look at?</p>
  <pre><code>&lt;?php
  // Set time limit to indefinite execution
  set_time_limit (0);
  // Set the ip and port we will listen on
  $address = '127.0.0.1';
  $port = 9000;
  $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);
  // Loop continuously
  while (true) {
      // Setup clients listen socket for reading
      $read[0] = $sock;
      for ($i = 0; $i &lt; $max_clients; $i++)
      {
          if (isset($client[$i]))
          if ($client[$i]['sock']  != null)
              $read[$i + 1] = $client[$i]['sock'] ;
      }
      // Set up a blocking call to socket_select()
      $ready = socket_select($read, $write = NULL, $except = NULL, $tv_sec = NULL);
      /* if a new connection is being made add it to the client array */
      if (in_array($sock, $read)) {
          for ($i = 0; $i &lt; $max_clients; $i++)
          {
              if (!isset($client[$i])) {
                  $client[$i] = array();
                  $client[$i]['sock'] = socket_accept($sock);
                  echo(&quot;Accepting incoming connection...\n&quot;);
                  break;
              }
              elseif ($i == $max_clients - 1)
                  print (&quot;too many clients&quot;);
          }
          if (--$ready &lt;= 0)
              continue;
      } // end if in_array
  
      // If a client is trying to write - handle it now
      for ($i = 0; $i &lt; $max_clients; $i++) // for each client
      {
          if (isset($client[$i]))
          if (in_array($client[$i]['sock'] , $read))
          {
              $input = socket_read($client[$i]['sock'] , 1024);
              if ($input == null) {
                  // Zero length string meaning disconnected
                  echo(&quot;Client disconnected\n&quot;);
                  unset($client[$i]);
              }
              $n = trim($input);
              if ($n == 'exit') {
                  echo(&quot;Client requested disconnect\n&quot;);
                  // requested disconnect
                  socket_close($client[$i]['sock']);
              }
              if(substr($n,0,3) == 'say') {
                  //broadcast
                  echo(&quot;Broadcast received\n&quot;);
                  for ($j = 0; $j &lt; $max_clients; $j++) // for each client
                  {
                      if (isset($client[$j]))
                      if ($client[$j]['sock']) {
                          socket_write($client[$j]['sock'], substr($n, 4, strlen($n)-4).chr(0));
                      }
                  }
              } elseif ($input) {
                  echo(&quot;Returning stripped input\n&quot;);
                  // strip white spaces and write back to user
                  $output = ereg_replace(&quot;[ \t\n\r]&quot;,&quot;&quot;,$input).chr(0);
                  socket_write($client[$i]['sock'],$output);
              }
          } else {
              // Close the socket
              if (isset($client[$i]))
              echo(&quot;Client disconnected\n&quot;);
              if ($client[$i]['sock'] != null){ 
                  socket_close($client[$i]['sock']); 
                  unset($client[$i]); 
              }
          }
      }
  } // end while
  // Close the master sockets
  echo(&quot;Shutting down\n&quot;);
  socket_close($sock);
  ?&gt;
  </code></pre>