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
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);