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 is Connected...
recv: Success
Status = 60
Client: received O:4:"data":2:{s:1:"b";i:60;s:6:"string";s:11:"Hello World";}
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<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/stat.h>
#include<unistd.h>
#include<stdlib.h>
#include<iostream>
#include<fcntl.h>

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

	new_socket = accept(create_socket,(struct sockaddr *)&address,&addrlen);
	
	char buf[1024];
	Data *d=new Data;
  	if (new_socket > 0)
	{
     		cout<<"The Client is Connected...\n";
		int ch = recv(new_socket,buf,1023,0);
		perror("recv");
		cout<<"Status = "<<ch<<"\n";	
		
		if(ch !=-1)
		{
			//buf[ch]='\0';
			cout<<"Client: received"<<buf<<"\n";//even if i use buf(which is not obj) i get php serialized objects value 
			//cout<<"Client: received"<<d->val1<<"\n";//if i use d->val1,instead of printing value of val1 alone it gives entire serialized object value
                       //cout<<"Client: recieved"<<d<<"\n" //if i print d ,then also it comes with same output as for the above two case
			cout<<"Request Completed\n";
		}
		else
		perror("recv");
	}
	else
	{
		cout<<"Client not connected ...\n";
	}  	
		close(new_socket);
  		return close(create_socket);
}

since serialize() returns a string containing a byte-stream representation of any value that can be stored in PHP,im able to print that string containg values of serialized php object in c++ using a char array instead of getting it using a object in c++.

i know because of not deserializing properly in c++ im not able to get the values properly without junk characters / value along with correct values.so it wud b great if anyone could help me out ...

advance thanx

hi guys i tried to read object from a file in cpp using ifstream object like given below:

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

}

but this is not workin and its o/p was like dis:

The socket was created

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.