网络订单简短(Java) [英] Network Order short (Java)
问题描述
我需要发送网络订单(我要使用Java编写的游戏服务器的简称).我读到有关网络订单的信息,但找不到有关在数据发送之前发送的空头的任何详细信息.有人可以向我解释它是什么,以及如何使用Java将其发送给客户端吗?
I need to send a Network Order short for a game server I'm writing using Java. I read about network order, but I couldn't find any details about a short that is sent before the data. Could someone explain to me what it is, and how to send one to a client with Java?
推荐答案
Java NIO字节缓冲区支持更改字节顺序.因此,网络字节顺序为Big Endian.
Java NIO bytes buffers have support for changing the byte order. Network byte order is Big Endian therefore.
// 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);
字节顺序是存储大于单个字节的数值的字节的顺序.有两种口味:Big Endian(最高有效字节在前)和Little Endian(最低有效字节在前).
Byte Order is the order in which the bytes for numerical values that are larger than a single byte are stored. There are 2 flavours Big Endian (most significant byte first) and Little Endian (least significant byte first).
这篇关于网络订单简短(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!