当前位置: 动力学知识库 > 问答 > 编程问答 >

error in the communication between C client and Java server

问题描述:

This question already has an answer here:

  • send an integer from a C client to a Java server

    1 answer

网友答案:

You are using the htons function to translate the int value to network byte order.

The function htons function is defined to rearrange bytes on little-endian hosts to network byte order (big-endian), and Java is always big-endian.

But this function translates a 16-bit value, not a 32-bit value. Here's the bits:

4      = 00000100 00000000 00000000 00000000 (2^2) (little-endian)
262144 = 00000000 00000100 00000000 00000000 (2^18) (big-endian)

Try using the htonl function, which translates 32-bit int values to network byte order. Then this should result.

4      = 00000100 00000000 00000000 00000000 (2^2) (little-endian)
4      = 00000000 00000000 00000000 00000100 (2^2) (big-endian)
网友答案:

262144 is Hex 0x00040000, so something changes your byte order.

分享给朋友:
您可能感兴趣的文章:
随机阅读: