sending binary data via UDP

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I'm trying to send a packet of binary data to a UDP server.  If I send
a text string, it works fine.  If I attempt to send binary data, it
sends a UDP packet with 0 bytes of data (just the headers).  I can see
this because I'm running Ethereal and watching the packets.

I'm defining the packets as shown below:

  $text_msg = "Hello, world\r\n";
  $binary_msg = chr(0x01).chr(0x02).chr(0x03).chr(0x00).chr(0xA0);
  $binary_msg_size = 5;

Initially, I tried using
  $fp = fsockopen("udp://" . $server,$port,$errno,$errstr)

Since this didn't work, I then tried
  $sock = socket_create(AF_INET,SOCK_DGRAM,SOL_UDP);

For both methods, sending $text_msg worked, but sending $binary_msg
resulted in a UDP packet with 0 data bytes being sent.

I've found examples of sending binary data, but they used TCP, and
I've found examples of sending UDP, but they send text strings.

Anyone have experience with sending binary data to a UDP server?

Re: sending binary data via UDP

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. :-)
Quoted text here. Click to load it

Site Timeline