日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[搜片神器]BT种子下载超时很多的问题分析

發(fā)布時間:2023/12/20 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [搜片神器]BT种子下载超时很多的问题分析 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?

繼續(xù)接著第一篇寫:使用C#實現(xiàn)DHT磁力搜索的BT種子后端管理程序+數(shù)據(jù)庫設(shè)計(開源)[搜片神器]

?

謝謝園子朋友的支持,已經(jīng)找到個VPS進(jìn)行測試,國外的服務(wù)器: h31bt.org? 大家可以給提點意見...

?

開源地址:https://github.com/h31h31/H31DHTMgr

程序下載:H31DHT下載

下載種子文件的時候失敗很多,增加調(diào)試信息總是返回很多:Timeouts are not supported on this stream.

The remote server returned an error: (404) Not Found.

The operation has timed out.

附上之前的代碼:

private int DownLoadFileToSaveFile(string strURL, string fileName){Int32 ticktime1 = System.Environment.TickCount;try{Int32 ticktime2 = 0;byte[] buffer = new byte[4096];WebRequest wr = WebRequest.Create(strURL);wr.ContentType = "application/x-bittorrent";wr.Timeout = 300;WebResponse response = wr.GetResponse();int readsize = 0;{bool gzip = response.Headers["Content-Encoding"] == "gzip";Stream responseStream = gzip ? new GZipStream(response.GetResponseStream(), CompressionMode.Decompress) : response.GetResponseStream();using (MemoryStream memoryStream = new MemoryStream()){responseStream.ReadTimeout = 1000;int count = 0;do{count = responseStream.Read(buffer, 0, buffer.Length);memoryStream.Write(buffer, 0, count);readsize += count;Thread.Sleep(1);} while (count != 0);ticktime2 = System.Environment.TickCount;byte[] result = memoryStream.ToArray();Thread.Sleep(10);using (BinaryWriter writer = new BinaryWriter(new FileStream(fileName, FileMode.Create))){writer.Write(result);}}Int32 ticktime3 = System.Environment.TickCount;//H31Debug.PrintLn("下載成功" + strURL + ":" + readsize.ToString() + ":" + (ticktime2 - ticktime1).ToString() + "-" + (ticktime3 - ticktime2).ToString()); }return 1;}catch (Exception e){Int32 ticktime3 = System.Environment.TickCount;//H31Debug.PrintLn("下載失敗" + strURL + ":" + (ticktime3 - ticktime1).ToString());return -2;}}

測試在國內(nèi)服務(wù)器上情況很少時間,一放到國外服務(wù)器上就出現(xiàn)此問題,網(wǎng)上搜索資料最終顯示是

參考1:http://stackoverflow.com/questions/9791423/httpwebresponse-readtimeout-timeouts-not-supported

經(jīng)過代碼分析,原來Stream responseStream里面使用的TIMEOUT參數(shù)設(shè)置.

Stream responseStream = gzip ? new GZipStream(response.GetResponseStream(), CompressionMode.Decompress) : response.GetResponseStream();using (MemoryStream memoryStream = new MemoryStream()){responseStream.ReadTimeout = 1000;int count = 0;do{count = responseStream.Read(buffer, 0, buffer.Length);memoryStream.Write(buffer, 0, count);readsize += count;Thread.Sleep(1);} while (count != 0);ticktime2 = System.Environment.TickCount;byte[] result = memoryStream.ToArray();Thread.Sleep(10);using (BinaryWriter writer = new BinaryWriter(new FileStream(fileName, FileMode.Create))){writer.Write(result);}}

然后為了防止程序界面卡住,錯誤的增加了Thread.Sleep(1);其它問題可能就出現(xiàn)在此地方,由于設(shè)置超時,然后數(shù)據(jù)流就被超時退出,從而被系統(tǒng)認(rèn)為Stream沒有注銷導(dǎo)致異常,從而顯示Timeouts are not supported on this stream.

修改后的代碼為:

private int DownLoadFileToSaveFile(string strURL, string fileName,int timeout1){Int32 ticktime1 = System.Environment.TickCount;try{Int32 ticktime2 = 0;byte[] buffer = new byte[4096];WebRequest wr = WebRequest.Create(strURL);wr.ContentType = "application/x-bittorrent";wr.Timeout = timeout1;WebResponse response = wr.GetResponse();int readsize = 0;{bool gzip = response.Headers["Content-Encoding"] == "gzip";Stream responseStream = gzip ? new GZipStream(response.GetResponseStream(), CompressionMode.Decompress) : response.GetResponseStream();using (MemoryStream memoryStream = new MemoryStream()){int count = 0;do{count = responseStream.Read(buffer, 0, buffer.Length);memoryStream.Write(buffer, 0, count);readsize += count;} while (count != 0);ticktime2 = System.Environment.TickCount;byte[] result = memoryStream.ToArray();Thread.Sleep(10);using (BinaryWriter writer = new BinaryWriter(new FileStream(fileName, FileMode.Create))){writer.Write(result);}}Int32 ticktime3 = System.Environment.TickCount;//H31Debug.PrintLn("下載成功" + strURL + ":" + readsize.ToString() + ":" + (ticktime2 - ticktime1).ToString() + "-" + (ticktime3 - ticktime2).ToString()); }return 1;}catch (WebException e){Int32 ticktime3 = System.Environment.TickCount;if (e.Status == WebExceptionStatus.Timeout)//文件超時 {return -2;}else if (e.Status == WebExceptionStatus.ProtocolError)//文件不存在 {return -3;}else{H31Debug.PrintLn("下載失敗" + strURL + ":" + (ticktime3 - ticktime1).ToString() + e.Status.ToString() + e.Message);return -4;}}}


測試程序后出現(xiàn)下載失敗的HASH文件少了很多.

The remote server returned an error: (404) Not Found. 此問題是服務(wù)器沒有此文件,可以采用if (e.Status == WebExceptionStatus.ProtocolError)來判斷

The operation has timed out.?? 此問題是時間不夠,可以增加??????????????? wr.Timeout = 300;這個時間的問題.

特此記錄一下,希望大家多指教..大家可以從開源地址:https://github.com/h31h31/H31DHTMgr下載代碼一起交流下..

大家覺得好的話,希望推薦支持下...

?

?

?

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/miao31/p/3224868.html

總結(jié)

以上是生活随笔為你收集整理的[搜片神器]BT种子下载超时很多的问题分析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。