Network Reduction (Java)

I need to send a short order to a network server, which I write using Java. I read about network order, but I could not find details about the short one that is sent before the data. Can someone explain to me what it is and how to send it to a client with Java?

+3
source share
3 answers

Java NIO byte buffers support byte ordering. Thus, the network byte order is a large endian.

// Allocate a big endian byte buffer
ByteBuffer bb = ByteBuffer.allocate(4096);
bb.order(ByteOrder.BIG_ENDIAN);
bb.putShort(12345);

// Write the buffer to an NIO channel
bb.flip();
channel.write(bb);

Byte order is the order in which bytes are stored for numeric values ​​that are more than one byte. There are 2 flavors Big Endian (high byte) and Little Endian (low low byte).

+6
source

java int - 2 . , .., . OutputStream o short i,

o.write((i >> 8) & 0xff);
o.write(i & 0xff);

. DataOutputStream, writeShort() ( writeInt, writeLong ..), .

+1

OutputStream DataOutputStream.

DataOutputStream.writeShort. .

0

Source: https://habr.com/ru/post/1727102/


All Articles