<?php$fp = fsockopen("udp://74.54.186.226", 27910, $errno, $errstr);if (!$fp) { echo "ERROR: $errno - $errstr<br />\n";} else { fwrite($fp, "\xFF\xFF\xFF\xFFping\x00"); $coutput = fread($fp, 7); $cresponse = "\xFF\xFF\xFF\xFFack"; if(strcmp($coutput,$cresponse) == 0){ echo "server up"; } else{ echo "server down"; } fclose($fp);}?>
<?php$serv1 = fsockopen("udp://www.aceintergraphs.com", 27910, $errno, $errstr);$serv2 = fsockopen("udp://www.aceintergraphs.com", 27911, $errno, $errstr);$serv3 = fsockopen("udp://www.aceintergraphs.com", 27912, $errno, $errstr);$serv4 = fsockopen("udp://www.aceintergraphs.com", 27913, $errno, $errstr);$serv5 = fsockopen("udp://www.aceintergraphs.com", 27914, $errno, $errstr);$serv6 = fsockopen("udp://www.aceintergraphs.com", 27915, $errno, $errstr);$serv7 = fsockopen("udp://www.aceintergraphs.com", 27916, $errno, $errstr);$serv8 = fsockopen("udp://www.aceintergraphs.com", 27917, $errno, $errstr);$serv9 = fsockopen("udp://www.aceintergraphs.com", 27918, $errno, $errstr);$serv10 = fsockopen("udp://www.aceintergraphs.com", 27919, $errno, $errstr);$serv11 = fsockopen("udp://www.aceintergraphs.com", 27920, $errno, $errstr);$serv12 = fsockopen("udp://www.aceintergraphs.com", 27921, $errno, $errstr);if ($serv1){ fwrite($serv1, "\xFF\xFF\xFF\xFFping\x00"); $soutput = fread($serv1, 7); $sresponse = "\xFF\xFF\xFF\xFFack"; if(strcmp($soutput,$sresponse) == 0){ echo '<br>Fezonia'; echo '<img src="images/sonline.gif" width="100" height="8" alt="The Fezonia Realm is Online">'; }else{ echo '<br>Fezonia'; echo '<img src="images/soffline.gif" width="100" height="8" alt="The Fezonia Realm is Offline">'; }fclose($serv1);}if ($serv2){ fwrite($serv2, "\xFF\xFF\xFF\xFFping\x00"); $soutput = fread($serv2, 7); $sresponse = "\xFF\xFF\xFF\xFFack"; if(strcmp($soutput,$sresponse) == 0){ echo '<br>Feasa'; echo '<img src="images/sonline.gif" width="100" height="8" alt="The Feasa Realm is Online">'; }else{ echo '<br>Feasa'; echo '<img src="images/soffline.gif" width="100" height="8" alt="The Feasa Realm is Offline">'; }fclose($serv2);}if ($serv3){ fwrite($serv3, "\xFF\xFF\xFF\xFFping\x00"); $soutput = fread($serv3, 7); $sresponse = "\xFF\xFF\xFF\xFFack"; if(strcmp($soutput,$sresponse) == 0){ echo '<br>Harlband'; echo '<img src="images/sonline.gif" width="100" height="8" alt="The Harlband Realm is Online">'; }else{ echo '<br>Harlband'; echo '<img src="images/soffline.gif" width="100" height="8" alt="The Harlband Realm is Offline">'; }fclose($serv3);}if ($serv4){ fwrite($serv4, "\xFF\xFF\xFF\xFFping\x00"); $soutput = fread($serv4, 7); $sresponse = "\xFF\xFF\xFF\xFFack"; if(strcmp($soutput,$sresponse) == 0){ echo '<br>Sodoara'; echo '<img src="images/sonline.gif" width="100" height="8" alt="The Sodoara Realm is Online">'; }else{ echo '<br>Sodoara'; echo '<img src="images/soffline.gif" width="100" height="8" alt="The Sodoara Realm is Offline">'; }fclose($serv4);}if ($serv5){ fwrite($serv5, "\xFF\xFF\xFF\xFFping\x00"); $soutput = fread($serv5, 7); $sresponse = "\xFF\xFF\xFF\xFFack"; if(strcmp($soutput,$sresponse) == 0){ echo '<br>Binndroa'; echo '<img src="images/sonline.gif" width="100" height="8" alt="The Binndroa Realm is Online">'; }else{ echo '<br>Binndroa'; echo '<img src="images/soffline.gif" width="100" height="8" alt="The Binndroa Realm is Offline">'; }fclose($serv5);}if ($serv6){ fwrite($serv6, "\xFF\xFF\xFF\xFFping\x00"); $soutput = fread($serv6, 7); $sresponse = "\xFF\xFF\xFF\xFFack"; if(strcmp($soutput,$sresponse) == 0){ echo '<br>Elboria'; echo '<img src="images/sonline.gif" width="100" height="8" alt="The Elboria Realm is Online">'; }else{ echo '<br>Elboria'; echo '<img src="images/soffline.gif" width="100" height="8" alt="The Elboria Realm is Offline">'; }fclose($serv6);}if ($serv7){ fwrite($serv7, "\xFF\xFF\xFF\xFFping\x00"); $soutput = fread($serv7, 7); $sresponse = "\xFF\xFF\xFF\xFFack"; if(strcmp($soutput,$sresponse) == 0){ echo '<br>Eskaara'; echo '<img src="images/sonline.gif" width="100" height="8" alt="The Eskaara Realm is Online">'; }else{ echo '<br>Eskaara'; echo '<img src="images/soffline.gif" width="100" height="8" alt="The Eskaara Realm is Offline">'; }fclose($serv7);}if ($serv8){ fwrite($serv8, "\xFF\xFF\xFF\xFFping\x00"); $soutput = fread($serv8, 7); $sresponse = "\xFF\xFF\xFF\xFFack"; if(strcmp($soutput,$sresponse) == 0){ echo '<br>Eldorian'; echo '<img src="images/sonline.gif" width="100" height="8" alt="The Eldorian Realm is Online">'; }else{ echo '<br>Eldorian'; echo '<img src="images/soffline.gif" width="100" height="8" alt="The Eldorian Realm is Offline">'; }fclose($serv8);}if ($serv9){ fwrite($serv9, "\xFF\xFF\xFF\xFFping\x00"); $soutput = fread($serv9, 7); $sresponse = "\xFF\xFF\xFF\xFFack"; if(strcmp($soutput,$sresponse) == 0){ echo '<br>Shamka'; echo '<img src="images/sonline.gif" width="100" height="8" alt="The Shamka Realm is Online">'; }else{ echo '<br>Shamka'; echo '<img src="images/soffline.gif" width="100" height="8" alt="The Shamka Realm is Offline">'; }fclose($serv9);}if ($serv10){ fwrite($serv10, "\xFF\xFF\xFF\xFFping\x00"); $soutput = fread($serv10, 7); $sresponse = "\xFF\xFF\xFF\xFFack"; if(strcmp($soutput,$sresponse) == 0){ echo '<br>Redaila'; echo '<img src="images/sonline.gif" width="100" height="8" alt="The Redaila Realm is Online">'; }else{ echo '<br>Redaila'; echo '<img src="images/soffline.gif" width="100" height="8" alt="The Redaila Realm is Offline">'; }fclose($serv10);}if ($serv11){ fwrite($serv11, "\xFF\xFF\xFF\xFFping\x00"); $soutput = fread($serv11, 7); $sresponse = "\xFF\xFF\xFF\xFFack"; if(strcmp($soutput,$sresponse) == 0){ echo '<br>Skara'; echo '<img src="images/sonline.gif" width="100" height="8" alt="The Skara Realm is Online">'; }else{ echo '<br>Skara'; echo '<img src="images/soffline.gif" width="100" height="8" alt="The Skara Realm is Offline">'; }fclose($serv11);}if ($serv12){ fwrite($serv12, "\xFF\xFF\xFF\xFFping\x00"); $soutput = fread($serv12, 7); $sresponse = "\xFF\xFF\xFF\xFFack"; if(strcmp($soutput,$sresponse) == 0){ echo '<br>Prison'; echo '<img src="images/sonline.gif" width="100" height="8" alt="The Prison is Online">'; }else{ echo '<br>Prison'; echo '<img src="images/soffline.gif" width="100" height="8" alt="The Prison is Offline">'; }fclose($serv12);}?>
<?php$servarr = array('Fezonia'=>27910, 'Feasa'=>27911, 'Harlband'=>27912, 'Sodoara'=>27913, 'Binndroa'=>27914, 'Elboria'=>27915, 'Eskaara'=>27916, 'Eldorian'=>27917, 'Shamka'=>27918, 'Redaila'=>27919, 'Skara'=>27920, 'Prison'=>27921 );$pingtext="\xFF\xFF\xFF\xFFping\x00";$acktext="\xFF\xFF\xFF\xFFack";foreach ($servarr as $key=>$val) { $errno=0; $errstr=''; $sock=fsockopen("udp://www.aceintergraphs.com",$val,$errno,$errstr); if ($errno || !$sock) { // an error. log it, notice, whatever. your choice. $online=false; } else { fwrite($sock,$pingtext); $resp=fread($sock,7); $online=(strcmp($resp,$acktext)==0); fclose($sock); } echo "<br>{$key}<img src=\"images/s",($online?"on":"off"), "line.gif\" width=\"100\" height=\"8\" alt=\"The {$key} Realm is ", ($online?"On":"Off"),"line\" />";}
A friend of mine cleaned up your code a little
However, it should be noted that the refactored code will be significantly slower than the original to obtain all its results.The original sent out all the UDP requests in parallel, and thus only had to wait for the results as long as the single slowest reply took to arrive.
Can you provide benchmarks for that assertion? I understand fread()/fwrite() are supposed to function asynchronously, but those calls are mixed with necessarily synchronous code in both samples. In the original code, all of the sockets would be opened asynchronously, but the writing to/reading from would happen in order of appearance, yes? e.g., the second write does not begin until after the first response has been received.
About the servers that do not return the ping...how does the original code bypass the default timeout of 60 seconds? The actual page loads quickly, so either all servers are returning *something*, or the timeout is being set somewhere other than the sample provided...? All of the offline servers triggered the timeout when I ran the code.
You're right. Brain fart on my part; sorry for the noise.
A guess: If the orignal page is being hosted on the same system as the Q2 servers being pinged, perhaps the OS kernel is helping to shortcut the delay by providing an 'unreachable' response, resulting in an immediate EOF on the fread().