GoLang文件传输停止,没有任何错误 [英] GoLang File Transfer Stops Without Any Error

查看:66
本文介绍了GoLang文件传输停止,没有任何错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 go 中运行一个简单的文件传输代码.从服务器端,我尝试一次读取1024字节的数据.从客户端,我正在使用 io.Copy 函数来传输文件.文件传输成功完成70%的时间.但是在30%的时间内失败了.它卡在 connection.Read 函数上.通过一些实验,我发现它仅在以前的 Read 操作读取的数据少于 1024 个字节的情况下发生.

I've running a simple file transfer code in go. From the server side I try to read 1024 bytes of data at a time. From client side I'm using the io.Copy function to transfer the file. The file transfer completes successfully 70% of the time. But in 30% of time it fails. It gets stuck on connection.Read function. Through some experiments I saw that it happens only on the occasion where previous Read operation reads less than 1024 bytes of data.

因此,从我的代码中,理想情况下,它应该在所有连续的 Read 上读取文件的 1024 字节数据,但文件传输的最后一个 Read 除外已经完成.但是在某些情况下,如果读取的内容少于 1024 个字节,则下一个 Read 操作会卡住而不会引发任何错误.这是我的代码:

So from my code, ideally it should read 1024 bytes of data on all successive Read except the very last Read where the file transfer is complete. But on some occasion if it reads less than 1024 bytes, the next Read operation gets stuck without throwing any error. Here is my code:

  1. Server.go

  1. Server.go

fileBuffer := make([]byte, BUFFER_SIZE)     //BUFFER_SIZE is a constant of 1024
bytesRead := int64(0)
count := 0
for {
    if fileSize-bytesRead < int64(BUFFER_SIZE) {        //fileSize is the size of file in bytes, which I calculated earlier.
        fileBuffer = make([]byte, fileSize-bytesRead)
    }

    fmt.Println("Reading ", BUFFER_SIZE, " bytes of data")

    n, err := connection.Read(fileBuffer)

    count++

    fmt.Println("Completed reading", n, " bytes of data, count=", count)

    file.Write(fileBuffer[0:n])

    bytesRead += int64(n)
    fmt.Println("So far read", bytesRead, " bytes of data")

    if err != nil {
        fmt.Println(err)
    }

    if err == io.EOF {
        result.Message = "File transfer incomplete"
        break
    }

    if bytesRead >= fileSize {
        result.Message = "File transfer complete"
        break
    }
}

  • Client.go

  • Client.go

    n, err := io.Copy(conn, file)
    fmt.Println(n, " bytes sent")
    

  • 对于我来说,成功传输后发送的字节数正确,接收的字节数也正确.同一文件出现问题.

    For my case, number of sent bytes is correct, number of received bytes is also correct on successful transfer. The problem happens for the same file.

    推荐答案

    TL; DR:这应该是复制文件所需的全部内容.

    TL;DR: This should be all you need to copy a file.

    if _, err := io.Copy(file, connection); err != nil {
        // handle error
    }
    

    如果您需要在复制之间做一些逻辑(例如,打印读取的字节)时需要分块复制,则可以使用

    If you need to copy in chunks while doing some logic in between copies (for example, printing the bytes read), you can use io.CopyN

    totalRead := int64(0)
    for {
        n, err := io.CopyN(outfile, infile, 1024)
        totalRead += n
        // print bytes read followed by a carriage return
        fmt.Printf("Bytes read: %d\r", totalRead)
        if err != nil {
            if err == io.EOF {
                fmt.Println() // print a newline
                break
            }
            // handle error
        }
    }
    

    这篇关于GoLang文件传输停止,没有任何错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆