Click to See Complete Forum and Search --> : php client / c++ server socket program


coder87
October 30th, 2009, 09:28 AM
hi guys ,im trying to send serialized php object to c++ server. i dont have clear idea if it is possible to deserialize the php object in c++ code.i dont know how to convert it into c++ object and get the value out of it.

when im running the server and client ,the O/P is like this:
c++ server:

The socket was created

workingBinding Socket
*************************
*************************
The Client 127.0.0.1 is Connected...
recv: Success
Status = 60
Client: received {
Client: received �
Request Completed

php client:

Socket created ...
Socket connected ...
serialized object : O:4:"data":2:{s:1:"b";i:60;s:6:"string";s:11:"Hello World";}
This is my buffer
sent 60 bytes from socket_send(). Closing socket...

php client:


<?php

set_time_limit (0);
$address = 'localhost';//'192.168.1.93';
$port = 4000;
// Create the socket
if(($sockd = socket_create(AF_INET, SOCK_STREAM,SOL_TCP))<1)
die("Unable to create socket:" . socket_strerror(socket_last_error()));
else
echo "Socket created ...\n";

if(socket_connect($sockd,$address,$port) == FALSE)
die("Unable to connect:" . socket_strerror(socket_last_error()));
else
echo "Socket connected ...\n";

$buffer =" This is my buffer";

class data
{
var $b = 60;
var $string ="Hello World";
}
$obj1 = new data();
echo "serialized object : " . serialize($obj1) . "\n";
//if(socket_send($sockd, $buffer,1024,MSG_WAITALL) == false))
//if (false != ($bytes = socket_send($sockd, $buffer, 1024, MSG_WAITALL)))
if(($bytes=socket_send($sockd,serialize($obj1),1024, MSG_WAITALL))==false)
{
die("Unable to connect:" . socket_strerror(socket_last_error()));
}
else
{
echo "$buffer \n";
echo "sent $bytes bytes from socket_send(). Closing socket...";
}
unset($obj1);

//if(($buffer = socket_read($sockd, 1024)) == FALSE)
//die("Unable to read from socket:" . socket_strerror(socket_last_error()));

socket_close($sockd);
?>



c++ server :


#include <iostream>
#include <fstream>
#include <string>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/stat.h>
#include<fcntl.h>

using namespace std;
class Data
{
public:
char data1[255];
char val1[255];
};

int main()
{
int create_socket,new_socket,fd;
socklen_t addrlen;
struct sockaddr_in address;

if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)
cout<<"The socket was created\n";
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;//INADDR_ANY;inet_addr("localhost");
address.sin_port = htons(4000);
cout<<"\nworking";

if (bind(create_socket,(struct sockaddr *)&address,sizeof(address)) == 0)
cout<<"Binding Socket\n";

listen(create_socket,3);
addrlen = sizeof(struct sockaddr_in);

cout<<"*************************\n";
new_socket = accept(create_socket,(struct sockaddr *)&address,&addrlen);
cout<<"*************************\n";

//char buf[1024];
//Data *d=new Data;
Data dobj;
if (new_socket > 0)
{
cout<<"The Client "<<inet_ntoa(address.sin_addr)<<" is Connected...\n";//inet_ntoa(address.sin_addr));
int ch = recv(new_socket,&dobj,1023,0);
perror("recv");
cout<<"Status = "<< ch<<"\n";
ifstream in("binary.txt", ios::binary);
in.read((char*)&dobj, sizeof(dobj));

if(ch !=-1)
{
//buf[ch]='\0';
//printf("Client: received %s\n",buf);
cout<<"Client: received "<<dobj.data1<<"\n";
cout<<"Client: received "<<dobj.val1<<"\n";
cout<<"Request Completed\n";

}
else
perror("recv");
}
else
{
cout<<"Client not connected ...\n";
}
close(new_socket);
return close(create_socket);

}



i did googled for this and i came to know that c++ does not support / n does not have any standard libraries for object serialization but does support binary serialization which i have done above.also i came across BOOST c++ libraries and many other libraries which many wrote by themselves to support this .but i found most of these libraries/API little bit tough to use and was not sure whether it 'ill deserialize object of some other language other than c++ .

so if any1 could throw a light on on this topic ,it would be more helpful for me ...

thanx

hoxsiew
October 30th, 2009, 10:52 AM
I don't know anything about PHP, but it looks like your data structure is more like:



struct data{
int data1;
char data2[xxx]; //don't know how PHP handles strings. xxx should be the length sent by PHP
};


Best to either use a packet analysis tool like wireshark to look at the byte stream, or use the debugger to look at the raw bytes received on the C++ side (what is the value of ch from your recv() call BTW?).

coder87
October 31st, 2009, 03:06 AM
hi hoxsiew

thanx for ur reply,

>> I don't know anything about PHP

PHP is an loosely-typed language, so a variable does not need to be of a specific type and can freely move between types as demanded by the code it is being used in.

>>(what is the value of ch from your recv() call BTW?).

before im sending serialized php object in php im checking itz return value , send() -if its sucess then it returns the number of bytes sent else FALSE on error.

so in d above php client program im gettin 60 bytes, now serialize() function of php returns a string containing a byte-stream representation of any value that can be stored in PHP.

now in C++ ,when im receiving the object using recv() ,im checking how many bytes itz getting thr and it was xactly showing the same bytes as in php.i.e 60 bytes

when im passing strings or integer value from php ,im able to receive them correctly in c++.so im pretty confident tat c++ is receiving the bytes which im sending,but the problem is i have to deserialize the object properly in c++.
is thr any way to do tat ??

thanx