0

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 ?

1
  • How do you convert 13928 into bytes on client and how do you convert bytes into 6829568 on server? Commented Apr 19, 2012 at 7:23

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
@David Schwartz: Well, I was operating under the assumption that the OP's reference to htonl was relevant.
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...
1

Just do the math. Use bit shifts, logical AND, and logical OR. For example, to extract the four octets, you can use:

x & 0xff
(x >> 8) & 0xff
(x >> 16 ) & 0xff
(x >> 24 ) % 0xff

Instead of >> 8 you can use / 256. Instead of & 0xff, you can use % 256. Just put the operations together.

Comments

0

look up XDR , this defines the standard that should be used while communication between client and server . if possible copy the int for the cleint in a string and send that string to the server and convert the string to integer over there. this is the most efficient way to send an int.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.