note 41089 deleted from function.fsockopen by aidan
| From: | aidan@php.net | Date: | Sun, 11 Jul 2004 05:50:38 +0000 |
| Subject: | note 41089 deleted from function.fsockopen by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-73041@lists.php.net to get a copy of this message | ||
Note Submitter: mega-squall@beng-team.net
----
I wanted to connect my CounterStrike server in order to get live stats from the server. As told in
the doc you must use UDP to connect.
The matter was that fsockopen() was always returning true, even when the program was not running. As
the script was looking like :
<?
$fp = fsockopen ("udp://$srv_ip", $srv_port, &$errno, &$errstr, 1)
if (!$fp)
{
// Display error
}
else
{
// Request, read infos using fread() and fwrite() then display it
}
?>
... well you'll get that the script attempted to read/write through the socket, but as there
was no program listening on that port the script just hanged, as the read/write functions did run
until the 30 secs of PHP scipt execuction timeout were elapsed. Then of course you couldn't
even echo an error message.
One solution would be to have a separate script executed on a separate process which would request
the data and would timeout after 1 sec of execution. The script would return all the requested data
to the main thread if it has successfully read/wrote to the server, a "corrupted data" msg
if the answer did not match (another service running on that port for instance), and null in case of
timeout. Then you have a basic :
<?
if (!!$answer && $answer_is_good)
{
// Display your stuff
}
else
{
// Display the appropriate error message
// timeout || port busy
}
?>
Yet this needs to have a separate proccess with a different timeout. What mainly means to have a
second http server.
If anybody has an easier solution he's welcome !