I have got problem with sending int from Java Client to C++ server. I don't wan't to change code of server (i hear something about htonl). Now sending from client 13928 (0011 0110 0110 1000) i recive on my server 6829568(0110 1000 0011 0110 0000 0000). Is there un Java any function similar to htonl ?
3 Answers
Change the code of the server. It's broken.
The problem is that the format the server "understands" will actually vary depending on its CPU architecture. Java is sending the standard network byte order, which is the right thing to do - the server then has to to ntohl() to convert that to its internal format. Changing the client code to include assumptions about the server's endianness is a really, really bad idea.
3 Comments
David Schwartz
You don't know that for sure. It's certainly possible, but it's also possible that the server speaks a well-defined protocol that uses little-endian byte ordering.
Michael Borgwardt
@David Schwartz: Well, I was operating under the assumption that the OP's reference to htonl was relevant.
Malkocoglu
In these situations, I offload the endian processing to the client side. Server operates/communicates in its native endian. The client does the endian processing, only if it differs from the server...