LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   write/read double over a network (https://www.linuxquestions.org/questions/programming-9/write-read-double-over-a-network-92394/)

champ 09-13-2003 12:12 PM

write/read double over a network
 
Hi, Im writing a server/client system where I use c++ one the server side and java on the client side.

Im currently writing a communication protocol where I have to deal with byte ordering and other stuff. Writing/reading integer values is not a problem, since I can just shift the bits and everything works. But I also want to read/write a double.

On java they have functions called writeDouble and readDouble, specified in the DataInput and DataOutput interfaces. But on the server side I have to do it manually.
And If I want to use those functions on the client side, I have to use the same method on the server side. I know I have to do some bit manipulation, but I have no idea how.

There are probably some of you guys that have done this before, so I would be happy if you could help me.

Thanks.

Lugh 09-13-2003 01:33 PM

try
man htons
man ntops

that's better than shifting bits. there's also htonl for 32 bits, i
can't recall the bit requirements for int and double.

these are posix standard, I doubt they're supported by windows.

kev82 09-13-2003 01:48 PM

i was gonna suggest that but the only functions are htons(),ntohs() - 16bit and htonl(),ntohl() - 32bit and a double is 64bit so i dont think they work, they are supported in windows though.

Lugh 09-13-2003 04:09 PM

htonl is defined in <netinet/in.h>
it's a either a null macro, or the function bswap_32(x)
depending on the type of endian you system is.

Lookup the definition for bswap_32 and then write
your own bswap_64. Hell, maybe one exists and you
just need to define htond.

I'd check it out myself, but I've got other fish to fry.

champ 09-13-2003 10:51 PM

Thanks for you suggestions,
but those functions will only work on integer values such as short, int and long.

I think I have to convert the double to a long value first, and then maybe I can use htonl. Another problem is of course if Im gonna convert it to a long, the size of long is 4 byte on my little-endian linux box(server) and 8 byte on the client side using java.
So I might have deal with that aswell.

eric.r.turner 09-14-2003 05:21 PM

Quote:

Originally posted by kev82
i was gonna suggest that but the only functions are htons(),ntohs() - 16bit and htonl(),ntohl() - 32bit and a double is 64bit so i dont think they work, they are supported in windows though.
You're exchanging data with a Java program right? In Java a byte is 8 bits, a short is 16 bits, an int is 32 bits, a long is 64 bits, a float is 32 bits, and a double is 64 bits.

Passing around byte, short, int, and long won't be a problem (as long as you get the bit order correct and the number of bits in each type matches on both the client and server) because everyone uses two's complement representation of integer values. You'll have to do some translation if the size of each type is different on the client and server.

Float and double values are a different story. Different architectures represent floating point numbers in different ways. There are a bunch of different standards, and the Sun Java Specification does not require a JVM to use a specific format. So, when dealing with floating point numbers you have three things to worry about:

1. Variable size
2. Bit order
3. Floating point representation (i.e. which bits represent the mantissa, sign, and exponent).

The Java Specification may have something in it about how JVMs must transmit floating point values over a network connection, so you may want to look into that. If it does, then you know that you'll need to translate between whatever format your C++ client uses to the format that the JVM expects. Of course, that means your client isn't cross-platform.


All times are GMT -5. The time now is 02:53 AM.