Search code examples
phppythonbashsocketsfsockopen

ping a server thru a specific port (fsockopen) in php


function checkServer($domain, $port=80) { global $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. 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. It's a single apache proccess that drives my cpu crazy ... So i wanted to ask you if you have any ideas about it. maybe some tip how to do the same in python or bash or so will be appreciated.

Thank you for the responses :)


Solution

  • Use CURL

    this is an example how to conversion fsockopen to CURL PHP fsockopen to curl conversion

    Good luck