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