LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-13-2003, 12:12 PM   #1
champ
Member
 
Registered: Jul 2002
Distribution: Slackware 10.0
Posts: 46

Rep: Reputation: 16
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.
 
Old 09-13-2003, 01:33 PM   #2
Lugh
LQ Newbie
 
Registered: May 2003
Location: Toronto
Posts: 18

Rep: Reputation: 0
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.
 
Old 09-13-2003, 01:48 PM   #3
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 50
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.
 
Old 09-13-2003, 04:09 PM   #4
Lugh
LQ Newbie
 
Registered: May 2003
Location: Toronto
Posts: 18

Rep: Reputation: 0
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.
 
Old 09-13-2003, 10:51 PM   #5
champ
Member
 
Registered: Jul 2002
Distribution: Slackware 10.0
Posts: 46

Original Poster
Rep: Reputation: 16
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.
 
Old 09-14-2003, 05:21 PM   #6
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Ubuntu
Posts: 208

Rep: Reputation: 31
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mounted read-write Fat32 partition suddenly becomes read-only?? hohead Linux - General 8 04-05-2007 12:17 PM
Example share in smb.conf doesn't work - read/write vs read only kleptophobiac Linux - Networking 0 09-01-2004 07:14 PM
Read-Write Access for Network Shares HKDude Linux - Newbie 2 08-06-2004 06:17 PM
Open office read only, K-write read/write mode lwtvh Linux - Newbie 1 07-19-2003 11:33 AM
Change from Read only to Read Write File System? justiceisblind Linux - Newbie 3 03-03-2002 07:23 PM


All times are GMT -5. The time now is 12:23 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration