?
Solved

FSockOpen() Too Slow!

Posted on 2004-04-20
3
Medium Priority
?
1,459 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 750 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

719 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