UDP packet
| From: | Torben Dehn | Date: | Wed, 01 Jan 2003 16:34:05 +0000 |
| Subject: | UDP packet | ||
| Groups: | php.windows | ||
| Request: | Send a blank email to php-windows+get-17612@lists.php.net to get a copy of this message | ||
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
?>