c++ server htons to java ntohs client conversion
c++, data-conversion, htonl, java
Solution
I take it you meant that you use `ntohs` to decode the values read from the network, and `htons` to encode the values sent over the network.
Look into `ByteBuffer#getShort()` in concert with `ByteBuffer#order(ByteOrder)`. Network byte order is big endian, so use the value `ByteOrder#BIG_ENDIAN` to configure your `ByteBuffer` properly. Note that `BIG_ENDIAN` is the default order, but in this case it would be good form to state your preference explicitly.
You didn't mention what you're using for network communications in Java. If it's a `java.net.Socket`, you can call `Socket#getChannel()` to get a `java.nio.channels.SocketChannel`, a subtype of `java.nio.channels.ByteChannel`, with which you can use `ByteBuffer` to read and write data.
Problem
I am creating a small TFTP client server app where server is developed using c++ and client using java. Here i am sending "block count" value using `htons` conversion. But i am not able to convert it back to its original value at client. For example if am sending block count `ntohs(01)` (2 bytes) from server to client. Client is reading in bytes. Value which I am receiving is byte 0 and byte 1. Please if any one can provide a solution.