View Full Version : sending raw hex data from php to local process

02-08-2006, 07:51 PM
I am working on webGUI for networking application.There are around 30 to 35 network related parameters user would enter by PHP forms. I already have PHP page and I can print all this parameters entered by user in PHP script.
I have another process running on same system (Linux) and uses socket to receive data from PHP script. Here is the sample code
// PHP code
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)
socket_bind($sock, "", 1000);
$msg_code =0xc010;
$msg_len = 0x10;
$msg_data = 0xa0b012cc;

$msg = $msg_code
$msg .= $msg_len
$msg .= $msg_data
socket_write($msgsock, $msg, strlen($msg);

// C code user process
// User process written in C has following structure where I want to parse and use the data sent by PHP
// I want to map incoming data with this internal structure
ushort msg_code;
uchar msg_len;
unsigned long msg_data

My local process is receiving the data sent by PHP script but it is not in hex.
For e.g if I send 0xC0 from php then I receive 02 00
if I send 0xb2 from php then I receive 01 02

I just want packed hex data so that I can co related with my internal structure in the context of my local user process.

Can anybody help me how I can do this or am I missing anything?.
Thanks in Advance

02-08-2006, 07:58 PM
mm, something like pack then? http://us3.php.net/pack

02-08-2006, 08:15 PM
I did try pack but still data I receive in another process is not in Hex.
pack work very well in PHP script but not when we use socket.
I can print any hex number in PHP script but when I transmit that with socket it is not getting transmitted in hex.

I tried like this also
socket_write($msgsock, 0xc0, strlen($msg);

But I dont get data in hex in another process

02-08-2006, 08:36 PM
how about sprintf (http://us3.php.net/manual/en/function.sprintf.php ):

$msg_data = 0xa0b012cc;
echo sprintf("%X", $msg_data); // A0B012CC
or, you could send the hex as a string .

$msg_data = 'a0b012cc';
echo $msg_data; // a0b012cc

02-08-2006, 10:26 PM
$msg_data = 'a0b012cc';
echo $msg_data; // a0b012cc

I did try string but some how get following

a0b012cc ==> 00 00 01 00 01 02 02 02

There are two issues here

1) data is not packed (even if I use pack command)
2) Char greater than 9 is not getting transmitted correctly (a ==>00 b ==>01 c ==>02). I want to see hex data as it is I sent from PHP script (a0 b0 12 cc)

02-08-2006, 10:49 PM
what if you send as decimal?

$msg_code = sprintf("%d", 0xc010);

then decode it on the other end

sprintf("%x", msg_code);

I'm making guesses though, hopefully someone else has some suggestions..

02-09-2006, 12:51 AM
I asked someone on IRC, he had this to say:

17:45 `> static: any thoughts on this? http://www.codingforums.com/showthread.php?t=79166 it's php related but the concept isn't
17:46 static> ew, binary protocols
17:46 `> is there a way I can help him
17:46 static> if you're going to send binary data over the wire then you have to be very careful
17:47 static> first of all, C programs should always use integer types where the size is assured
17:47 static> like uint32_t if you want an unsigned 4-byte integer
17:48 static> short, int and long can vary in size between compilers and platforms
17:48 static> secondly, always always always convert binary data to network byte order before sending
17:49 static> different machines store integers with the bytes ordered differently
17:50 static> if you just write() a C integer from a program running on a PC and read() it from a program running on a mac, you won't get the same integer
17:52 static> and one shouldn't ever send a C struct directly over the network, the compiler may add padding between the struct members to satisfy machine alignment
17:53 static> basically, it's really a lot easier to just send a string representation
17:53 `> he said he tried sending a string (that's one of the things I suggested) but apparently it didn't make a difference unless his test was incorrect.
17:54 static> I mean, is sprintf("%i", foo) and foo = strtol(str, NULL, 10) really that hard?
17:54 `> ok, I'll just cp everything you said here, <3
17:54 static> I don't think he really has a very good understanding of what he's doing
17:55 static> he's assigning ints to $msg, then using string concatenation to join them with other ints
17:56 static> I think scripting languages like PHP prevent people from ever actually dealing with binary data such that they don't know what it is :p