繁体   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

我正在尝试使用套接字实现 HTTP 服务器。 如果客户端(例如浏览器)请求目录,则服务器会显示可用文件列表。 当客户端请求文件时会出现问题。 我收到以下错误:

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)

堆栈跟踪显示问题来自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();
}

但我无法弄清楚出了什么问题。 你能帮助我吗?

编辑

谢谢大家的回答。 阅读您的答案后,我明白问题出在发生错误时的 Socket。 这是我的错误代码:

// 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
    }
}

我的新代码如下所示:

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

TCP 套接字可能“关闭”并且您的代码尚未收到通知。

这是生命周期的动画。 http://tcp.cs.st-andrews.ac.uk/index.shtml?page=connection_lifecycle

基本上,连接已被客户端关闭。 您已经有throws IOExceptionSocketException extends IOException 这工作得很好。 您只需要正确处理IOException因为它是 api 的正常部分。

编辑: RST数据包在不存在或已关闭的套接字上接收到数据包时发生。 您的应用程序没有区别。 根据实现, reset状态可能会保持不变,并且永远不会正式发生closed

此问题通常是由写入已被对等方关闭的连接引起的。 例如,在这种情况下,它可能表明用户取消了下载。

我面临这个问题,但解决方法很简单。 我在 1024 字节缓冲区中写入 1 MB 文件导致此问题。 了解修复前后的参考代码。

异常代码

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

修复后:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM