Sending Hex through TCP

I wanted to create a tutorial for networking as there was nothing else available. I created this on the github for the website:

OpenFL Networking Tutorial

One thing I know I have wrong is encoding the data, I know I am just sending a string through the socket instead of Hex, which I’m sure requires a lot more bandwidth. Does anyone know how I can specifically send Hex through TCP sockets?

1 Like

You could use Haxe serialization or Haxe JSON for making sure your data is a String before sending it?

I was actually hoping to send Bytes directly as individual 1’s and 0’s, to compress the amount of data being sent. I’m unsure of how to turn Hex into Bytes though. Looking at the Bytes class it seems there is no way to create a Bytes variable from Hex though, or even 0’s and 1’s.

http://api.haxe.org/haxe/io/Bytes.html

I’m looking at sending Hex to the server. Here AB denotes a Move instruction, then 00001 is the ID, 00050 is the X position, 00100 is the Y position:

  1. AB-00001-00050-00100
  2. AB000010005000100->Bits: 10101011000000000000000000010000000000000101000000000000000100000000
  3. Send to Server.
  4. Server converts Bits->Hex
  5. Server updates position based on packet.

openfl.utils.ByteArray is an interface for haxe.io.Bytes you could use as well for writing shorts, floats, ints, strings or bytes as you wish. For example:

byteArray.writeByte (0); // abstract enum value AB
byteArray.writeInt (1); // id
byteArray.writeFloat (50); // x
byteArray.writeFloat (100); // y

Just pay attention to the endianness of the server and your byteArray

For an abstract enum, you could use something like:

@:enum abstract ServerCommand(Int) from Int to Int {
    
    var AB = 0;
    ...

}

That way you can use an ordinary enum in your Haxe code, but when you send it to the server, you can read or write it as an ordinary Int

Awesome tutorial!

Would it be possible to create one for UDP as well?

1 Like