title: php socket occasionally unresponsive and no errors thrown
body: <p>I've written a database application using <code>MySQL</code> and <code>PHP</code> on the server side, and <code>Flex</code> on the client side. I use a php socket to have it automatically update all clients whenever changes are made to the database.</p>
  
  <p>The whole system works swimmingly, but every now and then the socket seems to stop responding. The strange thing is that the connection is still good – any changes a client performs are implemented, but the socket doesn't broadcast the message. The socket file isn't throwing any errors (though when I run error_log from the socket those messages appear). Memory use of the socket doesn't change on the server, and no disconnect signal is sent. Stranger still, eventually the socket starts working again, after about half an hour or so. If I restart the socket that also solves the problem.</p>
  
  <p>I'm working on a hacky solution allowing the client to restart the socket if it becomes unresponsive, but that's unsatisfying and open to mistakes. What I'd really like is to learn why this might be happening. Does the socket somehow get "saturated" after a certain number of connections? Should I be doing something to clean up the socket server? I've tried three different physical servers (one local and two online) and the same thing happens, so it's definitely me.</p>
  
  <p>I feel like there's something basic that I'm doing wrong. Here's the code I'm using for the socket server (it's a slightly modified version of socket written by Raymond Fain on kirupa.com, so I've left his original comment at the top):</p>
  
  <pre><code>#!/usr/bin/php -q
  &lt;?php
  /*
  Raymond Fain
  Used for PHP5 Sockets with Flash 8 Tutorial for Kirupa.com
  For any questions or concerns, email me at ray@obi-graphics.com
  or simply visit the site, www.php.net, to see if you can find an answer.
  */
  
  //ini_set('display_errors',1);
  //ini_set('display_startup_errors',1);
  error_reporting(E_ALL);
  ini_set('error_log', 'socket_errors.log');
  ini_set('log_errors', 'On');
  ini_set('display_errors', 'Off');
  
  set_time_limit(0);
  
  ob_implicit_flush();
  
  error_log('testing');
  
  $address = 'xxx.xxx.xx.xx';
  $port = xxxxx;
  
  function send_Message($allclient, $socket, $buf)
  {
      $buf = str_replace("\0","",$buf);
      //echo "&lt;mbFeed&gt;$buf&lt;/mbFeed&gt;\n\0";
      foreach($allclient as $client)
      {
          socket_write($client, "&lt;mbFeed&gt;$buf&lt;/mbFeed&gt;\n\0");
      }
  }
  
  
  echo "connecting...
  ";
  //---- Start Socket creation for PHP 5 Socket Server -------------------------------------
  
  if (($master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) &lt; 0)
  {
      echo "socket_create() failed, reason: " . socket_strerror($master) . "\n";
  }
  
  socket_set_option($master, SOL_SOCKET,SO_REUSEADDR, 1);
  
  
  if (($ret = socket_bind($master, $address, $port)) &lt; 0)
  {
      echo "socket_bind() failed, reason: " . socket_strerror($ret) . "\n";
  }
  
  echo 'socket bind successfull.
  ';
  
  
  if (($ret = socket_listen($master, 5)) &lt; 0)
  {
      echo "socket_listen() failed, reason: " . socket_strerror($ret) . "\n";
  }
  
  $read_sockets = array($master);
  
  echo "connected.";
  
  //---- Create Persistent Loop to continuously handle incoming socket messages ---------------------
  while (true)
  { 
      $changed_sockets = $read_sockets;
      $num_changed_sockets = socket_select($changed_sockets, $write = NULL, $except = NULL, NULL);
      foreach($changed_sockets as $key =&gt; $socket)
      {
          if ($socket == $master)
          {
              if (($client = socket_accept($master)) &lt; 0)
              {
                  echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n";
                  continue;
              } 
              else
              {
                  array_push($read_sockets, $client);
              }
          }
          else
          {
              $bytes = socket_recv($socket, $buffer, 8192, 0);
  
              if ($bytes == 0)
              {
                  unset($read_sockets[$key]);
                  unset($changed_sockets[$key]);
                  socket_close($socket);
              }
              else
              {
                  $allclients = $read_sockets;
                  array_shift($allclients);
                  //any messages starting with ::: are not to be broadcast, and may be used for other things. This message
                  //usually comes from the client.
                  if (substr($buffer, 0, 3) == ":::") handleSpecial(substr($buffer, 3));
                  else
                  {
                      //otherwise the message comes from a php file that will be closed, so the socket needs to be closed.
                      unset($read_sockets[$key]);
                      unset($changed_sockets[$key]);
                      socket_close($socket);
                      send_Message($allclients, $socket, $buffer);
                  }
              }
          }
      }
  }
  
  function handleSpecial($message)
  {
      error_log($message);
  }
  ?&gt;
  </code></pre>