简体   繁体   English

软件导致连接异常终止:写入DataOutputStream时套接字写入错误

[英]Software caused connection abort: socket write error while writing to DataOutputStream

 private DataOutputStream output = null;
 private int send(String str) throws IOException {
  int response = 0;
  output.writeBytes(str);
  output.flush();
  return response;

} }

While writing String to writeBytes it is throwing error like java.net.SocketException: Software caused connection abort: socket write error java.lang.ArrayIndexOutOfBoundsException: 512 将String写入writeBytes时会引发类似java.net.SocketException的错误:软件导致连接中止:套接字写入错误java.lang.ArrayIndexOutOfBoundsException:512

DEBUG McaFee:123 - Exception java.net.SocketException: Software caused connection abort: socket write error McaFeeC:83 - Request Header **** GET http://localhost:8080/appApp \\filepath\\ACH_IMAGE_FIVE_TWO_FIVE_zip HTTP/1.1 调试McaFee:123-异常java.net.SocketException:软件导致连接中止:套接字写入错误McaFeeC:83-请求标头**** GET http:// localhost:8080 / appApp \\ filepath \\ ACH_IMAGE_FIVE_TWO_FIVE_zip HTTP / 1.1

Your question misses the part how you initialize the DataOutputStream but the error message lets me assume that you open an HttpUrlConnection to a server and call getOutputStream on it. 您的问题错过了如何初始化DataOutputStream的部分,但是错误消息让我假设您打开了到服务器的HttpUrlConnection并在其上调用getOutputStream

Your question also misses the part where you create the String that is passed to the send method and if you indeed create a ZIP-file using a String to contain that binary data will most likely lead to corrupted data at some point. 您的问题还错过了创建传递给send方法的String的部分,如果您确实使用String包含二进制数据来创建ZIP文件,则很可能会在某些时候导致数据损坏。

My answer is based on the following code I assume is working here: 我的答案基于我认为在此处工作的以下代码:

HttpUrlConnection conn = new URL("www.example.com/upload").openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
String dataToSend = createDataToSend();
dos.writeBytes(dataToSend.getBytes("8859_1"));

What can happen here is that the HttpUrlConnection is opening the connection to the server and then the connections sits idle until you've finished creating the ZIP. 此处可能发生的情况是HttpUrlConnection正在打开与服务器的连接,然后连接处于空闲状态,直到完成创建ZIP为止。 If it takes too long the server will run into a timeout and will close the connection. 如果花费的时间太长,则服务器将超时并关闭连接。 Trying to send data to a closed socket lead to the error message you've seen. 尝试将数据发送到封闭的套接字会导致您看到错误消息。

The reason why it works up to a given number of files is that the generation is then quick enough to stay within the timeout. 它可以处理给定数量的文件的原因是,生成速度足够快,可以保持在超时范围内。

There are different solutions for this: 有不同的解决方案:

  • Create the ZIP before you open the connection 打开连接之前创建ZIP
  • Create the ZIP "on the fiy" by sending the data during creation 通过在创建过程中发送数据来创建“随时随地”的ZIP

Which one is better depends. 哪个更好取决于。 If you can be sure that there will be no error while creating the ZIP it would be my preferred way. 如果可以确定在创建ZIP时没有任何错误,那将是我的首选方式。 The former would prevent the sending of incomplete data in case of an error. 前者可以防止发生错误时发送不完整的数据。 Note that the latter needs Chunked File-Transfer-Encoding, otherwise the HttpUrlConnection will buffer everything to evaluate the value for the Content-Length header that would otherwise be needed. 请注意,后者需要Chunked File-Transfer-Encoding,否则HttpUrlConnection将缓冲所有内容以评估原本需要的Content-Length标头的值。 So if you transfer data to a server that is not supporting this, you end up with variant 1. 因此,如果将数据传输到不支持此功能的服务器,则最终会出现变体1。

Here is an example for the latter: 这是后者的示例:

private int doSend(File baseDir) throws IOException {
    File[] files = baseDir.listFiles();
    if (files.length == 0) {
        return -1;
    }
    HttpURLConnection conn = (HttpURLConnection) new URL("www.example.com/upload").openConnection();
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setChunkedStreamingMode(250);
    OutputStream os = conn.getOutputStream();
    ZipOutputStream zos = new ZipOutputStream(os);
    for (int i = 0; i < files.length; i++) {
        ZipEntry ze = new ZipEntry(files[i].getName());
        zos.putNextEntry(ze);
        try (FileInputStream fis = new FileInputStream(files[i])) {
            byte[] buf = new byte[4096];
            int read;
            while ((read = fis.read(buf)) != -1) {
                zos.write(buf, 0, read);
            }
        }
    }
    zos.flush();
    return conn.getResponseCode();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 软件导致连接中止:套接字写入错误 - Software caused connection abort: socket write error jvisualvm:软件导致连接中止:套接字写入错误 - jvisualvm: Software caused connection abort: socket write error “软件导致连接中止:套接字写入错误”的官方原因 - Official reasons for "Software caused connection abort: socket write error" 在FTP中存储ObjectOutputStream导致java.net.SocketException:软件导致连接中止:套接字写入错误 - Store ObjectOutputStream in FTP causes java.net.SocketException: Software caused connection abort: socket write error java.net.SocketException:软件导致连接中止:重新提交请求时套接字写入错误 - java.net.SocketException: Software caused connection abort: socket write error when resubmitting the request javax.servlet.jsp.JspException:无法插入页面…软件导致连接中止:套接字写入错误 - javax.servlet.jsp.JspException: Can't insert page … Software caused connection abort: socket write error 通过串行端口java.net.SocketException的通信:软件导致连接中止:套接字写入错误 - Communication through serial port, java.net.SocketException: Software caused connection abort: socket write error 读取输入流时出错:软件导致连接中止'在java服务器中 - Error while reading inputstream : Software caused connection abort' in java server Maven Web项目在eclipse(tomcat 7)中运行错误:错误:无法调用Tomcat管理器:软件导致连接中止:套接字写入错误 - The maven web project runs error in eclipse(tomcat 7):error:Cannot invoke Tomcat manager: Software caused connection abort: socket write error 软件导致连接中止:即使从最近的列表中清除了该应用程序,套接字写入错误(它在与服务不同的线程中运行) - Software caused connection abort: socket write error on clearing the app from recent list even (it running in a different thread from a service)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM