如何在php中设置套接字读取时的超时

如何在php中设置套接字读取时的超时,php,sockets,Php,Sockets,我正在服务器端使用do-while循环读取套接字 <?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 so

我正在服务器端使用do-while循环读取套接字

<?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);
?>

在这种情况下,它总是在循环中运行并等待传入数据。
如果一分钟内没有从客户端接收到数据,如何停止循环?我无法从客户端关闭套接字。

请尝试使用我应该尝试的位置。。我在阅读之前试过,我犯了一个错误,我给了相同的资源,不管是什么,都是用来阅读的。stream\u set\u timeout():提供的资源不是有效的流资源。我尝试了stream\u set\u timeout($socket,0,500);或者流设置超时($spawn,0500);显示我在问题中更新的完整代码(使用$spawn creation)。。