I have some problems aka how to bring value of type double (or even array of double values) through socket from C++ server to Java applet client.
I tried two possibilities. For example:
- Doesn't work, but I think it could be ok while working
In c++ my server does:
double h=123.456; send(j,(const char*)&h,sizeof(h),0);
than i try to bring that double value in Java, but I'm really cofused, how.
BufferedReader in=new BufferedReader(new InputStreamReader(sock.getInputStream())); char buf=new char; i=in.read(buf); //so i get chars in char array
And here is the problem, that I cannot bring that chars back to double in Java.
In c++ client (which I also use in S-function Matlab) is just easy with using memcpy and that's it!
- Works, but is slow, and loose information
Than I decided to do otherwise, so in C++ i use sprintf(...) to bring double to int and int to char array, send char array to Java and than parse char array back to int and int to double, which could still be ok. But....i think it costs much more on CPU performance. :-/
char msg; double h=123.456; int i=sprintf(msg,"%d",(int)(h*1000));//i will divide it back in java send(j,(const char*)&msg,i,0);
And parsing in java:
char buf=new char; String tempArr=new String; String tempStr; double a; in.read(buf); tempStr=new String(buf); tempArr=tempStr.split(";"); a=((double)Integer.parseInt(tempArr))/1000;
Some advices would be very helpful, so thx in advance,