in a server client communication which uses Socket and ServerSocket classes of java, think that client is sending some information byte by byte,
and server is reading in the same way (byte by byte), then replies.
what i cannot figure out is: when does the server start reading the bytes which are sent by client.
does it wait until all bytes arrived?
or does it immediately read when 1 byte is written to the related port.
or does this behaviour is determined by the streams used in communication
(DataInputstream, DataOutputStream etc...)?
or is it about flushing? (some streams may have auto flush or something like that)
I don't know, if it's so with all Streams, but I've made the experience, that when I send Byte per Byte with something like
-
byte[] bytes = new byte[1024];
-
// Fill the byte array...
-
outputStream.write(bytes);
-
and receive with something like
-
byte[] bytes = new byte[1024];
-
inputStream.read(bytes);
-
I have correct data for quite a while, but at some point it starts to only have 0 0 0... What I do is, I wait, until the appropriate amount of Bytes are available with
-
while(inputStream.available < 1024)
-
{
-
try{Thread.sleep(1);}
-
catch(Exception e){e.printStackTrace();}
-
}
-
and then read the array in. Of course, that can be done with single bytes too.
Greetings,
Nepomuk