title: PHP Socket Server Hangs
body: <p>I've worked hours and hours on this, and I can't figure it out. I've browsed the posts on here trying to find the solution also and to no avail.</p>
  
  <p>I have a Socket Server setup for my browser-based game. I've stripped it down trying to find the issue, and it seems that fread is hanging because if I comment out "fread($fp, 10024);" then it runs fine, but of course doesn't read the response.</p>
  
  <p>While I debug this, I've broken the files down to the basics.</p>
  
  <p>I have the Socket Server: ChatServer.php</p>
  
  <pre><code>set_time_limit(0);
  $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  socket_get_option($sock, SOL_SOCKET, SO_REUSEADDR);
  socket_bind($sock, "127.0.0.1", "9990");
  socket_listen($sock, 4);
  
  $chatContent = "Testing, 1, 2, 3.";
  do
  {
      $childSocket = socket_accept($sock);
      $incomingData = socket_read($childSocket, 12048);
  
      socket_write($childSocket, $chatContent, strlen($chatContent));
  } while(true);
  </code></pre>
  
  <p>Then I have Test.php which should open the socket and read a response.</p>
  
  <pre><code>$fp = fsockopen("127.0.0.1", "9990", $errno, $errstr, 5);
  echo $errstr . "&lt;br /&gt;";
  echo fread($fp, 10024);
  </code></pre>
  
  <p>$errstr doesn't display an error, because when I start ChatServer.php then reload Test.php, it never reloads. It hangs for minutes and lags my entire server. I'm running on a VPS. This worked fine before, then suddenly stopped working, and I can't figure out why.</p>
  
  <p>Edit: Thanks to GigaWatt, I was able to get it working. Here is the code I used if you have the same issue. :)</p>
  
  <pre><code>    set_time_limit(0);
  $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  socket_get_option($sock, SOL_SOCKET, SO_REUSEADDR);
  socket_bind($sock, "127.0.0.1", "9990");
  socket_listen($sock, 4);
  
  $chatContent = "Testing, 1, 2, 3.";
  do
  {
      $childSocket = socket_accept($sock);
      $meta = stream_get_meta_data($sock);
  
      if($meta['unread_bytes'] &gt; 0) {
          $incomingData = socket_read($childSocket, $meta['unread_bytes']);
      }
  
      socket_write($childSocket, $chatContent, strlen($chatContent));
  } while(true);
  </code></pre>
  
  <p>Just use stream_get_meta_data and then unread_bytes.</p>