`

Connection reset by peer: socket write error

阅读更多

今天做了个过滤图片的servlet,功能是用来匹配自定义的文件后缀,如果是该后缀,则通过该文件的id到数据库的附件中将文件读出并将byte[]写入response的out流中,但经常会报错,初步排查原因是由于浏览器的Aborted,阻止了请求的响应,异常如下:

Java代码 复制代码 收藏代码
  1. ClientAbortException: java.net.SocketException: Connection reset by peer: socket write error at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:366)
  2. at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:433)
  3. at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:348)
ClientAbortException:  java.net.SocketException: Connection reset by peer: socket write error at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:366)    
        at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:433)    
        at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:348)  


在BAIDU和GOOGLE上找了下原因,大概归结为:
ClientAbortException: java.net.SocketException: Connection reset by peer: socket write error的原因是由于处理http连接时,正在输出内容时,用户关闭了IE,会出现一个"ClientAbortException",属于I/O处理中出现的一个异常,应用服务器应该会捕捉。
Connection reset by peer的原因:
经常出现的Connection reset by peer: 原因可能是多方面的,不过更常见的原因是:
①:服务器的并发连接数超过了其承载量,服务器会将其中一些连接Down掉;
②:客户关掉了浏览器,而服务器还在给客户端发送数据;
③:浏览器端按了Stop
很多人都说是客户端造成的,没有办法控制,是个比较郁闷的问题。

但是,我担心的是:虽然前台没有任何出错的迹象,但是后台会记录这个异常,日志也会疯狂爆满,时间长了,肯定会DOWN掉的,还没找到好的解决办法。

暂时HACK式的解决方法:
把下载文件的代码放在try……catch块中,在catch部分使用判断避免让系统抛出ClientAbortException,具体方法可以这样:

Java代码 复制代码 收藏代码
  1. try{
  2. ...
  3. }catch(Exception ex){
  4. String simplename = ex.getClass().getSimpleName();
  5. if(!"ClientAbortException".equals(simplename)){
  6. ex.printStackTrace();
  7. }
  8. }   
0
0
分享到:
评论
1 楼 hw1287789687 2013-08-15  
ClientAbortException:  java.net.SocketException: Connection reset by peer: socket write error
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:406)
at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:480)
at org.apache.catalina.connector.OutputBuffer.doFlush(OutputBuffer.java:352)
at org.apache.catalina.connector.OutputBuffer.close(OutputBuffer.java:303)
at org.apache.catalina.connector.CoyoteOutputStream.close(CoyoteOutputStream.java:108)
at org.apache.struts2.dispatcher.StreamResult.doExecute(StreamResult.java:305)
at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)
at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:374)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:278)
at com.tdr.mbs.core.http.interceptor.JSONWorkFlowInterceptor.intercept(JSONWorkFlowInterceptor.java:79)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.tdr.mbs.core.http.interceptor.JSONInterceptor.intercept(JSONInterceptor.java:78)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.tdr.mbs.endpoint.core.http.interceptor.TokenCheckInterceptor.intercept(TokenCheckInterceptor.java:68)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.tdr.mbs.endpoint.core.http.interceptor.HeaderParsingInterceptor.intercept(HeaderParsingInterceptor.java:57)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.tdr.mbs.core.http.interceptor.MbsExceptionInterceptor.intercept(MbsExceptionInterceptor.java:70)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:511)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at org.apache.coyote.http11.InternalOutputBuffer.realWriteBytes(InternalOutputBuffer.java:215)
at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:480)
at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:366)
at org.apache.coyote.http11.InternalOutputBuffer$OutputStreamOutputBuffer.doWrite(InternalOutputBuffer.java:240)
at org.apache.coyote.http11.filters.IdentityOutputFilter.doWrite(IdentityOutputFilter.java:84)
at org.apache.coyote.http11.AbstractOutputBuffer.doWrite(AbstractOutputBuffer.java:192)
at org.apache.coyote.Response.doWrite(Response.java:505)
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:401)
... 46 more

相关推荐

    java.net.SocketException Connection reset 解决方法

    * Connection reset by peer:Connection reset by peer 是一种特殊的 SocketException,它发生在客户端和服务器端之间的连接断开后,导致连接的一端继续发送数据,引发该异常。 实践经验 通过实践经验,我们可以...

    Modbus TCP客户端和服务器示例

    **Modbus TCP客户端和服务器示例** 在工业自动化和物联网(IoT)领域,Modbus是一种广泛使用的通信协议,它允许设备之间进行简单的数据交换。本文将深入探讨Modbus TCP,这是一种在网络环境中运行的Modbus变体,尤其...

    connection reset by peer问题总结及解决方案

    connection reset by peer问题总结及解决方案 1.服务器的并发连接数超过了其承载量,服务器会将其中一些连接关闭 如果知道实际连接服务器的并发客户端数并没有超过服务器的承载量,则有可能是中了病毒或者木马,引起...

    Socket长连接异常处理

    java.net.SocketException: Connection reset by peer: Socket write error 该异常可能发生在客户端和服务器端,原因是因为一端的 Socket 被关闭,而另一端继续发送数据。解决方法是确保在关闭连接前完成所有的数据...

    J2EE应用开发问题总结及最佳实践.pdf

    1. 使用 Oracle 数据库,大于 4k 的附件上传有问题,会报错 java.sql.SQLException: Connection reset by peer: socket write error。 解决办法是在插入 BLOB 字段值时,要先用 SELECT BLOBCOL FROM TABLENAME FOR ...

    javaerror处理资料

    java.net.SocketException: Connection reset by peer: Socket write error 此异常通常出现在客户端尝试向服务器写入数据时突然断开连接的情况。这种现象的原因可能是远程服务器突然关闭了连接,或者客户端发送的...

    DBCP连接池DBCP和C3P0配置

    根据提供的部分内容,“java.sql.SQLException: Already closed.”和“java.sql.SQLException: I/O exception: Connection reset by peer: socket write error”这类异常通常与数据库连接管理不当有关,如连接超时、...

    struts2+jsp实现文件上传的方法

    在文件上传或下载时,可能会遇到`SocketException: Connection reset by peer: socket write error`的异常。这个异常通常是由于网络中断或者客户端提前关闭连接引起的。在开发中,可以通过捕获异常并进行相应的处理...

    Java Socket常见异常处理

    4. **java.net.SocketException: (Connection reset 或 Connect reset by peer: Socket write error)** 这两种异常都表示连接已断开。可能的原因是一端关闭了Socket,而另一端仍在尝试读写。处理这类异常需要检测到...

    python中关于网络socket使用实例,供大家参考使用

    except ConnectionResetError: print('Connection reset by peer.') ``` 六、多线程或异步IO 在处理多个并发连接时,可以使用多线程或多进程,或者利用Python的asyncio库进行异步I/O。例如,使用asyncio创建一个...

Global site tag (gtag.js) - Google Analytics