This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes Network Byte Order - C++ server and Java Client Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Reply Bookmark "Network Byte Order - C++ server and Java Client" Watch "Network Byte Order - C++ server and Java Client" New topic
Author

Network Byte Order - C++ server and Java Client

Pearlo Muthukumaran
Ranch Hand

Joined: Sep 17, 2002
Posts: 79
Hi all,
Is it necessary that the byte order should be taken care at the client end to suit the server requirements through bitwise shifting or other techniques?
If that is the case, how application protocol clients work without even knowing about the server implementation?
I am using DataOutputstream at the Java Client end to send some data to Server implemented in C++, everything works fine but the order EXACTLY REVERSED.
Could anyone please explain how to address this and reason for this behavior?
Rgds
Muthu
Pearlo Muthukumaran
Ranch Hand

Joined: Sep 17, 2002
Posts: 79
Hi all,
Could anyone please help me on the above issue?
Rgds
Muthu
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
DataInputStream writes the high byte first. If that's not what you want, it looks like you've got two options. First, write your own OutputStream that writes the low word first. Second, if you're using Java 1.4 make use of the handy ByteOrder class to reverse your ByteBuffers as necessary.
The other option is of course the modify the C++.
"If that is the case, how application protocol clients work without even knowing about the server implementation?". In this case how you encode values isn't an implementation detail, it's a protocol spec detail. If you declare in the protocol specification that integers should be encoded low-word first, than so be it.
Eddy Chang
Greenhorn

Joined: Apr 05, 2001
Posts: 27
Anything data that is sent over a socket regardless of the application should be in network byte order, ie big endian. Byte order is hardware dependent, ie intel x86 cpus are little endian and motorola cpus for example are big endian.
Java will send data for you in network byte order (big endian) so if your C++ application is on an intel cpu, then you will need to convert from big endian (network byte order) to little endian to be able to interpret the data properly.
The c++ application should call ntohl (network to host long) and ntohs (network to host short) to convert big endian integer values to the local host little endian values. You do not need to do any conversions for ascii strings. If you are sending floating point numbers, you will need to send those as a string and then convert the string back into a floating point value. Similarly, you could send the interger values as a string and then convert the string back to an integer.
This link may explain things clearer.
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&th=18cc9e8a8f1cf78c&seekm=VA.000000be.0086bef3%40trmphrst&frame=off

 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Network Byte Order - C++ server and Java Client
 
Similar Threads
"MISSING BYTE ORDER MARK" exception during receiving messege through socket
C++ client connects with java server components?
Server Socket
communciate with c server
Connection refused: connect