Jump to content


Photo

socket_recvfrom timeout?


  • Please log in to reply
No replies to this topic

#1 Lorian

Lorian
  • Members
  • PipPip
  • Member
  • 19 posts
  • LocationUnited Kingdom

Posted 17 April 2006 - 04:54 PM

I have got a script that sends a message to a server and waits for a response, but I can't find any way to make it timeout after say 2 seconds. Here is (the relevant bit of) my code:
function query ($ip, $port = 29900) {
  $buf = chr (254).chr (253).chr (0).chr (4).chr (5).chr (6).chr (7).chr (255).
  chr (255).chr (255).chr (1);
  $socket = socket_create (AF_INET, SOCK_DGRAM, SOL_UDP);
  socket_set_option ($socket, SOL_SOCKET, SO_BROADCAST, 1);
  socket_sendto ($socket, $buf, strlen ($buf), 0, $ip, $port);
  $packet = null;
  if (!socket_recvfrom ($socket, $packet, 2048, 0, $ip, $port)) {
    $players = -1;
  } else {
    $players = parse ($packet);
  }
  return $players;
}

Chris Spencer [ aka Lorian ]

[span style=\'font-size:8pt;line-height:100%\']PHP Freaks Community Member
Contact me: corrosivechris (at) gmail d0t com[/span]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users