hi guys im tryin to do a java client / c++ server socket program .in the java client program i serialize the object and pass them over socket to c++ server.now when im gettin them in c++,im not able to deserialize it properly as i tried for binary serialization since c++ does not support object serialization.but it didnt work out properly,as when im printin the member variables of c++ class im gettin junk values.i did came across many libraries like Boost which supports serialization but was not sure whether it will deserialize a java object.

so what if my approach for dis problem is right or shud i go for JNI or any other concepts.if its in JNI then i fear my c++ server 'ill be loaded as shared library file along with java file at runtime which i think 'ill not be a right way to do client-server socket program.

my java client code:

import java.io.*;
import java.net.*;

public class KnockKnockClient
	public static void main(String[] args) throws IOException
		Socket kkSocket = null;
		ObjectOutputStream oos=null;
		Sample s;
		String str="hello dude";
            		kkSocket = new Socket("localhost", 4455);
			oos = new ObjectOutputStream(kkSocket.getOutputStream());
		catch (UnknownHostException e)
            		System.err.println("Don't know about host:");
		catch (IOException e)
            		System.err.println("Couldn't get I/O for the connection to:");
		s=new Sample();
		System.out.println("name :" + s.name);
                System.out.println("age  :" + s.value);

class Sample implements Serializable
	String name="xyz";
	int value=10;

my c++ server code :

#include <iostream> 
#include <fstream>
#include <string>

using namespace std;
class Data
		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(4455);

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

  	addrlen = sizeof(struct sockaddr_in);

  	new_socket = accept(create_socket,(struct sockaddr *)&address,&addrlen);

	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);
		cout<<"Status = "<< ch<<"\n";	
		ifstream in("binary.txt", ios::binary);
		in.read((char*)&dobj, sizeof(dobj));

		if(ch !=-1)
			cout<<"Client: received "<<dobj.data1<<"\n";
			cout<<"Client: received "<<dobj.val1<<"\n";
			cout<<"Request Completed\n";
		cout<<"Client not connected ...\n";
  		return close(create_socket);


o/p for client / server looked like this:

java client:


name :xyz
age :10

c++ server:
The socket was created

workingBinding Socket
The Client is Connected...
recv: Success
Status = 4
Client: received �
Client: received _+
Request Completed

it would be great if any1 cud throw a light on this topic on how to deserialize a java object in c++ if its possible if its not possible , then what all other concepts i can use to solve dis problem.


This article has been dead for over six months. Start a new discussion instead.