javacnetwork-programmingdoublehtonl

How to send c++ doubles from one system to another Sender: Winsock Receiver: Java


The sending C-side

double tmp = htonl(anydouble);
send(socket, (char *) &tmp, sizeof(tmp), 0);

On the Java side, im reading the network data into a char[8]

What is the proper way of performing the conversion back to double? Is it the way to go to simply send a string and parse it back to double?


Solution

  • I would advice to use Google Protocol Buffers: https://developers.google.com/protocol-buffers/docs/overview It has libs for C and Java and many other languages, like for example Python, which you may find useful later. It is efficient, robust, will take care of endianness, etc.