note 10629 deleted from function.fsockopen by jimw
| From: | jimw@php.net | Date: | Sat, 08 Dec 2001 21:56:38 +0000 |
| Subject: | note 10629 deleted from function.fsockopen by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-22251@lists.php.net to get a copy of this message | ||
Here's some example code to query a Q3arena gameserver (also applies to halflife servers). Alot
of people seem to have problems with doing sending/recieving UDP packets while not knowing how many
lines of data the server will return.
The trick is to do a socket_set_blocking($fp,0); while reading data.
<?php
//Q3 query/parsescript by M7@bigfoot.de 01/2001
//Open the network for transmission.
$fp = fsockopen("udp://$serverip",$port, &$errno, &$errstr,10);
if (!$fp)
{
//Something didn't work....
echo "ERROR: $errno - $errstr\n";
exit;
}
else
{
//Send statusrequest.
fwrite($fp,"ÿÿÿÿgetstatus\x00");
//wait till we receive a header and store it (isn't used at the moment)
//2Do, use header to determine, if we're really talking to the right server/getting the right
data.
$header=fgets($fp, 20);
//now we get the the servers statusdata in one line
$data=fgets($fp, 1500);
//Prepare socket for end of transmission from gameserver.
socket_set_blocking($fp,0);
//Now the server lists it's clients, one each line
$playernum=0;
while(!feof($fp))
{
//put the playerinformation in an array, while counting total number of players.
$player[$playernum]=fgets($fp, 100);
$playernum++;
}
//Closing connetion
fclose($fp);