令人头疼的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多的时 ...
随机推荐
- P4145 上帝造题的七分钟2
题目描述 "第一分钟,X说,要有数列,于是便给定了一个正整数数列. 第二分钟,L说,要能修改,于是便有了对一段数中每个数都开平方(下取整)的操作. 第三分钟,k说,要能查询,于是便有了求一段 ...
- [Swift实际操作]八、实用进阶-(3)闭包在定时任务、动画和线程中的使用实际操作
闭包的使用相当广泛,它是可以在代码中被传递和引用的具有独立功能的模块.双击打开之前创建的空白项目.本文将演示闭包在定时任务.动画和线程中的使用.在左侧的项目导航区,打开视图控制器的代码文件:ViewC ...
- CentOS6.5加域
为减少错误已提前关掉了SELinux,防火墙. yum install nss-pam-ldapd -y 第一步:更改主机名为linux.itxdm.me 第二步:更改setup内左边Winbind ...
- Spring注解大全,汇总版
Spring使用的注解大全和解释 注解 解释 @Controller 组合注解(组合了@Component注解),应用在MVC层(控制层),DispatcherServlet会自动扫描注解了此注解的类 ...
- P3292 [SCOI2016]幸运数字
题目链接 题意分析 一句话题意 : 树上一条链中挑选出某些数 异或和最大 我们可以考虑维护一个树上倍增线性基 然后倍增的时候 维护一个线性基合并就可以了 写起来还是比较容易的 CODE: #inclu ...
- 架构师养成记--16.disruptor并发框架中RingBuffer的使用
很多时候我们只需要消息中间件这样的功能,那么直需要RinBuffer就可以了. 入口: import java.util.concurrent.Callable; import java.util.c ...
- 工程手机设置apn
http://android.stackexchange.com/questions/117125/how-do-i-add-an-apn-via-adb xiaomi3 su mount permi ...
- MySQL的库、表详细操作
本节目录 一.库操作 二.表操作 三.行操作 一.库操作 1.创建数据库 1.1 语法 CREATE DATABASE 数据库名 charset utf8; 1.2 数据库命名规则 可以由字母.数字. ...
- 使用nginx+uwsgi+Django环境部署
环境准备 Python点这里 nginx点这里 uwsgi点这里
- 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...