开发者

c++ server htons to java ntohs client conversion

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 a开发者_如何学运维ble 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.


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.


   private void somemethod(){

    byte[] fileDataFull = new byte[516];
    byte[] receivedCounter = new byte[2];

    readLength = in.read(fileDataFull);

    receivedCounter[0] = fileDataFull[2];
    receivedCounter[1] = fileDataFull[3];

   if (expectedPktCount == convertntohs(receivedCounter)){

   }

   byte[] b = converthtons((short) expectedPktCount);

}

    private short convertntohs(byte[] value) {
        ByteBuffer buf = ByteBuffer.wrap(value);
        return buf.getShort();
    }

    private byte[] converthtons(short sValue) {
        byte[] baValue = new byte[2];
        ByteBuffer buf = ByteBuffer.wrap(baValue);
        return buf.putShort(sValue).array();
    }


Java's numbers internally are always in network byte order, even on systems where the native integers/doubles are not. This means that you can convert any number coming in to Java with any of the base input streams that do this kind of conversion - that implement java.io.DataInput. ByteBuffer also works if you are using a java.nio.Channel but there you can change the endianness (ByteBuffer.order()), though the default is correct for network byte ordering and Java internal storage.

By the way, I think you mean to use htons but in the example you show ntohs. Sending from C++ you want to convert host order to network order. Receiving from Java (or any client) your server would convert back using ntohs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜