简体   繁体   English

java.net.SocketException: Connection reset by peer: socket write error 提供文件时

[英]java.net.SocketException: Connection reset by peer: socket write error When serving a file

I am trying to implement an HTTP Server using Sockets.我正在尝试使用套接字实现 HTTP 服务器。 If the Client (For example a browser) requests a directory the server displays a list of available files.如果客户端(例如浏览器)请求目录,则服务器会显示可用文件列表。 The problem arises when the client is requesting a file.当客户端请求文件时会出现问题。 I get the following error:我收到以下错误:

java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:113)
at java.net.SocketOutputStream.write(SocketOutputStream.java:159)
at cf.charly1811.java.web.RequestHandler.writeFile(RequestHandler.java:152)
at cf.charly1811.java.web.RequestHandler.processRequest(RequestHandler.java:139)
at cf.charly1811.java.web.RequestHandler.handleRequest(RequestHandler.java:110)
at cf.charly1811.java.web.RequestHandler.run(RequestHandler.java:86)
at java.lang.Thread.run(Thread.java:745)

The stacktrace shows that the problem is coming from the writeFile() methods:堆栈跟踪显示问题来自writeFile()方法:

private void writeFile(File request) throws IOException 
{
    InputStream byteReader = new BufferedInputStream(new FileInputStream(request));
    byte[] buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = byteReader.read(buffer)) != -1) 
    {
        outputStream.write(buffer, 0, bytesRead);
    }
    byteReader.close();
}

But I can't figure out what's wrong.但我无法弄清楚出了什么问题。 Can you help me?你能帮助我吗?

EDIT编辑

Thanks everyone for your answers.谢谢大家的回答。 After I read your answers I understood that the problem was that the Socket when an error occured.阅读您的答案后,我明白问题出在发生错误时的 Socket。 Here's was my wrong code:这是我的错误代码:

// Method to process a single request
handleRequest() throw IOException
{
    // process here
    // if the client request a file
    writeFile();
    // close socket when the request is processed
}

// The method is called
public run()
{
    try{
        // If an error occurs the try/catch won't be called because it is implemented outside the loop. So when an IOException occurs, the loop just stop and exit the program
        while(true)
        {
            handleRequest();
        }
    }
    catch(IOException e) {
        // Handle exception here
    }
}

And my new code was looking like this:我的新代码如下所示:

// Method to process a single request
handleRequest()
{
   try {
        // process here
        // if the client request a file
        writeFile();
        // close socket when the request is processed
    }
    // If this exception occurs the catch() method will be called
    catch(IOException e)
    {
        // handle exception here
    }
}

// The method is called
public run()
{
    while(true)
        {
            handleRequest();
        }
    }
}

It is possible for the TCP socket to be "closing" and your code to not have yet been notified. TCP 套接字可能“关闭”并且您的代码尚未收到通知。

Here is a animation for the life cycle.这是生命周期的动画。 http://tcp.cs.st-andrews.ac.uk/index.shtml?page=connection_lifecycle http://tcp.cs.st-andrews.ac.uk/index.shtml?page=connection_lifecycle

Basically, the connection was closed by the client.基本上,连接已被客户端关闭。 You already have throws IOException and SocketException extends IOException .您已经有throws IOExceptionSocketException extends IOException This is working just fine.这工作得很好。 You just need to properly handle IOException because it is a normal part of the api.您只需要正确处理IOException因为它是 api 的正常部分。

EDIT: The RST packet occurs when a packet is received on a socket which does not exist or was closed.编辑: RST数据包在不存在或已关闭的套接字上接收到数据包时发生。 There is no difference to your application.您的应用程序没有区别。 Depending on the implementation the reset state may stick and closed will never officially occur.根据实现, reset状态可能会保持不变,并且永远不会正式发生closed

This problem is usually caused by writing to a connection that had already been closed by the peer.此问题通常是由写入已被对等方关闭的连接引起的。 In this case it could indicate that the user cancelled the download for example.例如,在这种情况下,它可能表明用户取消了下载。

I face this problem but resolution is very simple.我面临这个问题,但解决方法很简单。 I am writing the 1 MB file in 1024 Byte Buffer causing this issue.我在 1024 字节缓冲区中写入 1 MB 文件导致此问题。 To Understand refer code before and After Fix.了解修复前后的参考代码。

Code with Excepion异常代码

DataOutputStream dos = new DataOutputStream(s.getOutputStream());
        FileInputStream fis = new FileInputStream(file);
        byte[] buffer = new byte[1024];
        
        while (fis.read(buffer) > 0) {
            dos.write(buffer);
        }
    

After Fixes:修复后:

DataOutputStream dos = new DataOutputStream(s.getOutputStream());
        FileInputStream fis = new FileInputStream(file);
        byte[] buffer = new byte[102400];
        
        while (fis.read(buffer) > 0) {
            dos.write(buffer);
        }

    

暂无
暂无

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

相关问题 java.net.SocketException:对等连接重置:传输音频时套接字写入错误 - java.net.SocketException: Connection reset by peer: socket write error When transmitting Audio (java.net.SocketException) 在处理请求时被捕获:连接被对等重置:套接字写入错误 - (java.net.SocketException) caught when processing request: Connection reset by peer: socket write error ClientAbortException:java.net.SocketException:由peer重置连接:套接字写入错误 - ClientAbortException: java.net.SocketException: Connection reset by peer: socket write error “ java.net.SocketException:对等体重置连接:套接字写入错误”我该如何解决 - “java.net.SocketException: Connection reset by peer: socket write error” How can I solve it ClientAbortException:java.net.SocketException:对等重置连接:套接字写入错误 - ClientAbortException: java.net.SocketException: Connection reset by peer: socket write error 我收到 java.net.SocketException: Connection reset by peer: socket write error ,当在类截图中执行主函数时 - I am getting java.net.SocketException: Connection reset by peer: socket write error ,when executing main function in class screenshot Influxdb-java:org.influxdb.InfluxDBIOException:java.net.SocketException:通过对等方重置连接:套接字写入错误 - influxdb-java: org.influxdb.InfluxDBIOException: java.net.SocketException: Connection reset by peer: socket write error java.net.socketexception connection reset by peer socket write error 通过Jenkins在Tomcat7上部署war时(使用Maven) - java.net.socketexception connection reset by peer socket write error While deploying war on Tomcat7 through Jenkins (Using Maven) 当发送沉重的json将近5MB时,它将给我java.net.SocketException:连接被同级重置:套接字写入错误 - While sending heavy json almost 5MB it will give me java.net.SocketException: Connection reset by peer: socket write error 错误java.net.SocketException:连接重置 - ERROR java.net.SocketException: Connection reset
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM