1

I have requirement to develop a PHP server socket and a C client socket , visa versa.

I am thorough with TCP sockets in C and its concept.

I am stuck in on last thing.

I am able to send a whole structure from C client socket as follows

typedef struct _test {
char str[32];
char c;
int i;
float f;
}test;

//Some Coding ...

memset(&t,'\0',sizeof(test));

strcpy(t.str,"Sunny"); //String
t.c = 'M'; //Char
t.i = 26; //Integer
t.f = 98.8; //Float

//Send test STRUCT to server
if(send(sockfd,(void *)&t,sizeof(t),0) < 0)  
{   
    perror("Send failed ");
    exit(0);
}

//Some Coding ...

I am receiving this structure at PHP server socket as follows

...

 $client = socket_accept($socket);

   $input = socket_read($client, 1024);

   $arr = unpack("Z32Str/a1Chr/iInt/fFlt", $input);

   echo $arr['Str']; //Print String

   echo $arr['Chr']; //Print Char

   echo $arr['Int']; //Print Int

   echo $arr['Flt']; //Print Float

...

I am getting string and char properly but am not getting Integer and Float properly , i am sure its network to host byte order (little endian,big endian) problem.

i.e. am getting integer value as 436207616

Can any one please tell me how to make equivalent fucntions to ntohl and ntohs in PHP.


P.S. :- Am quite new at PHP ... Please help

Ankur Bhadania
  • 4,123
  • 1
  • 23
  • 38
Sunny Shukla
  • 537
  • 1
  • 6
  • 17
  • 3
    The problem is most likely not byte ordering, but structure padding. Remember that most C compilers *pad* their structures so that fields start at even addresses. After the character in the structure there is most likely a 3-byte padding field that you don't handle in the PHP code. You can easily veryfy this by printing `sizeof(struct _test)` in the C source, I would bet it's 44 and not 41. – Some programmer dude Jan 03 '15 at 07:27
  • ya joachim , that's right... I am checking it further... But can u then suggest how to write unpack function for such cases in PHP , as am quite new at php. – Sunny Shukla Jan 03 '15 at 07:55
  • Most compilers support *packing* of structures, to minimize padding or remove it completely. Of you can work with the padding but add three dummy bytes after `'Chr'` when unpacking in PHP. Just note that changing the structure will change the padding. – Some programmer dude Jan 03 '15 at 07:58
  • Adding to Joachim, I would send the size of an empty structure to the server at application start, just so you can be sure that it is what you're expecting on the PHP side. – Christian Jan 03 '15 at 08:00
  • You can either force your compiler not to add padding or you may reorganize your structure in the following order: str, i, f, c – Artur Jan 03 '15 at 08:31

1 Answers1

-1

i have disabled the structure padding in C as below and it worked .....

How to disable structure padding ? As Follows .... Following is the way to disable structure padding using pragma in C.

#pragma pack(push, 1)
//Define your structure here
#pragma pack(pop)
//Structure padding is re enabled.


#pragma pack(push,1)
typedef struct _test {
    char str[32];
    char c;
    int i;
    float f;
}test;
#pragma pack(pop)

Or:

I have kept padding on in C and do following at php side , and it worked ....

$arr = unpack("Z32Str/z4Chr/iInt/fFlt", $input);
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
Sunny Shukla
  • 537
  • 1
  • 6
  • 17
  • The preferred way to do it in gcc and clang is to use __attribute__((packed)) instead of the pragma. – dho Jan 03 '15 at 17:29
  • ya i also agree with @dho ... see this post http://stackoverflow.com/questions/11770451/what-is-the-meaning-of-attribute-packed-aligned4 – Sunny Shukla Jan 05 '15 at 07:29
  • donot use $input = trim($input); in php code before unpack method , as this method will strip whitespace (or other characters) from the beginning and end of a string which will make the binary string unreadable for any unpack methods ..... – Sunny Shukla Jan 09 '15 at 05:54
  • trim will strip an ordinary space, a tab, a new line (line feed), a carriage return, the NUL-byte and a vertical tab.... – Sunny Shukla Jan 09 '15 at 06:09
  • Wow someone decreased my reputation point for the answer. He he that's funny. – Sunny Shukla Sep 26 '16 at 10:50