什么原因导致PHP延迟?

时间:2015-01-07 10:56:35

标签: php irc

我有一个关于我编写的用于连接到IRC服务器的收集统计信息的PHP脚本的问题,作为测试我不熟悉PHP并且还在学习。

这是脚本:

<?php
set_time_limit(0);

$servers = array(
           "irc.icq.com",
           "irc.quakenet.org"
);

function get_statistics ($server, $port) {
    $nick   = 'IRCDir' . rand(1000, 9999);
    $irc = fsockopen($server, $port);

    fputs($irc,"USER $nick 0 * :$nick\n");
    fputs($irc,"NICK $nick\n");


    while ($data = fgets($irc, 128)) {

        $ex = explode(' ', $data);

        if (isset($ex[0]) && $ex[0] == "PING") {
            fputs($irc, "PONG ".$ex[1]."\n");
        }

        if (count($ex) > 0) {

            if (isset($ex[1]) && $ex[1] == "001") {
                $network = $ex[6];
                echo date('h:i:s') . " network: " . $server;
                echo "\n";
            }

            if (isset($ex[1]) && $ex[1] == "002") {
                $server = str_replace(',', '', $ex[6]);
                echo date('h:i:s') . " server: " . $server;
                echo "\n";
            }

            if (isset($ex[1]) && $ex[1] == "251") {
                $users   = $ex[5] + $ex[8];
                $servers = $ex[11];
                echo date('h:i:s') . " users: " . $users;
                echo "\n";
                echo date('h:i:s') . " servers: " . $servers;
                echo "\n";
            }

            if (isset($ex[1]) && $ex[1] == "252") {
                $ircops = $ex[3];
                echo date('h:i:s') . " ircops: " . $ircops;
                echo "\n";
            }

            if (isset($ex[1]) && $ex[1] == "254") {
                $channels = $ex[3];
                echo date('h:i:s') . " channels: " . $channels;
                echo "\n";
            }

        }

    }
    fclose($irc);
}

foreach ($servers as $server) {
    echo date('h:i:s') . " getting statistics for " . $server;
    echo "\n";
    get_statistics($server, '6667');
}

exit;
?>

这是输出:

root@li140-48:~# php bot.php
11:04:23 getting statistics for irc.freenode.net
11:04:24 network: irc.freenode.net
11:04:24 server: sendak.freenode.net
11:04:24 users: 90601
11:04:24 servers: 26
11:04:24 ircops: 22
11:04:24 channels: 50958
11:05:25 getting statistics for irc.icq.com
11:05:26 network: irc.icq.com
11:05:26 server: irc-k01a.orange.icq.com
11:05:26 users: 2671
11:05:26 servers: 3
11:05:26 ircops: 16
11:05:26 channels: 810
11:06:26 getting statistics for irc.quakenet.org
11:06:28 network: irc.quakenet.org
11:06:28 server: blacklotus.ca.us.quakenet.org
11:06:28 users: 37648
11:06:28 servers: 40
11:06:28 ircops: 67
11:06:28 channels: 26711
root@li140-48:~#

我的问题是导致服务器被索引之间的延迟的原因是什么?

在10.24:19,它已经完成了irc.icq.com的索引,所以此时它应该断开连接并立即索引数组中的下一个服务器,但不是这样,它等待一分钟才这样做,但是我在剧本中没有提到一分钟?

希望一些PHP大师可以伸出援助之手!

1 个答案:

答案 0 :(得分:1)

60秒是默认的套接字超时。在您已经获得了所有可以获得的信息后,您将继续尝试从流中读取 - 直到最终达到超时。