0

So lets say I have a byte array byte[] bytes which is initialised with some data(Client side).

The contents of the byte array can be of type A or B.

Then I want to sent the array over to my server. And I want the server to be able to identify which of the two types of data the array contains (A or B)

I thought about adding one more byte (as it is going to be only a couple of types. maybe 3) as a prefix to the beginning of the array so that the server can identify the type, then remove the first byte and read the contents.

Is there a reason why I shouldn't do that? Is there another approach better than that?

P.S. I don't want to send another message prior to my array to inform about the type of the next message

Rakim
  • 1,087
  • 9
  • 21
  • 40

1 Answers1

0

Is there a reason why I shouldn't do that?

No. Certainly there is no reason ... for the problem as you have described it.

Is there another approach better than that?

There might be, but your solution is good enough ... for the problem as you have described it.


The only improvement I would suggest is to design the server-side code so that it doesn't actually need to remove the "prefix" from the byte array. Get it the start reading the message content at byte position one rather than byte position zero.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I was thinking of the minimal load to the array to make it identifiable by the server. Using only a byte of the array sounds like the optimal solution... Digging deeper into the bits will only make the code more complicated. Yes, that is pretty much what I had in mind when I said "remove".. I was more of an "ignore"! – Rakim Oct 03 '15 at 12:38
  • Have you done the math to work out how much you could possibly save by encoding the prefix in less than one byte? Probably around 100,000th of a second, assuming a 1Mbit/second data rate. On a LAN you should get 10 to 100 times faster than that. Question: is this really worth worrying about? Really? – Stephen C Oct 03 '15 at 12:49
  • talking with numbers I can see why I would like to do it. But with the current speeds I don't think it is worth it. – Rakim Oct 03 '15 at 13:21