I've written some code that queries a half life gameserver and returns the number of players and the current map. This works pretty well except one rather annoying bug: There are circumstances under which the server will accept the connection, but will never send anything - like inbetween map changes, or if it gets started with an invalid map name. This means that my script just sits there waiting for a line of data that it never gets.
This is the relavent code. If anyone has any suggestions, i'd love to hear them:
$fp = fsockopen ("udp://$ip", $port, &$errno, &$errstr, 2);
if (!$fp) {
return "Could't connect to $ip";
$getgsfailed=1;
break;
} else {
fwrite($fp, "ÿÿÿÿinfo\n");
$serveroutput=fread($fp, 1);
socket_set_blocking($fp, 0);
$serveroutput.=fread($fp, 4096);
fclose($fp);
}
I have tried adding while(!feof($fp)) to the fread line, but if i do that it gets stuck in the while loop. I can only assume it never sends an EOF. I've also tried reducing the length parameter but that makes no difference either.