Software caused connection failedabort: recv failed怎么解决

Software&caused&connection&abort:&recv&failed
今天发现程序出现了这个异常:Software caused connection abort: recv failed,
简单记录一下:
出现这个异常是因为客户端网络连接断了,查了相关资料,简单总结一下,
在服务端/客户端单方面关闭连接的情况下,另一方依然以为
tcp连接仍然建立,试图读取对方的响应数据,导致出现
Software caused connection abort: recv failed的异常.
因此在receive数据之前,要先判断连接状态.
通过inputstream的available()方法来判断,是否有响应结果.
如果available()的返回值为0,说明没有响应数据,可能是对方已经断开连接,
如果available()的返回值大于0,说明有响应数据.
另外值得注意的是available()返回的值是非堵塞的,可以被多个线程访问
&&&&&&&&&&&
URL localurl = new URL(url) ;
URLConnection uc = localurl.openConnection() ;
uc.setRequestProperty("User-Agent","Mozilla/3.5.7 ( MSIE
5.0; Windows NT; DigExt)");
&&& uc.connect()
&&& InputStream
localObject1 = localurl.openStream();
System.out.println(localObject1.available()) ;
&&& byte[]
localObject2 = new byte[131072];
&&& StringBuffer
localStringBuffer = new StringBuffer() ;
&&& int j = 0
&&& while ((j =
(localObject1).read(localObject2)) & 0){
localStringBuffer.append(new String(localObject2, 0, j,
encoder));
localObject1.close() ;
修改后代码:
&&&&&&&&&&&
URL localurl = new URL(url) ;
URLConnection uc = localurl.openConnection() ;
uc.setRequestProperty("User-Agent","Mozilla/3.5.7 ( MSIE
5.0; Windows NT; DigExt)");
&&& uc.connect()
&&& InputStream
localObject1 = localurl.openStream();
System.out.println(localObject1.available()) ;
&&& byte[]
localObject2 = new byte[131072];
&&& StringBuffer
localStringBuffer = new StringBuffer() ;
&&& int j = 0
& & while(true){
if(localObject1 .available()&0){
if((y=localObject1.read(localObject2))&0){
sb.append(new String(localObject2,0,y,encode)) ;
if(in.available()==0){
System.out.println("与服务器的链接已中断") ;
localObject1.close() ;
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。java写了一个爬虫,出现Software caused connection abort: recv failed 异常
[问题点数:100分]
java写了一个爬虫,出现Software caused connection abort: recv failed 异常
[问题点数:100分]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
2013年10月 总版技术专家分月排行榜第三
2014年3月 Java大版内专家分月排行榜第一2014年1月 Java大版内专家分月排行榜第一2013年12月 Java大版内专家分月排行榜第一2013年11月 Java大版内专家分月排行榜第一2013年10月 Java大版内专家分月排行榜第一
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。最近写一个小爬虫;当多开了几个线程就会报错,一般报错的位置偏后:SocketException:
caused connection abort: recv failedI/O exception (.net.SocketException) caught when processing request: Software caused connection abort: recv failed(java.net.SocketException) caught when processing request: Connection reset在网上找了很久的资料,说是可能是没有主动关闭连接。自己找了,但一直找不出问题所在;请各位大牛赐教。Java codeprivate InputStream doGetRequest(final HttpContext context) throws Exception {
HttpResponse response = null;
HttpClient httpclient = null;
httpclient = new DefaultHttpClient();//BasicHttpClient.getHttpClient();
response = httpclient.execute(httprequest, context);
int status = response.getStatusLine().getStatusCode();
if (status == HttpStatus.SC_OK) {
BasicEntry basicEntry = this.getBasicEntry(response);
return this.getContentStream(basicEntry);
throw new CrawlerException("http status error!" + status);
} catch (Exception e) {
throw new CrawlerException(e + " " + httprequest.getURI().toString());
httprequest.releaseConnection();
httpclient.getConnectionManager().closeExpiredConnections();
httpclient.getConnectionManager().closeIdleConnections(0, TimeUnit.SECONDS);
httpclient.getConnectionManager().shutdown();
}Java codeprivate BasicEntry getBasicEntry(HttpResponse response) throws Exception {
InputStream in = null;
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
HttpEntity entity =response.getEntity();
if( entity != null ){
in = entity.getContent();
if(in.available() &= 0){
int c = -1;
while ((c = in.read()) != -1) {
byteArray.write(c);
byte[] bytes = byteArray.toByteArray();
String charset = EntityUtils.getContentCharSet(response.getEntity());
String contentType = null;
contentType = response.getEntity().getContentType().getValue().toLowerCase();
} catch (Exception e) {
contentType = "text/html";
return new BasicEntry(bytes, contentType, charset);
} catch (Exception e) {
if (in != null){
in.close();
catch (IOException e)
e.printStackTrace ();
--------------------------------------
是不是jdk版本的问题,jdk1.7出现这个问题,但换成jdk1.6就好了。。。6419人阅读
写socket的出现的异常
&&try {&&&InputStream is = socket.getInputStream();&&&BufferedReader br = new BufferedReader(new InputStreamReader(is));&&&S&&&while(true){&&&&&if((line = br.readLine()) != null){ &&&&&&//do ..&&&&&}&&&&//设置超时 关闭socket&&&}&&} catch (IOException e) {&&&e.printStackTrace();&&}&
注意红色修改部分:
try {&&&InputStream is = socket.getInputStream();&&&BufferedReader br = new BufferedReader(new InputStreamReader(is));&&&S&&&while(true){&&&&if(is.available()&0){&&&&&if((line = br.readLine()) != null){ &&&&&&//do ..&&&&&}&&&&}else{&&&&&&&&&}&&&&//设置超时 关闭socket&&&}&&} catch (IOException e) {&&&e.printStackTrace();&&}
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:64713次
积分:1059
积分:1059
排名:千里之外
原创:37篇
评论:16条
(2)(2)(3)(2)(2)(2)(1)(4)(2)(1)(3)(1)(2)(3)(1)(4)(1)(1)

我要回帖

更多关于 recv failed 的文章

 

随机推荐