Jump to content

Archived

This topic is now archived and is closed to further replies.

Psycho

Server Query Help

Recommended Posts

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):

[code]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;
    }
  .
  .
  .[/code]

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.

Share this post


Link to post
Share on other sites
[code]
if (!@socket_recvfrom($socket,$packet,2048,0,$client['ip'],$client['port'])) return;
[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.