Sending data using UDP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
larenge
Forum Newbie
Posts: 2
Joined: Tue Dec 27, 2011 8:54 am

Sending data using UDP

Post 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.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Sending data using UDP

Post 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));
}
larenge
Forum Newbie
Posts: 2
Joined: Tue Dec 27, 2011 8:54 am

Re: Sending data using UDP

Post 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.
Post Reply