Socket passing double type value from c++ to java

Reply

Join Date: Jul 2009
Posts: 3
Reputation: domenzup is an unknown quantity at this point 
Solved Threads: 0
domenzup domenzup is offline Offline
Newbie Poster

Socket passing double type value from c++ to java

 
0
  #1
Oct 10th, 2009
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.
    1. BufferedReader in=new BufferedReader(new InputStreamReader(sock.getInputStream()));
    2. char[] buf=new char[8];
    3. 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:
    1. char[] buf=new char[10];
    2. String tempArr[]=new String[2];
    3. String tempStr;
    4. double a;
    5. in.read(buf);
    6. tempStr=new String(buf);
    7. tempArr=tempStr.split(";");
    8. a=((double)Integer.parseInt(tempArr[0]))/1000;
Some advices would be very helpful, so thx in advance,
Domen
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark
 
0
  #2
Oct 10th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,610
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 465
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human
 
0
  #3
Oct 10th, 2009
> 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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 3
Reputation: domenzup is an unknown quantity at this point 
Solved Threads: 0
domenzup domenzup is offline Offline
Newbie Poster
 
0
  #4
Oct 12th, 2009
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
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC