[PHP] binary data over UDP with PHP?

2004-07-07 Thread coder_1024
I'm trying to send some binary data to a UDP server using PHP.  The examples
I've been able to find show sending binary data over TCP, or they show
sending text over UDP.

I'm constructing the messages using the below:

  $text_msg = "Hello, World\r\n";
  $binary_msg = chr(0x01).chr(0x02).chr(0x00).chr(0xAD);
  $binary_msg_size = 4;

I've tried a couple methods of sending the data:

  $fp = fsockopen("udp://" . $host,$port,);
  fwrite($fp,$binary_msg,$binary_msg_size);

and

  $sock = socket_create(AF_INET,SOCK_DGRAM,SOL_UDP);
  socket_sendto($sock,$binary_msg,$binary_msg_size,0,$host,$port);

In either case, a UDP packet is sent, but with a zero data size.  If I
instead send the $text_msg, it works as expected.  For some reason sending
the binary data doesn't work.

Does anyone have insight into how to send binary data over UDP using PHP?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] binary data over UDP with PHP?

2004-07-07 Thread coder_1024
Thanks for the feedback.  As it turns out, the reason I was getting zero
data bytes in my UDP packet was that I had declared the packet variable
outside the function in which I was using it.

$packet = chr(0x01).chr(0x1d) etc

function doSomething($x)
{
// using $packet in here gives you an empty variable.
}

So I guess it was a very basic PHP language thing I was running into.  Guess
I've done too much Perl and assumed the variables were accessible in the
function. :-)


"Keith Greene" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> I use the following without problem:
>
>  $fp = fsockopen("udp://www.server.com", 24250, &$errno, &$errstr,
.2);
> if (!$fp) {
>   $status = "Server not available";
> } else {
>  $trigger =
> chr(hexdec('FF')).chr(hexdec('FF')).chr(hexdec('01')).chr(hexdec('00'));
>  fwrite($fp,$trigger);   # Send trigger to the
> status server
>  $junk = fread($fp, 4);  # discard echoed command
> from status server
> }
>
> Keith
>
> At 04:23 PM 7/7/2004, coder_1024 wrote:
> >I'm trying to send some binary data to a UDP server using PHP.  The
examples
> >I've been able to find show sending binary data over TCP, or they show
> >sending text over UDP.
> >
> >I'm constructing the messages using the below:
> >
> >   $text_msg = "Hello, World\r\n";
> >   $binary_msg = chr(0x01).chr(0x02).chr(0x00).chr(0xAD);
> >   $binary_msg_size = 4;
> >
> >I've tried a couple methods of sending the data:
> >
> >   $fp = fsockopen("udp://" . $host,$port,);
> >   fwrite($fp,$binary_msg,$binary_msg_size);
> >
> >and
> >
> >   $sock = socket_create(AF_INET,SOCK_DGRAM,SOL_UDP);
> >   socket_sendto($sock,$binary_msg,$binary_msg_size,0,$host,$port);
> >
> >In either case, a UDP packet is sent, but with a zero data size.  If I
> >instead send the $text_msg, it works as expected.  For some reason
sending
> >the binary data doesn't work.
> >
> >Does anyone have insight into how to send binary data over UDP using PHP?
> >
> >--
> >PHP General Mailing List (http://www.php.net/)
> >To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php