In Java I have the following
while(true) {
input = clientSocket.getInputStream();
byte[] bytes = new byte[4096];
int numRead=input.read(bytes, 0, bytes.length);
System.out.println(numRead);
}
When I send a stream of data locally, in hex 0A 48 08 05 12 20 44 36, I receive this in one go and the out put is:
8 (with the hex being 0A 48 08 05 12 20 44 36)
BUT when I run this across a wireless network I get the following output:
1 (with the hex being 0A)
7 (with the hex being 48 08 05 12 20 44 36)
Why is it doing this? I would expect it to return the value 8 (hex 0A 48 08 05 12 20 44 36)
Am I missing something here?
Thanks in advance
You cannot assume that a socket stream will deliver everything at once, even if it seems like it would fit into a packet. Servers along the route may choose to break up packets at line terminators (or anywhere else) for their own reasons, and you have no control over that. Accumulate the response in a ByteArrayOutputStream or similar buffer and process it once you get it all.