Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Network Byte Order - C++ server and Java Client  RSS feed

 
Pearlo Muthukumaran
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Could anyone please help me on the above issue?
Rgds
Muthu
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!