Right now I have established a Socket connection with my desktop server. In general it is working fine and I am trying to find any remaining bugs. One that I can’t solve is when I spam my “Send” button. Sometimes it will combine messages meant to be sent separately into one message. I’m fairly sure that this is an Android issue and not an issue with my server. Here is an example of what happens (console output from server):
XX.XX.XX.XX: {"control":[],"commands":[]}
XX.XX.XX.XX: {"control":[],"commands":[]}
XX.XX.XX.XX: {"control":[],"commands":[]}
XX.XX.XX.XX: {"control":[],"commands":[]}{"control":[],"commands":[]}
This is causing my JSON parser to not work correctly. I have the ability to connect to my server either through Bluetooth or Wifi, and I’ve noticed that the problem of the messages being combined only happens while using Wifi and not a BluetoothSocket. Not to say that I don’t think the BluetoothSocket could lag enough to combine like this, but there is a lot less traffic running there. Is there any way to make sure that my packets won’t be combined when being sent? I’ve looked around the Socket options and didn’t know what to look for. Here’s an example of how I send, pretty basic:
mmDataOut.write(buffer);
Where mmDataOut is just a DataOutputStream and buffer is a byte[].
Also, I added the asyncsocket tag because my server is using them, not sure if that would affect anything.
It sounds like you are using TCP. This is expected behavior of TCP, and you need to upgrade your parser to support it.
You cannot safely assume that a chunk of data from the read end will include only one command, and additionally you cannot assume that you will get the entirety of a command in a single read.