I am using sockets to connect my Android application (client) and a Java backend Server. From the client I would like to send two variables of data each time I communicate with the server.
1) Some kind of message (Defined by the user through the interface)
2) The language of the message (Defined by the user through the interface)
How could I send these so that the server interprets each as a separate entity?
Having read the data on the server side and made an appropriate conclusion, I would like to return a single message to the client. (I think I will be okay with this)
So my two questions are how can I establish that the two Strings being sent (client to server) are unique on the client side and how can I separate these two Strings on the server side. (I was thinking an array of Strings, but could not establish if this was possible or appropriate.)
I was going to post some code but I'm not sure how that would help.
the easiest way to do this is to wrap your sockets in ObjectInput/OutputStreams and send serialized java objects. you can create classes which contain the relevant data, and then you don't need to worry about the nitty gritty details of handling binary protocols. just make sure that you flush your object streams after you write each object "message".
I assume you are using TCP sockets for the client-server interaction? One way to send different types of data to the server and have it be able to differentiate between the two is to dedicate the first byte (or more if you have more than 256 types of messages) as some kind of identifier. If the first byte is one, then it is message A, if its 2, then its message B. One easy way to send this over the socket is to use
DataOutputStream/DataInputStream
:Client:
Server:
Obviously, you can send all kinds of data, not just bytes and strings (UTF).
Note that
writeUTF
writes a modified UTF-8 format, preceded by a length indicator of an unsigned two byte encoded integer giving you2^16 - 1 = 65535
bytes to send. This makes it possible forreadUTF
to find the end of the encoded string. If you decide on your own record structure then you should make sure that the end and type of the record is either known or detectable.