c# - StreamReader.Read text out of order -
i'm trying send base64 string of screenshot server via networkstream , appears i'm receiving full string, problem it's scrambled...
i assume has being fragmented , put together? appropriate way go this...
client code
byte[] imagebytes = generics.imaging.imagetobyte(generics.imaging.get_screenshot_in_bitmap()); string streamdata = "remotedata|***|" + convert.tobase64string(imagebytes); sw.writeline(streamdata); sw.flush();
server code
char[] bytedata = new char[350208]; sr.read(bytedata, 0, 350208); string data = new string(bytedata); file.writealltext("c:\\recievedtext", data);
also size of sent message , char array same.\
edit: after messing around more realized text isnt scrambled proper text trailing previous stream.. how can ensure stream clear or gets entire text
it's you're not reading of previous response. have read in loop until no data, this:
char[] bytedata = new char[350208]; int totalchars = 0; int charsread; while ((charsread = sr.read(bytedata, totalchars, bytedata.length - totalchars) != 0) { totalchars += charsread; } string data = new string(bytedata, 0, totalchars); file.writealltext("c:\\recievedtext", data);
the key here streamreader.read reads up to maximum number of characters told to. if there aren't many characters available, read what's available , return those. return value tells how many read. have keep reading until number of characters want, or until read
returns 0
.
Comments
Post a Comment