如何在php中设置socket读取时间

时间:2013-09-11 04:36:48

标签: 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 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);
?>

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

0 个答案:

没有答案