PHP 套接字超时


php socket timeout

我有一个PHP服务器,其中包含连接到服务器的客户端数组。有没有办法让我找出每个客户端的连接时间以及客户端是否超过了超时时间来断开连接?PHP 是否在接受套接字时保存在某个地方?我正在尝试摆脱幽灵客户端。

我知道我可以设置套接字接受自己的时间并循环抛出它,但我的问题是否有某种类似的东西的 socket_get_connection_time() 函数?

处理此问题的一种方法是使用信号,如果您使用的是基于 Unix 的平台(我不相信这在 Windows 上可用)。

首先,您需要确保您的 PHP 构建包含 pcntl_* 函数。在此处查看文档:http://php.net/manual/en/book.pcntl.php 在许多安装中,默认情况下不会编译它。

你会做这样的事情:

if( !function_exists( "pcntl_signal" ) ) {
    die("pcntl_* functions not available");
}
pcntl_signal( 14, "cleanup", TRUE ); // 14 is the value for the SIGALARM unix signal
pcntl_alarm( XXXX ); // where XXXX is the number of second timeout for doing maintenance
function cleanup()
{
   // do your checks for timed out sockets here.
   // you will need some way to access the data, possibly a global variable pointing to
   // to the list of socket connections.
}