Click here to Skip to main content
14,826,961 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can i pack an object in php and send it over udp?
i have a packet class that has 6 propertis :
/** @var 32int  */
        private $headerSize;
        /** @var 32int  */
        private $highSessionID;
        /** @var 32int  */
        private $lowSessionID;
        /** @var array (char)  */
        private $signature;
         /** @var 32int  */
        private $comandID;
         /** @var 32int  */
        private $dataSize;

i tried a simple client-server app that it send a string over udp but i want to send an object that have different type of data

Edited : I used pack and serialized functions , they were very helpful : ) but i have one more question :

I read manual in but i did not understand how to pack different type of data . i tried this :
$n = ord('a');
 $buf =  pack('IS',200000000,$n);

 $array = unpack('I/S', $buf);
 foreach ($array as $key => $value)
    echo "\$array[$key] = ". $value ."<br>\n";

output :
$array[1] = 97<br>

why 200000000 was not print ? how can i fix the code?

Updated 20-Oct-13 20:42pm
Richard MacCutchan 20-Oct-13 13:07pm
It's just the same, you send it as a stream of bytes and the receiving program unpacks it to its components. As long as both ends know the structure it should work.
kleymanx90 21-Oct-13 2:06am
Could u tell me how can i send data as a stream of bytes? socket_sendto fucntion get data as string
Richard MacCutchan 21-Oct-13 3:31am
A string is just a stream of bytes. Alternatively, you could convert all your numbers to strings and send them that way. Unfortunately I don't know php so cannot offer any code.
kleymanx90 21-Oct-13 4:04am
thank u : )

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900