- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个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
似乎需要更长的时间才能移至下一个状态。
最佳答案
我在您的代码中发现了两个问题:
首先,socket_select的第四个参数(tv_sec)应该为NULL而不是0。如Manual中所述,0会在无用的无穷循环中导致较高的CPU负载,而NULL将阻塞直到发生任何事情:
tv_sec may be zero , causing socket_select() to return immediately. This is useful for polling. If tv_sec is NULL (no timeout), socket_select() can block indefinitely.
if (socket_select($read, $write = NULL, $except = NULL, NULL, 0) < 1)
// check if the client is disconnected
if ($data === false || $data === '') {
关于php - PHP套接字脚本导致许多长期的CLOSE_WAIT套接字连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19091319/
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 5年前关闭。 Improve this
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我试图弄清楚如何基于 JSESSIONID cookie 为具有长期交互式用户 session 的 web 应用程序提供零停机滚动更新,这些 session 应该是粘性的。 出于这个(和其他)原因,我
这更像是一个“你能给我指出正确的方向吗”类型的问题。 基本上,我想为客户提供一种“保存”购物车的方法,也许以后可以与亲戚一起查看(这是摄影师客户的“照片篮”)。 当使用我的“照片篮”时,它会创建一个篮
我正在编写一个使用商店的网络应用程序。如果客户端在30秒内没有收到响应,则认为该请求已失效,并给出超时错误。 我正在尝试让 MongoDB 做同样的事情。例如,如果连接断开 1 分钟,驱动程序将尝试重
我多年来一直在开发和管理 Oracle 数据库应用程序,并且想学习 SQL Server。有没有人有任何书籍推荐(最好是电子版)。当然,我已经对 SQL 了如指掌,所以我想避免任何介绍性的内容,直接进
我是一名优秀的程序员,十分优秀!