java.net.SocketException:软件导致连接中止:recv 失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/135919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.net.SocketException: Software caused connection abort: recv failed
提问by grammar31
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: 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).
- 此错误不常见且不可预测;尽管收到此错误意味着以后对 URI 的所有请求也将失败。
- 唯一有效的解决方案(也只是偶尔)是重新启动 Tomcat 和/或实际机器(在这种情况下为 Windows)。
- 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();
采纳答案by AdamC
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 错误,您应该能够看到它。此外,您可以检查路由器日志(如果适用)。如果在任何地方都涉及无线,那是此类错误的另一个来源。
回答by Ken
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 库而不是标准库吗?该库有更多选项,将提供更好的错误消息。
回答by pfranza
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.
我唯一见过这样的事情发生的时候是当我的连接不好时,或者当有人从不同的线程上下文中关闭我正在使用的套接字时。
回答by Anantharaman
Try adding 'autoReconnect=true' to the jdbc connection string
尝试将 'autoReconnect=true' 添加到 jdbc 连接字符串
回答by Michael J. Gray
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
清除会话来避免这种情况。
回答by trinity
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 端口上运行。当我尝试使用该端口并被另一个程序占用时,它发生在我身上。
回答by Maro? Ko?ina
If you are using Netbeans to manage Tomcat, try to disable HTTP monitor in Tools - Servers
如果您使用 Netbeans 管理 Tomcat,请尝试在工具 - 服务器中禁用 HTTP 监视器
回答by desbocages
This also happens if your TLS client is unable to be authenticate by the server configured to require client authentication.
如果您的 TLS 客户端无法通过配置为要求客户端身份验证的服务器进行身份验证,也会发生这种情况。
回答by Sergio
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.
也许这不是每个人的答案,而是某些人的答案。
回答by rustyx
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 连接时,它可能会偶尔发生,但如果时机合适(例如,在本地主机上保持活动连接),它也会系统地发生。
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 HttpClientwhich 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 ...