I am using sockets and implementing a Java server and a c++ client. The client is sending server a class. Java server receives it as array of bytes but after that it is not converting it back into the class members right. I have looked a lot but I guess I don't really know what to look for. Here are the parts of the code, complete code is quite big
Client C++
IntVal temp;
Set(temp,values);
int tempp;
tempp=send(s_id,&temp,sizeof(temp),0);
if(tempp==-1)
{
printf("Sending Error \n");
}
"IntVal Class" has 7 to 8 float values and no member function.
Server Java
public static void main(String[] args) throws IOException {
// TODO code application logic here
Values values=new Values();
gui display=new gui();
display.setVisible(true);
ServerSocket Sock=new ServerSocket(9090);
try{
while(true){
System.out.println("Waiting");
Socket socket=Sock.accept();
System.out.println("Connected");
InputStream ins=socket.getInputStream();
InputStreamReader insr= new InputStreamReader(ins);
BufferedReader br=new BufferedReader(insr);
byte[]Array=br.readLine().getBytes("UTF-8");
// values.SetValues(Array);
//display.SetValues(values);
values.tWidth=Array[0];
values.waterLevel=Array[4];
values.camHeight=Array[8];
values.camViewAngleY=Array[12];
values.camViewAngleX=Array[16];
values.distFromCamBank=Array[20];
values.distTwoPoints=Array[24];
values.AvgVelocity=Array[28];
values.crossSecArea=Array[32];
values.Flow=Array[36];
values.camTiltAngle=Array[40];
values.aboveWater=Array[44];
System.out.println(values.tWidth);
System.out.println(values.waterLevel);
socket.close();
}
}
finally{
Sock.close();
}
}
}
Here Class Values is equivalent to IntVal class in c++ client. As you can see I am only checking with first two values, They display Garbage values. Please point me in right direction that what is wrong here.
You are encoding and decoding the bytes yourself so it's you who is to blame for this, not Java. Use an
InputStream,
not aReader,
and process the bytes directly.