c# - Does DataInputStream receives only 2048 bytes of data in Android? -
from pc (server side) c#.net application has sent 22000 bytes of data android device (client side) via wi-fi. datainputstream in android device showing 2048 bytes of it.
datainputstream = new datainputstream(workersocket.getinputstream()); byte[] rvdmsgbyte = new byte[datainputstream.available()]; (int = 0; < rvdmsgbyte.length; i++) rvdmsgbyte[i] = datainputstream.readbyte(); string rvdmsgstr = new string(rvdmsgbyte); i confused following:
- is pc can send 2048 bytes of data?
- or, android device has 2048 bytes of capacity receive data?
or,
datainputstreamshows 2048 bytes after device received bytes?if (data_received <= 2048 bytes) above code works perfect;
you shouldn't using inputstream.available(). tells how data available right now - may not whole message, if it's still coming on network.
if stream end @ end of message, should keep looping, reading block @ time until you're done (preferrably not reading byte @ time!). example:
byte[] readfully(inputstream input) throws ioexception { bytearrayoutputstream output = new bytearrayoutputstream(); byte[] buffer = new byte[8 * 1024]; // 8k buffer int bytesread; while ((bytesread = input.read(buffer)) > 0) { output.write(buffer, 0, bytesread); } return output.tobytearray(); } if need break stream messages, should consider how - sending "message length" prefix before data best option.
once you've read data, should not this:
string rvdmsgstr = new string(rvdmsgbyte); that use platform default encoding convert bytes text. if genuinely text data, should use string constructor overload lets specify encoding explicitly.
if it's not genuinely text data, shouldn't trying treat such. if need represent string, use base64.
also note there's no need use datainputstream here, far can tell.
Comments
Post a Comment