简体   繁体   English

Java创建要发送到服务器的数据包字节数组

[英]Java Creating a packet byte array to send to server

How would one go about making a byte[] that is a packet that needs to be run through an RC4 encryption class and then sent to a server? 如何制作一个byte [],它是一个需要通过RC4加密类运行然后发送到服务器的数据包?

So let's say I need the packet to stsrt with a string, followed with an int, byte, int, string. 因此,假设我需要使用字符串将数据包发送到stsrt,然后是int,byte,int,string。 How would I create that as a byte array? 如何将其创建为字节数组? (Byte[]) (字节[])

Thanks! 谢谢!

OK, so you have a data structure containing strings, integers and bytes, that you want to serialize to a byte array. 好的,所以您有一个包含字符串,整数和字节的数据结构,您想要序列化为字节数组。 There are several options: 有几种选择:

  • create a Serializable class containing all this information as fields, and use an ObjectOutputStream to write it. 创建一个包含所有这些信息作为字段的Serializable类,并使用ObjectOutputStream进行编写。 Beware: the result will only be easily readable by a Java program using the exact same class. 注意:仅使用完全相同的类的Java程序才能轻松读取结果。
  • create a class containing all this information as fields, and use a JSON object mapper (like Jackson) to write it. 创建一个包含所有这些信息作为字段的类,并使用JSON对象映射器(如Jackson)编写它。
  • create a class containing all this information as fields, and use an XML object mapper (like JAXB) to write it. 创建一个包含所有这些信息作为字段的类,并使用XML对象映射器(如JAXB)来编写它。
  • design a binary representation of this structure, that can be transformed back into the individual parts, and use a DataOutputStream to write it. 设计此结构的二进制表示形式,可以将其转换回各个部分,并使用DataOutputStream进行编写。
  • Use protocol buffers 使用协议缓冲区
  • ... ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM