java.net.SocketException:软件导致连接中止:recv失败[重复]

时间:2022-11-15 23:57:43

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

java.net.SocketException:软件导致连接中止:recv失败

Notes:

笔记:

  • This error is infrequent and unpredictable; although getting this error means that all future requests for URIs will also fail.
  • 这个错误很少且不可预测;虽然获取此错误意味着将来所有URI请求也将失败。
  • The only solution that works (also, only occasionally) is to reboot Tomcat and/or the actual machine (Windows in this case).
  • 有效的唯一解决方案(也是偶尔)是重启Tomcat和/或实际的机器(本例中是Windows)。
  • The URI is definitely available (as confirmed by asking the browser to do the fetch).
  • URI绝对可用(通过要求浏览器进行提取来确认)。

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();

10 个解决方案

#1


22  

This usually means that there was a network error, such as a TCP timeout. I would start by placing a sniffer (wireshark) on the connection to see if you can see any problems. If there is a TCP error, you should be able to see it. Also, you can check your router logs, if this is applicable. If wireless is involved anywhere, that is another source for these kind of errors.

这通常意味着存在网络错误,例如TCP超时。我首先在连接上放置一个嗅探器(wireshark),看看你是否能看到任何问题。如果存在TCP错误,您应该能够看到它。此外,如果适用,您可以检查路由器日志。如果任何地方都涉及无线,那么这就是这类错误的另一个来源。

#2


17  

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

如果您的TLS客户端无法由配置为需要客户端身份验证的服务器进行身份验证,也会发生这种情况。

#3


5  

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).

当连接突然关闭时(当发送缓冲区中仍有数据时重置TCP连接时)会发生此错误。这种情况非常类似于更常见的“由同伴重置连接”。它可能在通过Internet连接时偶尔发生,但如果时机正确则可系统地发生(例如,在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.

HTTP客户端应该只是重新打开连接并重试请求。重要的是要理解,当一个连接处于这种状态时,除了关闭它之外没有任何方法可以解决它。任何发送或接收的尝试都会产生相同的错误。

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

不要使用URL.open(),使用Apache-Commons HttpClient,它具有重试机制,连接池,保持活动和许多其他功能。

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 ...

#4


4  

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.

我见过这样的事情的唯一一次是当我有一个错误的连接,或者有人正在关闭我从不同的线程上下文使用的套接字。

#5


3  

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数据吗?你能使用HttpClient库而不是标准库吗?该库有更多选项,可以提供更好的错误消息。

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

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

#6


2  

Try adding 'autoReconnect=true' to the jdbc connection string

尝试将“autoReconnect = true”添加到jdbc连接字符串

#7


1  

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.

当连接超时或远程主机终止其连接(关闭应用程序,计算机关闭等)时,这将不时发生。您可以通过自己管理套接字并通过其通信协议处理应用程序中的断开连接,然后调用shutdownInput和shutdownOutput来清除会话来避免这种情况。

#8


1  

Look if you have another service or program running on the http port. It happened to me when I tried to use the port and it was taken by another program.

查看您是否在http端口上运行了其他服务或程序。当我尝试使用该端口并且它被另一个程序占用时,它发生在我身上。

#9


0  

If you are using Netbeans to manage Tomcat, try to disable HTTP monitor in Tools - Servers

如果您使用Netbeans管理Tomcat,请尝试在工具 - 服务器中禁用HTTP监视器

#10


0  

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.

从网站复制 - >通过使用setSoLinger()方法,您可以在发送重置之前明确设置延迟,从而为读取或发送数据留出更多时间。

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

也许这不是每个人的答案,而是某些人的答案。

#1


22  

This usually means that there was a network error, such as a TCP timeout. I would start by placing a sniffer (wireshark) on the connection to see if you can see any problems. If there is a TCP error, you should be able to see it. Also, you can check your router logs, if this is applicable. If wireless is involved anywhere, that is another source for these kind of errors.

这通常意味着存在网络错误,例如TCP超时。我首先在连接上放置一个嗅探器(wireshark),看看你是否能看到任何问题。如果存在TCP错误,您应该能够看到它。此外,如果适用,您可以检查路由器日志。如果任何地方都涉及无线,那么这就是这类错误的另一个来源。

#2


17  

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

如果您的TLS客户端无法由配置为需要客户端身份验证的服务器进行身份验证,也会发生这种情况。

#3


5  

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).

当连接突然关闭时(当发送缓冲区中仍有数据时重置TCP连接时)会发生此错误。这种情况非常类似于更常见的“由同伴重置连接”。它可能在通过Internet连接时偶尔发生,但如果时机正确则可系统地发生(例如,在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.

HTTP客户端应该只是重新打开连接并重试请求。重要的是要理解,当一个连接处于这种状态时,除了关闭它之外没有任何方法可以解决它。任何发送或接收的尝试都会产生相同的错误。

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

不要使用URL.open(),使用Apache-Commons HttpClient,它具有重试机制,连接池,保持活动和许多其他功能。

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 ...

#4


4  

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.

我见过这样的事情的唯一一次是当我有一个错误的连接,或者有人正在关闭我从不同的线程上下文使用的套接字。

#5


3  

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数据吗?你能使用HttpClient库而不是标准库吗?该库有更多选项,可以提供更好的错误消息。

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

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

#6


2  

Try adding 'autoReconnect=true' to the jdbc connection string

尝试将“autoReconnect = true”添加到jdbc连接字符串

#7


1  

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.

当连接超时或远程主机终止其连接(关闭应用程序,计算机关闭等)时,这将不时发生。您可以通过自己管理套接字并通过其通信协议处理应用程序中的断开连接,然后调用shutdownInput和shutdownOutput来清除会话来避免这种情况。

#8


1  

Look if you have another service or program running on the http port. It happened to me when I tried to use the port and it was taken by another program.

查看您是否在http端口上运行了其他服务或程序。当我尝试使用该端口并且它被另一个程序占用时,它发生在我身上。

#9


0  

If you are using Netbeans to manage Tomcat, try to disable HTTP monitor in Tools - Servers

如果您使用Netbeans管理Tomcat,请尝试在工具 - 服务器中禁用HTTP监视器

#10


0  

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.

从网站复制 - >通过使用setSoLinger()方法,您可以在发送重置之前明确设置延迟,从而为读取或发送数据留出更多时间。

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

也许这不是每个人的答案,而是某些人的答案。