I am utilizing a server query function that I picked up from someone else. The query "appears" to have some error handling in place in case the server is unavailable, but I am getting errors.
The query waits the entire length of the timeout variable then I get the error:
Warning: socket_recvfrom() unable to recvfrom [11]: Resource temporarily unavailable in /vhost/home/v0023/websites/repeat-offenders.org/modules/BF2Server/battlefield-2-class.php on line 10
Here is the function (with line numbers):
01] function serverQuery($ip,$port=29900,$players=true)
02] { $transmission = chr(254).chr(253).chr(0);
03] $transmission.= chr(4).chr(5).chr(6).chr(7);
04] $transmission.= chr(255).chr(255).chr(255).chr(1);
05] # (begin) socket code
06] $socket=socket_create(AF_INET,SOCK_DGRAM,SOL_UDP);
07] socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,array('sec'=>$this->timeout,'usec'=>0));
08] if (!$socket) return; $packet=null; $this->packets=null;
09] if (!socket_sendto($socket,$transmission,strlen($transmission),0x100,$ip,$port)) return;
10] while (!$this->receiving($packet)) if (!socket_recvfrom($socket,$packet,2048,0,$client['ip'],$client['port'])) return;
11] socket_close($socket);
12] # (end) socket code
13] # (begin) packet parsing code
14] ksort($this->received);
15] $this->results=array('server'=>array(),'teams'=>array());
16] if ($players) $this->results['players']=array();
17] foreach ($this->received as $content)
18] { if (isset($content['server'])) $this->results['server']=array_merge($this->results['server'],$content['server']);
19] if (isset($content['players']) && $players) foreach ($content['players'] as $field=>$data) foreach ($data as $player=>$value) $this->results['players'][$player][preg_replace('/[^a-z]/i','',$field)]=$value;
20] if (isset($content['teams'])) $this->results['teams']=array_merge($this->results['teams'], $content['teams']);
21] }
Can anyone help to modify the code to 1) not error out if the server is not responding and 2) return immediately if the server is not responding?
Michael J