Stole Perov wrote:So, just to make it clear that i understood.
dos.writeInt(data.length); == write to the "buffer" the value of the data array.[do not send the buffered data]
dos.write(data); == add to the "buffer" the whole data array.[do not send the buffered data]
dos.flush(); == send to the output socket the following buffered data [data.length, data[]]
Right.
so, for example, if data.length = 2 and data[0] = 1 and data[1] = 3 then the output to the outgoing buffer would be 2,1,3
right?
Almost. An int takes up 4 bytes in size, so what is actually sent is <2 as 4 bytes>,1,3.
and, what does "int length = dos.readInt();" means. Measure the length of the received stream or read the first integer from the stream.
It reads the first 4 bytes and then converts them into an int. The reason I used writeInt and readInt is because you can't assume you need any less than 4 bytes to store the complete array size; your array can in theory have a length of Integer.MAX_VALUE and you need 4 bytes to store that value. DataOutputStream.writeInt and DataInputStream.readInt will convert a single int into 4 bytes and vice versa.