When creating a socket using the InputStream input stream, the read () method, if the call is available (), returns the length of the byte stream, may return 0, or other numeric values, often error in network operation, Because when you call the available () method, the data sent to the send may not yet arrive, and the count you get is 0.
Network transmission is implemented as follows:
int readbytes = 0; byte[] buffer = new byte[1024];// 1024 can be changed to any required value int len = buffer.length;while (Readbytes < len) { int read = inputstream.read (Buffer, readbytes, len - readbytes); //determines whether the data stream is read at the end of the  , preventing a dead loop. if (read == -1 | | read < (len - readbytes) { readbytes += read; break; } if (read == (len - readbytes)) { byte[] tmpbuffer = new byte[len * 2]; system.arraycopy (buffer, 0, tmpbuffer, 0, buffer.length); buffer = tmpbuffer; len = Buffer.length; } readbytes += read;} byte[] endodeddata = new byte[readbytes]; System.arraycopy (buffer, 0, endodeddata, 0, readbytes); System.out.println (endodeddata.length);
java-no.10 socket processing InputStream input stream