令人头疼的Connection Reset
背景:
要爬取某网站的数据,数据每页10条,有很多页(形式如同table表格)。使用HttpClient 逐行逐页爬取数据,但在循环爬取多次时,总会在不确定的位置报错
在检查代码逻辑无果之后,开始疯狂百度,网上给出的解释:
服务器端因为某种原因关闭了Connection,而客户端依然在读写数据。
给出的解决方案是:
- 客户端和服务器统一使用TCP长连接或者短连接。
- 客户端关闭了连接,检查代码,并无关闭。
以上两种情况均无法解决,于是决定自己看错误源码:
int read(byte b[], int off, int length, int timeout) throws IOException {
int n; // EOF already encountered
if (eof) {
return -1;
} // connection reset
if (impl.isConnectionReset()) {
throw new SocketException("Connection reset");
} // bounds check
if (length <= 0 || off < 0 || length > b.length - off) {
if (length == 0) {
return 0;
}
throw new ArrayIndexOutOfBoundsException("length == " + length
+ " off == " + off + " buffer length == " + b.length);
} boolean gotReset = false; // acquire file descriptor and do the read
FileDescriptor fd = impl.acquireFD();
try {
n = socketRead(fd, b, off, length, timeout);
if (n > 0) {
return n;
}
} catch (ConnectionResetException rstExc) {
gotReset = true;
} finally {
impl.releaseFD();
} /*
* We receive a "connection reset" but there may be bytes still
* buffered on the socket
*/
if (gotReset) {
impl.setConnectionResetPending();
impl.acquireFD();
try {
n = socketRead(fd, b, off, length, timeout);
if (n > 0) {
return n;
}
} catch (ConnectionResetException rstExc) {
} finally {
impl.releaseFD();
}
} /*
* If we get here we are at EOF, the socket has been closed,
* or the connection has been reset.
*/
if (impl.isClosedOrPending()) {
throw new SocketException("Socket closed");
}
if (impl.isConnectionResetPending()) {
impl.setConnectionReset();
}
if (impl.isConnectionReset()) {
throw new SocketException("Connection reset");
}
eof = true;
return -1;
根据图片中的提示信息,可以找到报错信息在倒数第4行,从后往前看,当n <= 0时,才会报错,然而
n = socketRead(fd, b, off, length, timeout);
认为是超时问题,故在代码中加入
但依旧未解决问题,最终通过手动捕捉SocketException异常,让异常发生时,重新请求该条记录,完成任务。
虽然问题解决了,但本质还是不理解为什么会导致错误,有明白的大佬麻烦指点一二。
令人头疼的Connection Reset的更多相关文章
- java.net.SocketException: Connection reset
java.net.SocketException: Connection reset at java.net.SocketInputStream.read(SocketInputStream.java ...
- ”Connection reset by peer“引发的思考
闲来无事,把之前写的一个游戏服务器框架(<一个java页游服务器框架>),部署到阿里云服务器上,测试运行了下,结果看到后台log中打印出了“Connection reset by peer ...
- 困扰我多年的Connection reset问题
第一次出现:是thrift的python client去请求server,发现偶尔出现这个问题 第二次:接入第三方的api,去请求数据时,发现一个接入方的api第一次总是报这个错,当时又没有做处理,导 ...
- TNS-12547 Linux Error: 104: Connection reset by pe (转载)
TNS-12547 Linux Error: 104: Connection reset by peer 解决过程参考:http://blog.chinaunix.net/u/7121/showart ...
- Error -27780: [GENERAL_MSG_CAT_SSL_ERROR]connect to host "124.202.213.70" failed: [10054] Connection reset by peer [MsgId: MERR-27780]
解决方案一: 备注: 此方案如果请求响应时间太长,勾选"WinInet replay instead of Sockets(Windows only)"将会导致如下错误:
- ab测试出现error: connection reset by peer的解决方案
我们在使用一些开源程序之前,可能会使用ab工具在服务器或者本地进行一次性能评估,但是很多时候却总是会以失败告终,因为,服务器会拒绝你的ab工具发出的http请求, 出现 error: connecti ...
- gem install 出现Errno::ECONNRESET: Connection reset by peer - SSL_connect (https://api.rubygems.org
在安装了rvm来管理多版本的ruby之后,想在不同环境下安装一些gems,结果gem install puma 之后,发现一次又一次失败. gem install 出现Errno::ECONNRESE ...
- OGG-01232 Receive TCP params error: TCP/IP error 104 (Connection reset by peer), endpoint:
源端: 2015-02-05 17:45:49 INFO OGG-01815 Virtual Memory Facilities for: COM anon alloc: mmap(MAP_ANON) ...
- apache ab压力测试报错(apr_socket_recv: Connection reset by peer (104))
apache ab压力测试报错(apr_socket_recv: Connection reset by peer (104)) 今天用apache 自带的ab工具测试,当并发量达到1000多的时 ...
随机推荐
- Mybatis 优化:
Mybatis 的优化: ** 第一个 对于数据库配置的优化: 创建一个 DB.properties 的文件 里面编写Key = value 形式的数据库信息 比如: driver = com.mys ...
- es6中export、export default、import的理解
export 与 import 的使用 export 与import是es6中新增模块功能最主要的两个命令.我们要知道在es6中,实现了模块功能,而且相当简单,意在取代commonjs和AMD规范.成 ...
- docker启动时报错
docker安装成功后,启动时报错. 1.后来排查后发现yum install docker安装的是从test存储库中安装的. 后来我指定了特定的版本后,而且从stable存储库安装的,以后再启动就好 ...
- BZOJ 1922--大陆争霸(最短路)
1922: [Sdoi2010]大陆争霸 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 2113 Solved: 947[Submit][Status ...
- memcache内存存储
memcache的内存分配默认是采用了Slab Allocator的机制分配.管理内存.在该机制出现以前,内存的分配是通过对所有记录简单地进行malloc和free来进行的. 但是,这种方式会导致内存 ...
- 架构师养成记--33.Redis哨兵、redis简单事务
Redis哨兵 有了主从复制,如果我想想对主从服务器进行监控,在redis2.6后提供了哨兵机制,2.6有哨兵1.0版本,并不稳定.2.8以后的哨兵功能才稳定起来. 顾名思义,哨兵就是监控Redis系 ...
- 解决织梦dedecms文档关键字(自动内链)php5.5以上失效的问题 urf-8版本的
找到include/arc.archives.class 在里面需要修改两次地方 在1230行 // 这里可能会有错误 if (version_compare(PHP_VERSION, '5.5.0' ...
- 【Alpha】任务分解与分配
Alpha阶段总体任务规划 Alpha阶段我们的任务主要是恢复原先项目的代码运行,并增加一部分物理实验(二)的内容以及完善之前项目未完成的功能,例如后台管理及用户管理界面.在恢复项目部分的主要工作是将 ...
- python附录-re.py模块源码(含re官方文档链接)
re模块 python官方文档链接:https://docs.python.org/zh-cn/3/library/re.html re模块源码 r"""Support ...
- HTML5创业的另一种可能
当一种新的颠覆式技术出现,是投靠大平台还是坚持走独立的道路? HTML5(下称H5)火了.在多数人还没完全弄清楚什么是H5时,它已悄无声息地潜入移动端,并渐呈燎原之势火速席卷云寻觅云寻觅. 以前,H5 ...