简体   繁体   English

通过php中的特定端口(fsockopen)ping服务器

[英]ping a server thru a specific port (fsockopen) in php

function checkServer($domain, $port=80) { global $checkTimeout, $testServer; 函数checkServer($ domain,$ port = 80){全局$ checkTimeout,$ testServer;

$status = 0;

$starttime = microtime(true);
$file = @fsockopen ($domain, $port, $errno, $errstr, $checkTimeout);
$stoptime = microtime(true);

if($file)
{
    fclose($file);
    $status = ($stoptime - $starttime) * 1000;
    $status = floor($status);
}
else
{
    $testfile = @fsockopen ($testServer, 80, $errno, $errstr, $checkTimeout);
    if($testfile)
    {
        fclose($testfile);
        $status = -1;
    }
    else
    {
        $status = -2;
    }
}

return $status;

} }

the testserver is google.sk, and checkTimeout is 10 seconds. 测试服务器是google.sk,checkTimeout是10秒。 This actually works, but when i try to run it in a loop for about 50 times, and do other stuff (mysql queries and things like that), it's not slow, but it causes 100% load of my CPU until the script ends. 这实际上是可行的,但是当我尝试在一个循环中运行它大约50次并执行其他操作(mysql查询和类似的操作)时,它的运行速度并不慢,但是会导致我的CPU 100%的负载,直到脚本结束。 It's a single apache proccess that drives my cpu crazy ... So i wanted to ask you if you have any ideas about it. 这是一个使Apache的CPU疯狂的单一apache进程。所以我想问问您是否对此有任何想法。 maybe some tip how to do the same in python or bash or so will be appreciated. 也许一些技巧,如何在python或bash中执行相同的操作,或者将被赞赏。

Thank you for the responses :) 感谢您的回复 :)

Use CURL 使用CURL

this is an example how to conversion fsockopen to CURL PHP fsockopen to curl conversion 这是一个示例,如何将fsockopen转换为CURL PHP fsockopen进行curl转换

Good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM