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";
try{
kkSocket = new Socket("localhost", 4455);
oos = new ObjectOutputStream(kkSocket.getOutputStream());
}
catch (UnknownHostException e)
{
System.err.println("Don't know about host: 192.168.1.137.");
System.exit(1);
}
catch (IOException e)
{
System.err.println("Couldn't get I/O for the connection to: 192.168.1.137.");
System.exit(1);
}
s=new Sample();
oos.writeObject(str);
System.out.println("name :" + s.name);
System.out.println("age :" + s.value);
kkSocket.close();
}
}
class Sample implements Serializable
{
String name="xyz";
int value=10;
}
my c++ server code :
#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(4455);
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";
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)
{
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);
}
o/p for client / server looked like this:
java client:
*********************
*********************
name :xyz
age :10
c++ server:
The socket was created
workingBinding Socket
*************************
*************************
The Client 127.0.0.1 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.
thanx