title: PHP-Websocket: Problems with socket_recv() and socket_getpeername()
body: <p>I'm using PHP Websocket and sometimes, I get the following both warnings.<br> 
  Warning-1:</p>
  
  <blockquote>
    <p>socket_recv(): An existing connection was forcibly closed by the remote host</p>
  </blockquote>
  
  <p>Warning-2:</p>
  
  <blockquote>
    <p>socket_getpeername(): unable to retrieve peer name [107]: Transport endpoint is not connected</p>
  </blockquote>
  
  <p>I send messages to the Websocket by another PHP-Script. This PHP-Script sends a message and if it's finised, the Socket-Call ends. <br/>
  So this Warning is probably correct, because the "endpoint" (= the PHP-Script) is no loger connected.<br/>
  But this Warning is not beautiful..<br/><br/>
  So this is the following Code for Websocket, which is received the messages: </p>
  
  <pre><code>define('HOST_NAME',$host_ip); 
  define('PORT',$socket_port);
  
  $null = NULL;
  
  $socketHandler = new SocketHandler();
  
  $socketResource = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  socket_set_option($socketResource, SOL_SOCKET, SO_REUSEADDR, 1);
  socket_bind($socketResource, 0, PORT);
  socket_listen($socketResource);
  
  $clientSocketArray = array($socketResource);
  while (true) {
  
      $newSocketArray = $clientSocketArray;
      socket_select($newSocketArray, $null, $null, 0, 10);
  
      if (in_array($socketResource, $newSocketArray)) {
          $newSocket = socket_accept($socketResource);
          $clientSocketArray[] = $newSocket;
  
          $header = socket_read($newSocket, 1024);
          $socketHandler-&gt;doHandshake($header, $newSocket, HOST_NAME, PORT);
  
          socket_getpeername($newSocket, $client_ip_address);
  
          $newSocketIndex = array_search($socketResource, $newSocketArray);
          unset($newSocketArray[$newSocketIndex]);
      }
      foreach ($newSocketArray as $newSocketArrayResource) {  
      while(socket_recv($newSocketArrayResource, $socketData, 1024, 0) &gt;= 1){
  
              $socketMessage = $socketHandler-&gt;unseal($socketData);
              $messageObj = json_decode($socketMessage);
  
              break 2;
          }
          $socketData = @socket_read($newSocketArrayResource, 1024, PHP_NORMAL_READ);
  
          if ($socketData === false) { 
              socket_getpeername($newSocketArrayResource, $client_ip_address);
              $newSocketIndex = array_search($newSocketArrayResource, $clientSocketArray);
              unset($clientSocketArray[$newSocketIndex]);         
          }
      }
  }
  socket_close($socketResource);
  </code></pre>
  
  <p>The command, which produces the Warning-1 </p>
  
  <blockquote>
    <p>socket_recv(): An existing connection was forcibly closed by the remote host</p>
  </blockquote>
  
  <p>is:</p>
  
  <blockquote>
    <p>while(socket_recv($newSocketArrayResource, $socketData, 1024, 0) >= 1)</p>
  </blockquote>
  
  <p>In the <a href="http://php.net/manual/en/function.socket-recv.php" rel="nofollow noreferrer">PHP-Manual</a>  there is mentioned to get the return-value of socket_recv(). But in the examples there is only shown to execute socket_recv() once, by using if(). <br/>
  But I use socket_recv() within the loop-header. <br/>
  So my question: <br/>
  <b>How do I check return-value in my case - by using socket_recv() within a loop-header?</b></p>
  
  <p>Warning-2 </p>
  
  <blockquote>
    <p>socket_getpeername(): unable to retrieve peer name [107]: Transport endpoint is not connected</p>
  </blockquote>
  
  <p>was produced with the following code:</p>
  
  <blockquote>
    <p>socket_getpeername($newSocketArrayResource, $client_ip_address);</p>
  </blockquote>
  
  <p>My question is:<br/>
  <b>What have I to do in order not to execute socket_getpeername(), if the client is no longer connected?</b></p>
  
  <p>Maybe someone can help me to fix these Warnings. It's very difficult do watch, because these Warings do not always appear..</p>
  
  <p>Bye,
  Chris</p>