Hi,
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:

  1. 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[8];
    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!

  2. 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[10];
    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[10];
    String tempArr[]=new String[2];
    String tempStr;
    double a;
    in.read(buf);
    tempStr=new String(buf);
    tempArr=tempStr.split(";");
    a=((double)Integer.parseInt(tempArr[0]))/1000;

Some advices would be very helpful, so thx in advance,
Domen

I wouldn't waste a single millisecond worrying about char/Stringdouble conversion CPU usage. Whatever you do will be a fraction of 1% of the CPU used by the OS doing the socket operations.
You don't need to do the conversion to int and back; just send the double in text format and parseDouble(...) at the Java end.

> You don't need to do the conversion to int and back; just send the
> double in text format and parseDouble(...) at the Java end.

I don't think that would handle values like +INF, -INF and NaN or even deal with the obvious truncation of values [think 1/3].

@Domenzup
Even though it might be a bit more work for you, your best bet would to rely on a data transfer format/specification which does the job of converting the double to a string and back *without* any loss and in a portable manner. Using a in-house hacked version to transfer data is something I won't recommend.

If you need an enterprisy solution, go with XML. JSON is another option which is fast along with being portable. Of course there are many other data transfer formats out there like ProtoBuffer but these are most widely used ones.

For libraries which deal with converting C++ to and from JSON to Java, see here.

Thanx very much for so quick reply. Now I decided to make one msg, in which I'll memcpy structure of double values for c++ client and in the same message after I attached values for java client with sprintf function, all separated with ";" for parsing.

Is not optimal solution, but I lost to much energy with searching how to read double value send from c++ to java that I gave up.

Domen

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