Jump to content


Photo

Server Query Help


  • Please log in to reply
2 replies to this topic

#1 Psycho

Psycho
  • Moderators
  • Move along, nothing to see here
  • 11,891 posts
  • LocationCanada

Posted 16 May 2006 - 04:28 PM

I have a class with a server query developed by someone else. The problem is that if the server being queried is down or does not respond the function errors out instead of exiting gracefully. I would appreciate anyones help in correcting this.

Here is the portion of the code that has the problem (NOTE: replaced () with [] on first few lines to prevent posting error):

function battlefield_2($ip,$port=29900,$players=true)
{ $transmission = chr[254].chr[253].chr[0];
  $transmission.= chr[4].chr[5].chr[6].chr[7];
  $transmission.= chr[255].chr[255].chr[255].chr[1];
  # (begin) socket code
  $socket=socket_create(AF_INET,SOCK_DGRAM,SOL_UDP);
  socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,array('sec'=>$this->timeout,'usec'=>0));
  if (!$socket) return; $packet=null; $this->packets=null;
  if (!socket_sendto($socket,$transmission,strlen($transmission),0x100,$ip,$port)) return;[color=#FF0000]while (!$this->receiving($packet)) {
    if (!socket_recvfrom($socket,$packet,2048,0,$client['ip'],$client['port'])) return;
    }
  .
  .
  .

If there is no response from the server, the last line of code will pause on that line for the specified timeout period then the following error is returned:

[!--coloro:#006600--][span style=\"color:#006600\"][!--/coloro--]Warning: socket_recvfrom() unable to recvfrom [11]: Resource temporarily unavailable in battlefield-2-class.php on line 37[!--colorc--][/span][!--/colorc--]

Looking at the code, to me it looks as if that line is specifically there to test if the server is responding. But, that is not the case. Again, any help is appreciated.
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 16 May 2006 - 04:33 PM

if (!@socket_recvfrom($socket,$packet,2048,0,$client['ip'],$client['port'])) return;


#3 Psycho

Psycho
  • Moderators
  • Move along, nothing to see here
  • 11,891 posts
  • LocationCanada

Posted 16 May 2006 - 04:48 PM

Thanks very much. I've been working on an application utilizing that server query class, but didn't want to release my code until that was fixed.

By the way I had a problem with the forum not accepting part of the code and had to edit my post several times until I was able to get it all in there - not sure how much I had there when you responded.

Anyway, I have one follow up question. Is there any way to "quickly" determine if the server is not there/not responding vs. just not responding to the query? It would be helpful if the function "quickly" exited if there is no server at the specified IP:Port instead of waiting for the timeout period to expire. Hope that makes sense.
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users