简体   繁体   中英

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?

So let's say I need the packet to stsrt with a string, followed with an 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. Beware: the result will only be easily readable by a Java program using the exact same class.
  • create a class containing all this information as fields, and use a JSON object mapper (like Jackson) to write it.
  • create a class containing all this information as fields, and use an XML object mapper (like JAXB) to write it.
  • design a binary representation of this structure, that can be transformed back into the individual parts, and use a DataOutputStream to write it.
  • Use protocol buffers
  • ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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