[搜片神器]BT种子下载超时很多的问题分析
?
繼續(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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python 零基础入门到实战(一)笔记
- 下一篇: 实话实说别直说