note 41089 added to function.fsockopen

From: Date: Mon, 29 Mar 2004 23:15:09 +0000
Subject: note 41089 added to function.fsockopen
Groups: php.notes 
Request: Send a blank email to php-notes+get-67348@lists.php.net to get a copy of this message
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 ! ---- Manual Page -- http://www.php.net/manual/en/function.fsockopen.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+41089 Delete -- http://master.php.net/manage/user-notes.php?action=delete+41089&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+41089&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#67348) next »