Page 1 of 1

Sending data using UDP

Posted: Tue Dec 27, 2011 8:56 am
by larenge
I want to send the following data using UDP.

$packet[0] = 0x01;
$packet[1] = 0x11;
$packet[2] = 0xF1;
$packet[3] = 0xD1;
$packet[4] = 0xF1;
$packet[5] = 0xB1;
$packet[6] = 0xC1;
$packet[7] = 0xF1;

I used int socket_sendto ( resource $socket , string $buf , int $len , int $flags , string $addr [, int $port = 0 ] ) function to send.

But it accepts a string.How can i convert interger array and send data to UDP server, So that data is treated as 1 byte each.

Thanks.

Re: Sending data using UDP

Posted: Tue Dec 27, 2011 4:27 pm
by Eric!
As a string that would be: $packet="\x01\x11\xf1\xd1\xf1\xb1\xc1\xf1";

You could also loop through your array

Code: Select all

$string="";
foreach($packet as $value)
{
    $string.=chr(hexdec($value));
}

Re: Sending data using UDP

Posted: Tue Dec 27, 2011 11:32 pm
by larenge
PROBLEM SOLVED
Thanks for your Reply. I have checked your code.It worked by ::

Code: Select all

foreach($packet as $value) {
$string.= chr($value);
}
and also by

Code: Select all

$packet = "\x01\x11\xF1\xD1\xF1\xB1\xC1";
I have same probelm in reverse direction while receiving the packet.
Function for receive using UDP accepts string.But I have to treat it as Hexadecimal value and add in array.
How can i do it.