开发者

C# Robust net connection handling on flakey networks

开发者 https://www.devze.com 2023-03-02 09:22 出处:网络
I\'ve got quite a few code snippets which correctly download x/html pages and files off the Internet, and they - for the most part - work quite well.

I've got quite a few code snippets which correctly download x/html pages and files off the Internet, and they - for the most part - work quite well.

Unfortunately, my home Internet connection becomes near-unusable between midnight and 6am. I get a lot of packet loss and stalled connections. Similarly, I have a backup 3G connection at work which has intermittent outages depending on network load.

Of course I can put the download code into a try/catch - but ideally I would like the the file to be correctly downloaded, regardless of how long it takes.

I have tried a number of things to resolve this, and have had mixed success.

I've tried getting the length of the stream I'm capturing, and verifying that my local copy is the same length. For some reason, the (correctly downloaded) local copy always seemed to be a few dozen bytes shorter than the reported length of the stream, but the actual number by which it was shorter seemed to vary. Further开发者_高级运维more, not all streams have the length available.

I also tried putting my try/catch'd segment inside a for loop - making it try the download, say, 50 times. A successful download would break, whereas a failed one would try again.

I augmented the above with a gradually increasing delay - I had a Thread.Sleep() which would gradually sleep for longer and longer at each iteration before trying again.

I imagine this task isn't as easy as it sounds, since a lot of my personal software - Steam, Chrome, Windows Update, iTunes, etc... can't seem to deal with my flaky connection at all. I frequently have to attempt to download large files 10-15 times before they come down successfully. Either these devs don't care about handling connection problems or it is difficult to do.

If it is possible, can someone please provide a code snippet which will grab a stream off the Internet but will handle a flaky connection?

I imagine it should be something like:

public static MemoryStream Fetch(string url, int? maxRetries, int? maxDuration)
{
  // Download the response of the url to a MemoryStream.
  // Assume if null maxRetries and maxDuration, keep trying forever.
}


I would suggest looking into an existing solution. My first two thoughts are to either use BITS or shell out to Robocopy. Both are well vetted in downloading files in potentially unstable networks (BITS is what Windows Update uses to download updates to your machine).

0

精彩评论

暂无评论...
验证码 换一张
取 消