Hi guys,
I was looking for the best solution to send/recieve bytes and never found a great solution. The best one I found (and which works unless the socket calls come in a different order than sent) is the following:
Send:
DataOutputStream socketWriter;
try {
socketWriter.writeInt(message.length);
socketWriter.write(message);
} catch (IOException e) {
e.printStackTrace();
}
Receive:
DataInputStream socketReader;
byte[] b=null;
String message=null;
int size;
try {
size=socketReader.readInt();
b=new byte[size];
socketReader.readFully(b);
} catch (IOException e) {
e.printStackTrace();
}
The problem is that readFully needs to know how many bytes it should read. One way to solve this is to always let the server wait for a confirmation that the client got the whole message (both the int and the bytearray) before sending the next message but there should be a better way to do this, or?
Thanks,
Mike