title: how to set time out at socket read in php
body: <p>I am reading a socket using do-while loop at server side. </p>
  
  <pre><code>&lt;?php
  $host = "192.168.1.99";
  $port = 3501;
  set_time_limit(0);
  $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
  $result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
  $result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
  $spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
  $input = socket_read($spawn, 1024) or die("Could not read input\n");
  stream_set_timeout($spawn ,0 ,1000);
  do
  {
  $input = trim($input);
  echo "Client Message : ".$input;
  flush(); @ob_flush();
  $output = strrev($input) . "\n";
  socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
  sleep(3);
  }
  while ($input = socket_read($spawn, 1024) or die("Could not read input\n"));
  socket_close($spawn);
  socket_close($socket);
  ?&gt;
  </code></pre>
  
  <p>In this case, it is always running in a loop and waiting for incoming data.
  How to stop the loop if no data received from client within a minute? I cant close the socket from client side.</p>