java.net.SocketException: Software caused connecti

2019-01-04 10:25发布

This question already has an answer here:

I haven't been able to find an adequate answer to what exactly the following error means:

java.net.SocketException: Software caused connection abort: recv failed

Notes:

  • This error is infrequent and unpredictable; although getting this error means that all future requests for URIs will also fail.
  • The only solution that works (also, only occasionally) is to reboot Tomcat and/or the actual machine (Windows in this case).
  • The URI is definitely available (as confirmed by asking the browser to do the fetch).

Relevant code:

BufferedReader reader;
try { 
 URL url = new URL(URI);
 reader = new BufferedReader(new InputStreamReader(url.openStream())));
} catch( MalformedURLException e ) { 
 throw new IOException("Expecting a well-formed URL: " + e); 
}//end try: Have a stream

String buffer;
StringBuilder result = new StringBuilder();
while( null != (buffer = reader.readLine()) ) { 
 result.append(buffer); 
}//end while: Got the contents.
reader.close();

标签: java sockets
10条回答
姐就是有狂的资本
2楼-- · 2019-01-04 11:02

Are you accessing http data? Can you use the HttpClient library instead of the standard library? The library has more options and will provide better error messages.

http://hc.apache.org/httpclient-3.x/

查看更多
虎瘦雄心在
3楼-- · 2019-01-04 11:03

The only time I've seen something like this happen is when I have a bad connection, or when somebody is closing the socket that I am using from a different thread context.

查看更多
Deceive 欺骗
4楼-- · 2019-01-04 11:04

This error occurs when a connection is closed abruptly (when a TCP connection is reset while there is still data in the send buffer). The condition is very similar to a much more common 'Connection reset by peer'. It can happen sporadically when connecting over the Internet, but also systematically if the timing is right (e.g. with keep-alive connections on localhost).

An HTTP client should just re-open the connection and retry the request. It is important to understand that when a connection is in this state, there is no way out of it other than to close it. Any attempt to send or receive will produce the same error.

Don't use URL.open(), use Apache-Commons HttpClient which has a retry mechanism, connection pooling, keep-alive and many other features.

Sample usage:

HttpClient httpClient = HttpClients.custom()
            .setConnectionTimeToLive(20, TimeUnit.SECONDS)
            .setMaxConnTotal(400).setMaxConnPerRoute(400)
            .setDefaultRequestConfig(RequestConfig.custom()
                    .setSocketTimeout(30000).setConnectTimeout(5000).build())
            .setRetryHandler(new DefaultHttpRequestRetryHandler(5, true))
            .build();
// the httpClient should be re-used because it is pooled and thread-safe.

HttpGet request = new HttpGet(uri);
HttpResponse response = httpClient.execute(request);
reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
// handle response ...
查看更多
来,给爷笑一个
5楼-- · 2019-01-04 11:06

This also happens if your TLS client is unable to be authenticate by the server configured to require client authentication.

查看更多
Luminary・发光体
6楼-- · 2019-01-04 11:06

I too had this problem. My solution was:

sc.setSoLinger(true, 10);

COPY FROM A WEBSITE -->By using the setSoLinger() method, you can explicitly set a delay before a reset is sent, giving more time for data to be read or send.

Maybe it is not the answer to everybody but to some people.

查看更多
等我变得足够好
7楼-- · 2019-01-04 11:07

This will happen from time to time either when a connection times out or when a remote host terminates their connection (closed application, computer shutdown, etc). You can avoid this by managing sockets yourself and handling disconnections in your application via its communications protocol and then calling shutdownInput and shutdownOutput to clear up the session.

查看更多
登录 后发表回答