Solved

FSockOpen() Too Slow!

Posted on 2004-04-20
3
1,435 Views
Last Modified: 2010-05-19
Hi everyone, I will start with what I wanted to do, and how I am doing it- maybe someone can help me re-invent the wheel.

I had an idea < dangerous > and wanted to write a small network monitoring system.  I succeeded!  The software written looks at servers and specifically open ports running on servers, ie 1433 is MS Sql server, port 1677 is Groupwise POA, etc...

Using PHP and fsockopen() function, I am able to check to see if the host is alive, the port is open, etc...  It works great when everything is up.  When systems are down, the page refresh is god-awful slow.  I set the timeout on the FSockOpen to 1 second, and the load time for the page is sometimes 15 to 30 seconds.

Here is the snippet of code from the PHP application:

while ( $row = mysql_fetch_array($result) ) {
     $sysid = $row["sysid"];
     $hostname = $row["hostname"];
     $port = $row["port"];
     $hostdesc = $row["hostdesc"];

     $sockpointer = fsockopen($hostname,$port,$errno,$errstr,1);
     if($sockpointer) //check that it returns a value, if true, then host/port is alive, if false, host/port is dead.
    {
      //do some stuff- found an open port
      fclose($sockpointer); //close port
    } //end if
    else
    {
     //do some stuff- found dead port
    } //end else
    flush(); //flush the socket
} //end while

$hostname, $port are values from a MySQL database getting shoved into this function

As I have said, the program is lightning fast when all servers are up, just sloooow when they do not respond.  The 1 second timeout doesn't seem to change this delay either.  I had tried different values all together.

This all being said- is there a faster way to accomplish what I am looking to do?  Is my while loop structure horribly written- I am only a novice PHP scripter.  Any help is much appreciated.  

Along with the 250 points, I will gladly mention anyone who helps out in my PHP headers.  I plan to release this tiny code on the internet to sites as hotscripts.com etc....

thanks!
0
Comment
Question by:jhines2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
CosminB earned 250 total points
ID: 10875391
use this function to see which is the slowest part ot the code

function timer()
{
  list($usec, $sec) = explode(" ", microtime());
  return ((float)$usec + (float)$sec);
}

... smth like this:
while ( $row = mysql_fetch_array($result) ) {
     $sysid = $row["sysid"];
     $hostname = $row["hostname"];
     $port = $row["port"];
     $hostdesc = $row["hostdesc"];

     $start = timer()
     $sockpointer = fsockopen($hostname,$port,$errno,$errstr,1);
     $stop1 = timer();
     echo 'fsockopen: ', $stop1-$start;
     if($sockpointer) //check that it returns a value, if true, then host/port is alive, if false, host/port is dead.
    {
      //do some stuff- found an open port
      fclose($sockpointer); //close port
      $stop2 = timer();
      echo 'if: ', $stop2-$stop1;
    } //end if
    else
    {
     //do some stuff- found dead port
      $stop3 = timer();
      echo 'else: ', $stop3-$stop2;
    } //end else
    flush(); //flush the socket ?!? actually flush() flushes the output buffer not the socket
} //end while


also this is from the manual: " Depending on the environment, the Unix domain or the optional connect timeout may not be available."
you could also try to use the socket functions(http://www.php.net/sockets). This functions are not enabled by default. You'll have to recompile your php wth the --enable-sockets option
0
 
LVL 1

Author Comment

by:jhines2000
ID: 11259223
Long time working on this project!

Still not working as fast as it could be.  Are there any other ideas out there?
0
 
LVL 1

Author Comment

by:jhines2000
ID: 11561684
I havent been able to figure this out yet.  I am going to abandon this question.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question