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

news/2024/10/18 18:22:56/

 

继续接着第一篇写:使用C#实现DHT磁力搜索的BT种子后端管理程序+数据库设计(开源)[搜片神器]

 

谢谢园子朋友的支持,已经找到个VPS进行测试,国外的服务器: h31bt.org  大家可以给提点意见...

 

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

程序下载:H31DHT下载

下载种子文件的时候失败很多,增加调试信息总是返回很多: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;}}

测试在国内服务器上情况很少时间,一放到国外服务器上就出现此问题,网上搜索资料最终显示是

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

经过代码分析,原来Stream responseStream里面使用的TIMEOUT参数设置.

                    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);其它问题可能就出现在此地方,由于设置超时,然后数据流就被超时退出,从而被系统认为Stream没有注销导致异常,从而显示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;}}}


测试程序后出现下载失败的HASH文件少了很多.

The remote server returned an error: (404) Not Found. 此问题是服务器没有此文件,可以采用if (e.Status == WebExceptionStatus.ProtocolError)来判断

The operation has timed out.   此问题是时间不够,可以增加                wr.Timeout = 300;这个时间的问题.

特此记录一下,希望大家多指教..大家可以从开源地址:https://github.com/h31h31/H31DHTMgr下载代码一起交流下..

大家觉得好的话,希望推荐支持下...

 

 

 

 

 

转载于:https://www.cnblogs.com/miao31/p/3224868.html


http://www.ppmy.cn/news/701340.html

相关文章

1964503-39-6,Carboxy-PEG4-phosphonic acid ethyl ester包含羧酸端基和膦酸乙酯部分

英文名称:Carboxy-PEG4-phosphonic acid ethyl ester 中文名称:羧基-PEG4-膦酸乙酯 化学式:C15H31O9P 分子量:386.4 CAS:1964503-39-6 纯度:95% 储存条件:-20C 运输:环境温度…

[搜片神器]BT管理程序数据库速度调试优化问题

DHT抓取程序开源地址:https://github.com/h31h31/H31DHTDEMO 数据处理程序开源地址:https://github.com/h31h31/H31DHTMgr 服务器在抓取和处理同时进行,所以访问速度慢是有些的,特别是搜索速度通过SQL的like来查询慢,正在通过分词…

java的Duration的时间格式解析,ISO-8601持续时间格式

一、前言 在配置springboot的配置的时候突然看到时间是Duration来配置的&#xff0c;上源码看到这样一个方法 /*** Obtains a {code Duration} from a text string such as {code PnDTnHnMn.nS}.* <p>* This will parse a textual representation of a duration, includ…

CAS:1260865-91-5 聚集诱导发光材料

CAS:1260865-91-5&#xff08;AIE材料&#xff09; 中文名称:1-(4-苯硼酸频哪醇酯)-1,2,2-三苯乙烯 中文同义词:1-(4-苯硼酸频哪醇酯)-1,2,2-三苯乙烯;[1-(4-硼酸频那醇酯基苯基)-1,2,2-三苯基]乙烯;4-(1,2,2-三苯乙烯基)-苯硼酸频那醇酯;1-(4-苯硼酸频哪醇酯)-1,2,2-三苯乙烯…

ClobberError: The package ‘xxx‘ cannot be installed due to a path collision for ‘xx‘ This path alre

原始链接&#xff1a;https://stackoverflow.com/questions/51217876/conda-update-anaconda-fails-clobbererror?newreg2c51dd84b04b42c49294714471612f07 造成这种问题的原因是conda和pip等相关包的版本太低了&#xff0c;自动更新不能用&#xff0c;解决方案&#xff0c;在…

汇编实验:数 据 的 建 立 与 传 送 程 序

在内存 10000H 单元开始&#xff0c;建立 00H&#xff5e;0FH&#xff5e;00H 31 个数&#xff0c;要求 00H&#xff5e;0FH 数据逐渐增大,0FH&#xff5e;00H 逐渐减小。该程序从内存 CS:0100H 地址开始输入。把上一个程序的执行结果(建立的 31 个字节数据块,其首地址在 10000…

CSS代码语法/CSS注释代码

CSS代码语法 css 样式由选择符和声明组成&#xff0c;而声明又由属性和值组成&#xff0c;如下图所示&#xff1a; 选择符&#xff1a;又称选择器&#xff0c;指明网页中要应用样式规则的元素&#xff0c;如本例中是网页中所有的段&#xff08;p&#xff09;的文字将变成蓝色&a…

4_unittest测试框架_管理测试用例生成测试报告

文章目录 前言&#xff1a;单元测试的定义什么是单元测试为什么做单元测试 1.unittest框架及原理2.unittest的断言3.编写TestCase测试用例PyCharm编译器的配置 4.TestSuite测试套件(内容较多&#xff0c;内还有链接)详细管理测试用例的4种方式 5.生成带截图测试报告1.在 Lib\si…