title: PHP socket_accept throws warnings in non-blocking mode
body: <p>I want to create an easy PHP server (TCP-based) that would serve actual time and close the connection immediately. I've done that already. I wanted to add crtl-C handling so I needed to replace blocking socket_accept with non-blocking (this is because when the blocking socket_accept instruction is reached and I send SIGINT /ctrl-C/ then the server will still be alive until the first client is server and then it closes itself - and I didn't want this behavior).</p>
  
  <p>My current code looks like this:</p>
  
  <pre><code>&lt;?php
  
  error_reporting(E_ALL);
  ob_implicit_flush();
  
  if ($argc != 2)
      die("Wrong params");
  
  $address = 'localhost';
  $port = $argv[1];
  
  if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false)
      die(socket_strerror(socket_last_error()) . "\n");
  
  if (socket_bind($sock, $address, $port) === false)
      die(socket_strerror(socket_last_error($sock)) . "\n");
  
  if (socket_listen($sock, 5) === false)
      die(socket_strerror(socket_last_error($sock)) . "\n");
  
  socket_set_nonblock($sock);
  
  $remote_host = $remote_port = $msgsock = null;
  
  declare(ticks = 1);
  
  function sig_handler($signo)
  {
      switch ($signo) {
          case SIGTERM:
          case SIGINT:
              global $sock;
              socket_shutdown($sock);
              socket_close($sock);
              echo "Terminating...\n";
              exit;
      }
  }
  
  pcntl_signal(SIGTERM, "sig_handler");
  pcntl_signal(SIGINT, "sig_handler");
  
  echo "Starting server\n";
  
  while (1) {
      do {
          $msgsock = socket_accept($sock);
          usleep(100000);
      } while ($msgsock === false);
  
      socket_getpeername($msgsock, $remote_host, $remote_port);
      echo "Connection made from {$remote_host}:{$remote_port}\n";
  
      $msg = date('r', time()) . "\n";
      socket_write($msgsock, $msg, strlen($msg));
      socket_close($msgsock);
  };
  
  socket_close($sock);
  </code></pre>
  
  <p>Everything works fine except for one detail... I get the following PHP warning every 0.1 second (= 100000 microseconds):</p>
  
  <pre><code>PHP Warning:  socket_accept(): unable to accept incoming connection [11]: Resource temporarily unavailable in /home/tomasz/Development/Python/twisted/time-server.php on line 55
  PHP Stack trace:
  PHP   1. {main}() /home/tomasz/Development/Python/twisted/time-server.php:0
  PHP   2. socket_accept() /home/tomasz/Development/Python/twisted/time-server.php:55
  </code></pre>
  
  <p>What I've tried to achieve is non-blocking accept: PHP uses the server socket, checks if there's any connection awaiting to be served. I not - wait 0.1 second. If there is a pending connection, serve it. All functionality is OK except that I've got no idea why is this warning thrown - I just want to check if there's any connection to be served. Modifying <code>error_reporting</code> to <code>E_ERROR</code> makes the warnings quiet, but I hope there's a better way to solve that...</p>
  
  <hr>
  
  <p>edit:</p>
  
  <p>modifying <code>socket_accept($sock)</code> to <code>@socket_accept($sock)</code> will just suspress warnings from  being thrown, but still this doesn't state why it is thrown...</p>