Tuesday 15 September 2015

download - Segmented C# file downloader -


Trying to write a program in C # that downloads files using multiple segments , Like most download managers, they run into problems that are downloaded files that are broken eg. I download a video and play it for 2 seconds, then WMP says that it is impossible.

I hexhed the downloaded file and it appears that there are squares of scattered zerbites throughout the file, why have anyone got any idea? VS does not report any errors.

Each segment is called getPart () in a separate thread.

  Launch public launch; Public long end; Public int thread; Public form 1 handle; Public myFile handler; Public Zero getPart () {log ("get share" + start.ToString () + "," + end.ToString ()); HttpWebRequest part = (HttpWebRequest) WebRequest.Create (Handler. Url); Part.AddRange ((int) start, end (interval)); HttpWebResponse PR = (HttpWebResponse) part.GetResponse (); Stream rstream = pr.getResponseStream (); Logs ("start up" + start.ToString ()); Int'l count = 0; Byte [] buffer = new byte [256]; Int x = rstream.Read (buffer, 0, 256); While (x> gt;) {handler.litfile (buffer, (int) (total + start), x); Kulbite + = X; X = rstream.Read (buffer, 0, 256); } Log (start.ToString (+) + "-byte started part ran ..."); Rstream.Close (); Pr.Close (); Handler.partDone (thread); Return; } Public Zero WriteFile (Byte [] buffer, integer start, integer size) {mFileStream.Seek (SeekOrigin.Begin); MFileStream.Write (buffer, 0, size); Return; }  

OK I thought it, just thought I would leave an answer to this For anyone with type of issues, a lock is required around the file writing stream.

  Public Waste Written File (Byte [] Buffer, Integer Start, Integer Size) {mFileStream.Seek (SeekOrigin.Begin;); MFileStream.Write (buffer, 0, size); Return; }    

becomes

  write public zero file (byte [] buffer, integer start, integer size) {lock (mFileStream) {mFileStream .Seek (Start, SeekOrigin.Begin); MFileStream.Write (buffer, 0, size); Return; }}  

No comments:

Post a Comment