I'm having to modify some client side code because the comms protocol wasn't defined properly.
I'd assummed that a tcp message from the server would terminate in a new line so I used reader.readLine() to read my Data.
Now I've been told that this is not the case and that instead the first 4 chars of the message are the message length and then I have to read the rest of the message.
What is the most efficient sensible way to do this?
my general Idea was as follows:
- Create a 4 char array
- Read in first 4 characters
- Determine what the message length is
- Create a new array of message length
- read into the new array.
Here is an example of the code (reader is a BufferedReader created elsewhere):
char[] chars = new char[4];
int charCount = reader.read(chars);
String messageLengthString = new String(chars);
int messageLength = Integer.parseInt(messageLengthString);
chars = new char[messageLength];
charCount = reader.read(chars);
if (charCount != messageLength)
{
// Something went wrong...
}
I know how to do this, but Do I have to worry about the character buffers not being filled? if so how should I deal with this happening?