InputDataStream.available() is always 0 Java Socket Client -


why part of client code 0 ?

inputstream inputstream = clientsocket.getinputstream(); int readcount = inputstream.available();  // >> 0 byte[] recvbytes = new byte[readcount]; bytearrayoutputstream baos = new bytearrayoutputstream(); int n = inputstream.read(recvbytes); 

...

presumably it's because no data has been received yet. available() tries return amount of data available right now without blocking, if call available() straight after making connection, i'd expect receive 0 of time. if wait while, may find available() returns different value.

however, don't typically use available() anyway. create buffer of appropriate size situation, , read that:

byte[] data = new byte[16 * 1024]; int bytesread = stream.read(data); 

that block until some data available, may return read 16k of data. if want keep reading until reach end of stream, need loop round.

basically depends on you're trying do, available() useful in experience.


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -