Hello !
I am very new to PHP so please don't laugh if my code really sucks ;=)
What i am trying to do is :
1) Opening an UDP connectión to a server (That works allright)
2) Making a query to the server (That works allright)
3) Recieve an unknow number of UDP packets with an unknown packet size (This is my problem)
I don't know the packet size and i don't know how many packets the server will return...
Take a look at the WHILE loop because thats where it "hangs".
If i remove the WHILE loop and just make a fread($socket,1024) it will return some data.
My questions are as follow :
1) How can i make sure that ALL data has been recieved ?
2) Is it possible to read each UDP packet into it's own variable (array) ???
I would be very happy if someone could answer one or all question.
<?
$ip = "192.246.40.65";
$port = 27950;
$command = "ÿÿÿÿgetservers 60 empty full";
$data = '';
Echo "Connecting to Master Server : $ip:$port<br>";
$socket = fsockopen("udp://$ip",$port,$errno,$errstr,2);
if (!$socket)
{
echo "There was an error connecting to the Master Server<br>";
echo "Error returned was : $errorno and $errostr";
exit;
} //Socket error
else
{
echo "Connection established ..<br>";
If (!fwrite($socket,$command))
{
echo "There was an error querying the master server ..";
exit;
} //Fwrite error
echo "Query complete ..<br>";
while (!feof($socket))
{
echo "Reading data ..<br>";
$data .= fread($socket,4096);
} //End of read section
echo "$data<br>";
fclose($socket);
echo "Connection closed ..<br>";
} //End of main loop
?>
A data packet returned could look like this
ÿÿÿÿgetserversResponse\ÙÃ%N”H\±|Em9\Põã ‚p\Çð\%m\ÕÝ®«q \ÃCïÅm8\Ï,¬[m9\Põãƒb\B–m8\ém8\B—lhm8\Aqvžm8\Pýo m9\Ô€fm:\Ùž–4y\Ï,ŽbmL>@ m8@ö.)m8\Õ’¾–iŒ\ØÄòfm8\Â]‘¾mL\„±ÔŽm8\Ô‰FpØ>]Éei>Pohmƒ@“m9\ØV(mB\عÈm8\ÂuŠþm;@ëâÂm8\B–€m=\Ô+öm8\’Yi‰\Çg´ m8>]Éeiy\B–Øm?\Øþ‡m=\Õ’¿i‡\B\Ÿ‡m=\ƒÓâÈm9\Ã…žm8\B–Îm’\ÙL m8\Õ@‹îm9\QíZm8\B—´m8\¬ªëm8@-îrm8\BuÓšm9\ÃÓm8\?¬1mN\ˆò½œm8\Õm8\Ïbðm8\PÃò.m8>Jœ\йÊm8\Pøj=m8\ÌHm8\Ø ÒÙmB\Õ_°,m8\Ú,¦m9\Âò-–x>PLy\Põã €½\ÕS7ÔIÅm8>Puím8\CS•‘m8\Õݲq \Âʧ'\AqvËm8\Õ‘$m8>Ø Vm8\™¬Òm8\Õ’£Éj \Ó Öm8\Ø ÒÙmA\Õ‘#m8\Ï,¨Fm8>Pokma\Q„’iŒ\ÕÝ¥q!\Q.m8\ÃNõm8\Ø@ˆm8\Ã&mm8\Ï©ºm9:ëøm8\ȱåöm9\Pýom8\Põã zÊ\ËÌÍÞm8\Õrùm8\¦ßm8\Õ]µÉm9\ÕÝ®ªq$\ȱåöm8>ÔIÀm8\Æ¥\m8\EOT