网络订单简称 (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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Java NIO 字节缓冲区支持更改字节顺序。因此,网络字节顺序是 Big Endian。
字节顺序是大于单个字节的数值的字节存储顺序。有两种风格:Big Endian(最高有效字节在前)和 Little Endian(最低有效字节在前)。
Java NIO bytes buffers have support for changing the byte order. Network byte order is Big Endian therefore.
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中,short int是一个2字节的量。网络字节顺序首先发送高位字节,然后发送下一个最高位字节,依此类推,最后发送低位字节。如果您有一个OutputStream o 和一个short i,则按
网络字节顺序发送short。我建议使用
DataOutputStream
,它有一个方法writeShort()
(以及writeInt
、writeLong
等),该方法自动按网络字节顺序写入。In java, a short int is a 2 byte quantity. Network byte order send the high-order byte first, followed by the next highest-order byte and so on, with the low order byte sent last. If you have an
OutputStream o
, and ashort i
, thensend the short in network byte order. I recommend using a
DataOutputStream
which has a methodwriteShort()
(andwriteInt
,writeLong
, etc.) which automatically write in network byte order.您可以使用 DataOutputStream 包装您的 OutputStream。
然后,您可以使用 DataOutputStream。写短。根据约定,该方法按网络顺序写入数据。
You can wrap your OutputStream with DataOutputStream.
You can then use DataOutputStream.writeShort. By contract the method writes data in network order.