Java “软件导致连接中止:套接字写入错误”的官方原因

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2126607/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:50:04  来源:igfitidea点击:

Official reasons for "Software caused connection abort: socket write error"

javaexceptionsocketstomcatstack-trace

提问by Eran Medan

Given this stack trace snippet

鉴于此堆栈跟踪片段

Caused by: java.net.SocketException: Software caused connection abort: socket write error
 at java.net.SocketOutputStream.socketWrite0(Native Method)

引起:java.net.SocketException:软件导致连接中止:
 java.net.SocketOutputStream.socketWrite0(本机方法)处的套接字写入错误

I tried to answer the following questions:

我试图回答以下问题:

  1. What code is throwing this exception? (JVM?/Tomcat?/My code?)
  2. What causes this exception to be thrown?
  1. 什么代码抛出这个异常?(JVM?/Tomcat?/我的代码?)
  2. 是什么导致抛出这个异常?

Regarding #1:

关于#1:

Sun's JVM source doesn't contain this exact message, but I think the text Software caused connection abort: socket write erroris from the native implementation of SocketOutputStream:

Sun 的 JVM 源代码不包含此确切消息,但我认为文本软件导致连接中止:套接字写入错误来自本机实现SocketOutputStream

private native void socketWrite0(FileDescriptor fd, byte[] b, int off,
                 int len) throws IOException;

Regarding #2

关于#2

My guess is that it is caused when the client has terminated the connection, before getting the full response (e.g. sent a request, but before getting the full response, it got closed / terminated / offline)

我的猜测是它是在客户端终止连接时引起的,在获得完整响应之前(例如发送了一个请求,但在获得完整响应之前,它被关闭/终止/离线)

Questions:

问题:

  1. Are the above assumptions correct (#1 and #2)?
  2. Can this be diffrentiated from the situation: "could not write to the client, due to a network error on the serverside"? or would that render the same error message?
  3. And most important: Is there an official document (e.g from Sun) stating the above?
  1. 上述假设是否正确(#1 和 #2)?
  2. 这可以与以下情况区别开来:“由于服务器端的网络错误,无法写入客户端”?还是会呈现相同的错误消息?
  3. 最重要的是否有官方文件(例如来自 Sun)说明上述内容?

I need to have a proof that this stack trace is the socket client's "fault", and there is nothing that the server could have done to avoid it. (except catching the exception, or using a non Sun JVM SocketOutputStream, though both don't really avoid the fact the client has terminated)

我需要证明这个堆栈跟踪是套接字客户端的“错误”,并且服务器无法避免它。(除了捕获异常,或使用非 Sun JVM SocketOutputStream,尽管两者都没有真正避免客户端已终止的事实)

采纳答案by user207421

This error can occur when the local network system aborts a connection, such as when WinSock closes an established connection after data retransmission fails (receiver never acknowledges data sent on a datastream socket).

当本地网络系统中止连接时,例如当 WinSock 在数据重新传输失败后关闭已建立的连接(接收器从不确认数据流套接字上发送的数据)时,可能会发生此错误。

See this MSDN article. See also Some information about 'Software caused connection abort'.

请参阅此 MSDN 文章。另请参阅有关“软件导致连接中止”的一些信息

回答by Brian Agnew

Have you checked the Tomcat source code andthe JVM source ? That may give you more help.

您是否检查过 Tomcat 源代码JVM 源代码?那可能会给你更多的帮助。

I think your general thinking is good. I would expect a ConnectExceptionin the scenario that you couldn't connect. The above looks very like it's client-driven.

我认为你的总体思路很好。我希望ConnectException在您无法连接的情况下。上面看起来很像它是客户端驱动的。

回答by stacker

To prove which component fails I would monitor the TCP/IP communication using wiresharkand look who is actaully closing the port, also timeouts could be relevant.

为了证明哪个组件出现故障,我将使用wireshark监视TCP/IP 通信并查看谁实际关闭了端口,超时也可能相关。

回答by user2028913

I have seen this most often when a corporate firewall on a workstation/laptop gets in the way, it kills the connection.

当工作站/笔记本电脑上的企业防火墙妨碍连接时,我最常看到这种情况。

eg. I have a server process and a client process on the same machine. The server is listening on all interfaces (0.0.0.0) and the client attempts a connection to the public/home interface (note not the loopback interface 127.0.0.1).

例如。我在同一台机器上有一个服务器进程和一个客户端进程。服务器正在侦听所有接口 (0.0.0.0),客户端尝试连接到公共/本地接口(注意不是环回接口 127.0.0.1)。

If the machine is has its network disconnected (eg wifi turned off) then the connection is formed. If the machine is connected to the corporate network (directly or vpn) then the connection is formed.

如果机器的网络断开(例如wifi关闭),则连接形成。如果机器连接到公司网络(直接或 vpn),则连接形成。

However, if the machine is connected to a public wifi (or home network) then the firewall kicks in an kills the connection. In this situation connecting the client to the loopback interface works fine, just not to the home/public interface.

但是,如果机器连接到公共 wifi(或家庭网络),则防火墙会启动并终止连接。在这种情况下,将客户端连接到环回接口工作正常,只是不能连接到家庭/公共接口。

Hope this helps.

希望这可以帮助。

回答by PRO_gramista

For anyone using simple Client Server programms and getting this error, it is a problem of unclosed (or closed to early) Input or Output Streams.

对于使用简单的客户端服务器程序并收到此错误的任何人来说,这是未关闭(或关闭到早期)输入或输出流的问题。

回答by Sefi Erlich

My server was throwing this exception in the pass 2 days and I solved it by moving the disconnecting function with:

我的服务器在过去 2 天内抛出了这个异常,我通过移动断开连接功能解决了这个问题:

outputStream.close();
inputStream.close();
Client.close();

To the end of the listing thread. if it will helped anyone.

到列表线程的末尾。如果它会帮助任何人。

回答by Nirav Chhatrola

I was facing the same issue.
Commonly This kind of error occurs due to client has closed its connection and server still trying to write on that client.
So make sure that your client has its connection open until server done with its outputstream.
And one more thing, Don`t forgot to close input and output stream.

我面临同样的问题。
通常发生这种错误是由于客户端已关闭其连接而服务器仍在尝试在该客户端上写入。
因此,请确保您的客户端打开其连接,直到服务器完成其输出流。
还有一件事,不要忘记关闭输入和输出流。

Hope this helps.
And if still facing issue than brief your problem here in details.

希望这可以帮助。
如果仍然面临问题,请在此处详细介绍您的问题。

回答by kenorb

The java.net.SocketExceptionis thrown when there is an error creating or accessing a socket(such as TCP). This usually can be caused when the server has terminated the connection (without properly closing it), so before getting the full response. In most cases this can be caused either by the timeout issue (e.g. the response takes too much time or server is overloaded with the requests), or the client sent the SYN, but it didn't receive ACK (acknowledgment of the connection termination). For timeout issues, you can consider increasing the timeout value.

java.net.SocketException创建或访问套接字(例如TCP)时出现错误时抛出。这通常是在服务器终止连接(没有正确关闭它)时引起的,因此在获得完整响应之前。在大多数情况下,这可能是由超时问题引起的(例如,响应花费了太多时间或服务器因请求而过载),或者客户端发送了 SYN,但它没有收到 ACK(连接终止的确认) . 对于超时问题,可以考虑增加超时值。

The Socket Exception usually comes with the specified detail message about the issue.

套接字异常通常带有有关该问题的指定详细消息。

Example of detailed messages:

详细消息示例:

  • Software caused connection abort: recv failed.

    The error indicates an attempt to send the message and the connection has been aborted by your server. If this happened while connecting to the database, this can be related to using not compatible Connector/J JDBC driver.

    Possible solution: Make sure you've proper libraries/drivers in your CLASSPATH.

  • Software caused connection abort: connect.

    This can happen when there is a problem to connect to the remote. For example due to virus-checker rejecting the remote mail requests.

    Possible solution: Check Virus scan service whether it's blocking the port for the outgoing requests for connections.

  • Software caused connection abort: socket write error.

    Possible solution: Make sure you're writing the correct length of bytes to the stream. So double check what you're sending. See this thread.

  • Connection reset by peer: socket write error / Connection aborted by peer: socket write error

    The application did not check whether keep-alive connection had been timed out on the server side.

    Possible solution: Ensure that the HttpClient is non-null before reading from the connection.E13222_01

  • Connection reset by peer.

    The connection has been terminated by the peer (server).

  • Connection reset.

    The connection has been either terminated by the client or closed by the server end of the connection due to request with the request.

    See: What's causing my java.net.SocketException: Connection reset?

  • 软件导致连接中止:接收失败。

    该错误表示尝试发送消息并且连接已被您的服务器中止。如果在连接到数据库时发生这种情况,这可能与使用不兼容的 Connector/J JDBC 驱动程序有关

    可能的解决方案:确保您的 CLASSPATH 中有正确的库/驱动程序。

  • 软件导致连接中止:connect。

    当连接到遥控器出现问题时,可能会发生这种情况。例如,由于病毒检查器拒绝远程邮件请求

    可能的解决方案:检查病毒扫描服务是否阻塞了用于发出连接请求的端口。

  • 软件导致连接中止:套接字写入错误。

    可能的解决方案:确保将正确长度的字节写入流。因此,请仔细检查您发送的内容。看到这个线程

  • 对等方重置连接:套接字写错误/对等方中止连接:套接字写错误

    应用程序没有检查服务器端的保持连接是否超时。

    可能的解决方案:在从连接读取之前确保 HttpClient 为非空。E13222_01

  • 对等方重置连接。

    连接已被对等方(服务器)终止。

  • 连接重置。

    连接要么被客户端终止,要么被连接的服务器端关闭,因为请求带有请求。

    请参阅:是什么导致了我的 java.net.SocketException:连接重置?

回答by Marco

This error happened to me while testing my soap service with SoapUI client, basically I was trying to get a very big message (>500kb) and SoapUI closed the connection by timeout.

这个错误发生在我用 SoapUI 客户端测试我的肥皂服务时,基本上我试图获得一个非常大的消息(> 500kb)并且 SoapUI 超时关闭了连接。

On SoapUI go to:

File-->Preferences--Socket Timeout(ms)

在 SoapUI 上转到:

文件-->首选项--套接字超时(毫秒)

...and put a large value, such as 180000 (3 minutes), this won't be the perfect fix for your issue because the file is in fact to large, but at least you will have a response.

...并输入一个较大的值,例如 180000(3 分钟),这不是解决您问题的完美方法,因为文件实际上太大了,但至少您会有响应。

回答by Yallaling Goudar

I was facing the same problem with wireMock while mocking the rest API calls. Earlier I was defining the server like this:

我在模拟其余 API 调用时遇到了与 wireMock 相同的问题。早些时候我是这样定义服务器的:

WireMockServer wireMockServer = null;

But it should be defined like as shown below:

但它应该定义如下:

@Rule 
public WireMockRule wireMockRule = new WireMockRule(8089);