PHP fsockopen() 非常慢

发布于 2024-10-21 02:10:31 字数 655 浏览 3 评论 0原文

我正在使用 fsockopen() 调用列表中的多个连接来查看各种 ip/主机和端口的在线状态...

<?php
$socket = @fsockopen($row[2], $row[3], $errnum, $errstr, 1);
if ($errnum >= 1) { $status = 'offline'; } else { $status = 'online';}
fclose($socket);

如果有效,我不会抱怨这一点,但我有大约 15 个 ip/端口我正在列表中检索(php for() 命令..)。我想知道是否有更好的方法来做到这一点?这条路很慢!?!服务器大约需要 1-2 分钟才能返回所有内容的响应。

更新:

<?php
$socket = @fsockopen("lounge.local", "80", $errnum, $errstr, 30);
if ($errnum >= 1) { $status = 'offline'; } else { $status = 'online'; }
?>

它将显示在列表中:“ReadyNAS AFP Readynas.local:548 online”

我不知道还有什么可以告诉你吗?加载结果集合需要很长时间......

I'm using fsockopen() to call a number of connections in a list to see the online status of various ip/host and ports ...

<?php
$socket = @fsockopen($row[2], $row[3], $errnum, $errstr, 1);
if ($errnum >= 1) { $status = 'offline'; } else { $status = 'online';}
fclose($socket);

if works, I'm not complaining about that, but I have approximately 15 ip/ports that i'm retrieving in a list (php for() command..). I was wondering if there is a better way to do this? This way is VERY slow!?! It is taking about 1-2 minutes for the server to come back with a response for all of them..

Update:

<?php
$socket = @fsockopen("lounge.local", "80", $errnum, $errstr, 30);
if ($errnum >= 1) { $status = 'offline'; } else { $status = 'online'; }
?>

It will display in a list: "ReadyNAS AFP readynas.local:548 online"

I don't know what more I can tell you? It just takes forever to load the collection of results...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

ㄖ落Θ余辉 2024-10-28 02:10:31

根据我自己的经验:

此代码:

$sock=fsockopen('www.site.com', 80);

相比,速度较慢

$sock=fsockopen(gethostbyname('www.site.com'), 80);

与在 PHP 5.4 中测试的代码 。如果同时进行多个连接,可以保留主机解析结果并重新使用它,以进一步减少脚本执行时间,例如:

function myfunc_getIP($host) {
   if (isset($GLOBALS['my_cache'][$host])) {
      return $GLOBALS['my_cache'][$host];
   }

   return $GLOBALS['my_cache'][$host]=gethostbyname($host);
}

$sock=fsockopen(myfunc_getIP('www.site.com'), 80);

From my own experience:

This code:

$sock=fsockopen('www.site.com', 80);

is slower compared to:

$sock=fsockopen(gethostbyname('www.site.com'), 80);

Tested in PHP 5.4. If doing many connections at the same time one could keep host resolution result and re-use it, to further reduce script time execution, for example:

function myfunc_getIP($host) {
   if (isset($GLOBALS['my_cache'][$host])) {
      return $GLOBALS['my_cache'][$host];
   }

   return $GLOBALS['my_cache'][$host]=gethostbyname($host);
}

$sock=fsockopen(myfunc_getIP('www.site.com'), 80);
吹泡泡o 2024-10-28 02:10:31

如果您打算“ping”某个 URL,我建议使用curl 来执行此操作,为什么?你可以使用curl并行发送ping,看看这个-> http://www.php.net/manual/en/function .curl-multi-init.php。在之前的项目中,它应该向我们的服务器提供实时数据,我们用来 ping 主机以查看它们是否还活着,而 Curl 是唯一对我们有帮助的选择。
这是一个建议,可能不是解决您问题的正确方法。

If you plan to "ping" some URL, I would advise doing it with curl, why? you can use curl to send pings in parallel, have a look at this -> http://www.php.net/manual/en/function.curl-multi-init.php. In a previous project, it was supposed to feed Real Time Data to our server, we used to ping hosts to see if they are alive or not and Curl was the only option that helped us.
Its an advice, may not be a right solution for your problem.

穿透光 2024-10-28 02:10:31

fsockopen() 的最后一个参数是超时,将其设置为一个较低的值以使脚本更快完成,如下所示:

fsockopen('192.168.1.93', 80, $errNo, $errStr, 0.01)

The last parameter to fsockopen() is the timeout, set this to a low value to make the script complete faster, like this:

fsockopen('192.168.1.93', 80, $errNo, $errStr, 0.01)
晚雾 2024-10-28 02:10:31

您是否比较过 fsockopen(servername)fsockopen(ip-address) 的结果?如果超时参数没有改变任何事情,则问题可能出在您的名称服务器中。如果使用 IP 地址的 fsockopen 速度更快,则必须修复名称服务器,或将域添加到 /etc/hosts 文件中。

Have you compared the results of fsockopen(servername) versus fsockopen(ip-address)? If the timeout parameter does not change a thing, the problem may be in your name server. If fsockopen with an IP address is faster, you'll have to fix your name server, or add the domains to /etc/hosts file.

淡淡绿茶香 2024-10-28 02:10:31

我建议做一些不同的事情。
将此主机放入数据库的表中,如下所示:

++++++++++++++++++++++++++++++++++++
| host | port | status | timestamp |
++++++++++++++++++++++++++++++++++++

并将状态检查部分移至 cron 脚本中,每 5 分钟或您想要的频率运行一次。
该脚本将检查主机:端口并更新每条记录的状态和时间戳,在您的页面中,您只需执行数据库查询并显示主机、其状态以及上次检查的时间(例如:1分钟前等... )
这样你的页面就会加载得很快。

I would recommend doing this a bit different.
Put this hosts in a table in a database something like:

++++++++++++++++++++++++++++++++++++
| host | port | status | timestamp |
++++++++++++++++++++++++++++++++++++

And move the status checking part in a cron script that you run it once every 5 minutes or how often you want.
This script will check the host:port and update status and timestamp for each record and in your page you will just do a db query and show the host, its status and when was last checked (something like: 1minute ago, etc...)
This way your page will load fast.

漫漫岁月 2024-10-28 02:10:31

根据php 手册,有一个超时参数。尝试将其设置为较低的值。

编辑:要添加丹尼尔的答案,nmap 可能是最好使用的工具。使用 cron 作业将其设置为每 X 分钟扫描并更新您的记录。类似的东西

$ for ip in $(seq 6 8); 
do 
     port_open=$(nmap -oG - -p 80 10.1.0.$ip|grep open|wc -l); 
     echo "10.1.0.$ip:$port_open"; 
done

10.1.0.6:1
10.1.0.7:1
10.1.0.8:0

According to the php manual, there's a timeout parameter. Try setting it to a lower value.

Edit: To add to Daniel's answer, nmap might be the best tool to use. Set it up with a cron job to scan and update your records every X minutes. Something like

$ for ip in $(seq 6 8); 
do 
     port_open=$(nmap -oG - -p 80 10.1.0.$ip|grep open|wc -l); 
     echo "10.1.0.$ip:$port_open"; 
done

10.1.0.6:1
10.1.0.7:1
10.1.0.8:0
寻梦旅人 2024-10-28 02:10:31

我遇到了一个问题,fsockopen 请求很慢,但 wget 确实很快。就我而言,发生这种情况是因为主机名同时具有 ipv4 和 ipv6 地址,但 ipv6 已关闭。因此,每次 ipv6 请求需要 20 秒左右才会超时。

I had an issue where fsockopen requests were slow, but wget was really snappy. In my case, it was happening because the hostname had both an ipv4 and ipv6 address, but ipv6 was down. So it took 20 or so seconds on each request for the ipv6 to time out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文