I have a problem with UDP sockets in PHP. What I want to do is the 
following, i send a UDP message
to a server, and a reply should come instantly back. Now, the length of the 
message returned to me
is unknown. It can be anywhere from 1 to 8192 (or more) bytes. However, 
some strange problems
arise when i try to do this.

I first open a udp socket to my server. The socket handle is valid, and I 
send off my message.
(my socket is now in blockingmode, opened by fsockopen).

A message is returned to me and I receive one byte at the time in a loop. 
The receive procedure I
use is "fgetc". It states in the manual that this function retrieves 1 
byte, if the file pointer is at the
end of the stream, it should return false.

This is my code so far:
$query_handle = fsockopen("udp://$host", $port, &$errno, &$errstr, $timeout);
$request = "mymsg";
$databuffer = "";

if ($query_handle) {
  fwrite($query_handle, $request);
  $do = true;

   while ($do) {
    $tempdata = fgetc($query_handle);
    if (!$tempdata) {
     $do = false;
    } else {
     $databuffer .= $tempdata;

  echo("Received " . strlen($databuffer) . " bytes of data.");
} else {
  echo "Invalid socket handle - $errstr.\n";

Now, the fgetc doesn't seem to return false. Ever? Instead it sits there 
and waits for another byte, but since
there is no more data, my script sits here until the script times out (30 sec).

In short,
How can I get the number of bytes waiting for me in the socket buffer (so I 
can use fread to get all in one)
How can I set a timeout for fgetc? For example, it should wait for a char 
for x seconds.

Please help. I'm getting extremly frustrated about this now!

I've tried fread($mysockhandle, 8192) to receive all data but it doesn't 
work either. It seems like
fread also waits until 8192 bytes is in the buffer, THEN returns. But since 
i cannot assume the
message is a constant size, this is no good for me.

PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to