【发布时间】:2013-09-30 09:58:05
【问题描述】:
我有一个 PHP 脚本(我在网上找到的)可以无限运行并监听一个端口,如果与该端口建立了连接,它将建立一个 TCP connection。但是,当我运行这个脚本并且有很多连接(大约 500 个)时,CLOSE_WAIT 连接的数量会增加。在此状态下连接的远程设备无法再次连接,因为CLOSE_WAIT 没有终止。
// port info
$host = "0.0.0.0";
$port = 10260;
$pos = 1;
// don't timeout!
set_time_limit(0);
record("START");
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
$timeout = array('sec'=>3000,'usec'=>0);
$try = socket_set_option($sock,SOL_SOCKET,SO_RCVTIMEO,$timeout);
// Bind the socket to the address/port
if(!socket_bind($sock, $host, $port))
{
echo socket_last_error() ;
die('Could not bind to address');
}
record("SOCKET BIND OK");
// start listening for connections
$result = socket_listen($sock, 1024) or die("Could not set up socket listener\n");
record("SOCKET LISTEN OK");
$clients = array($sock);
// infinite while loop
while(1)
{
// Setup clients listen socket for reading
$read = $clients;
$e = NULL;
if (socket_select($read, $write = NULL, $except = NULL, 0,0) < 1)
{
continue;
}
/* if a new read ready is being made add it to the client array */
if (in_array($sock, $read)) {
record("NEW CONNECTION");
$clients[$pos] = $newsock = socket_accept($sock);
$curpos = $pos;
$pos++;
socket_getpeername($newsock, $ip,$port);
record("Incoming IP: {$ip} PORT: {$port}");
// remove the listening socket from the clients-with-data array
$key = array_search($sock, $read);
unset($read[$key]);
} // end if in_array
// loop through all the clients that have data to read from
foreach ($read as $read_key => $read_sock) {
// read until newline or 1024 bytes
// socket_read while show errors when the client is disconnected, so silence the error messages
$key = $read_key;
$fulldata = $data = @socket_read($read_sock, 1024);
// check if the client is disconnected
if ($data === false) {
// remove client for $clients array
$key = array_search($read_sock, $clients);
socket_close($read_sock);
unset($clients[$key]);
record("NO DATA");
// continue to the next client to read from, if any
continue;
}
// .. do something with $data ...
}
}
socket_close($sock);
record("END");
die("DONE");
我尝试在代码中使用socket_close(),但无济于事。 CLOSE_WAIT 似乎需要更长的时间才能进入下一个状态。
【问题讨论】:
-
我遇到了类似的问题。在我的情况下,问题是一个客户端突然断开连接,当它的缓冲区已满时,就再也无法连接到端口了。曾经理解过这个问题,但我已经修复了一个容差较小的手工 ping 系统(只是一个可能对你有帮助的信息)
-
@chumkiu 感谢您提供的信息。当您说客户端的缓冲区已满时,您是指连接到端口的应用程序还是远程设备?如果是应用程序,有没有办法确定缓冲区是否已满?谢谢!
-
我指的是服务器上
netstat中ghost 客户端的Recv-Q。我没有找到任何可以在 php 中阅读的内容(但我的经验是 6 年前) -
谢谢@chumkiu - 我遇到的问题是由下面的答案中提到的。
-
即使经过改进,此脚本在 Linux 上仍将限制为 1024 个套接字,因为即使是最新的 PHP 源代码仍使用 select() 调用。 HHVM 至少使用 poll()。