c# Stream data not reading correctly -
c# Stream data not reading correctly -
solved issue was assuming got info @ 1 time when told read, escaping out of read screenshot method , dumping info pack packet parser, proceeded spam console junk >.<
thanks jon.
i have new problem, time cant figure out whats wrong info streams, i'm attempting stream image files captured central networked server, have client > server set up, talking fine, passing info , forth day long, when effort phone call screenshot function , send it, thats issues.
the current code im using is:
notes datahandler wrapper sending , receiving data, addvar overloaded method takes variable , sends through stream (new datahandler(stream))
datahandler linked tcp stream
readint , readlong etc reverse, helper functions facilitate making code easier maintain later.
at point server sends out ping every second, client, client set respond ping if isnt busy responding packet (the incoming packets run on single thread)
server follows same rule, per client
writing info stream @ si.takescreenshotandstream, method takes screenshot of client computer , streams info stream passed, in case memorystream.
end note
(client side)
try { datahandler.addvariable((byte)50); memorystream = new memorystream(); si.takescreenshotandstream(memorystream, quality); datahandler.addvariable(memorystream.length); memorystream.position = 0; memorystream.copyto(clientstream); clientstream.flush(); } grab (exception e) { console.writeline(e.message); disconnect(); } and server side
if (!directory.exists(loadedsettings.directory + name)) directory.createdirectory(loadedsettings.directory + name); filestream = file.create(fullpath); long length = datahandler.readlong(); byte[] info = new byte[length]; clientstream.read(data, 0, (int)length); filestream.write(data, 0, (int)length); filestream.flush(); filestream.close(); the "take screenshot , save" function works, can stream filestream , save straight file, problem seems have no clue how work memorystream class if i'm far off mark on how this, nice tutorial memory streams helpful.
at 1 point attempted convery memorystream byte array didnt work either.
also note, doesnt mess up, both of these encapsulated in try/catch statements , invalid packets (anything except 1, 2 , 50 atm), both exceptions , packet numbers invalid logged console, when run code spews much console beeps until shut off (there no console.beep code anywhere in program)
any assistance appreciated :)
it's way you're reading info here:
clientstream.read(data, 0, (int)length); instead, should utilize value returned read:
// todo: if want handle more 4gb per file, you'll need // alter this... int length = (int) datahandler.readlong(); byte[] buffer = new byte[16 * 1024]; // read 16k @ time while (length > 0) { int bytesread = clientstream.read(buffer, 0, math.min(length, buffer.length)); if (bytesread <= 0) { // throw appropriate exception: info truncated } filestream.write(buffer, 0, bytesread); length -= bytesread; } c# stream
Comments
Post a Comment