Forum Moderators: coopster

Message Too Old, No Replies

socket connections

         

tunnelduck

3:50 am on Feb 23, 2004 (gmt 0)

10+ Year Member



I currently have a script that i use to test if servers are up and what not, and im having the problem that when one of the servers is down, it just sends the browser into an endless stall, leaving me looking at an "M" turning into a dinosaur and back. below is a abridged version of the code:

$ips = '1.2.3.4-1.2.3.4-1.2.3.4';
$ports = '1.2.3.4-1.2.3.4-1.2.3.4';

$ip = explode('-', $ips);
$port = explode('-', $ports);

$total = count($ip);

/* START THE LOOP */
$num = 0;
while ($num < $total) {
$buffer = "";
$fp = fsockopen("udp://$ip[$num]", "$port[$num]", $errno, $errstr);
stream_set_timeout($fp, 5); // Socket Timeout

if (!$fp)
{

display server is down

}
else
{
fwrite($fp,"˙˙˙˙details\x00");

fclose($fp);

read the returned packet and display the info in a table

}
$num++; //add one to $num
}
/* END THE LOOP */

If anyone could help me figure out a way to get it not to hang, it would be greatly apprciated.

tunnelduck

4:19 am on Feb 23, 2004 (gmt 0)

10+ Year Member



hmm, of course it is after i finally post that i answer my own question. i looked back at the php manual for fsockopen and noticed the little blurp " UDP sockets will sometimes appear to have opened without an error, even if the remote host is unreachable. The error will only become apparent when you read or write data to/from the socket. The reason for this is because UDP is a "connectionless" protocol, which means that the operating system does not try to establish a link for the socket until it actually needs to send or receive data." Which basically meant i had to send a packet to the server and then determine if i got a reply or not.