views:

76

answers:

1

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 :)

A: 

Use CURL

this is an example how to conversion fsockopen to CURL http://stackoverflow.com/questions/2957068/php-fsockopen-to-curl-conversion

Good luck

Danial
hello, thank you very much for your response, but i don't think that's what im looking for, cause i only want to check whether some port of some server is 'opened' and if there is some service running on this port ... the code i posted works very well, and also it's okay when i do it in a loop for 200 times. but when i add some other code to it, it starts to drain a lot of cpu usage.
Adka
sorry , what is that code you add ?
Danial