2012-05-06 109 views
0

我有一个php服务器与连接到服务器的客户端阵列.. 有没有一种方法可以让我找出每个客户端连接多长时间,如果客户端超时时间断开它?插座被接受的时候,php会保存在什么地方? 我试图摆脱鬼客户端..php套接字超时

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

回答

0

如果您使用的是基于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. 
}