尝试在Golang中使用io.CopyN()读取时客户端卡住了

时间:2018-09-08 08:49:39

标签: go tcp server client glide-golang

我正在尝试使TCP服务器传输文件。我正在起诉io.CopyN进行读写。从服务器端,我正在将文件发送到客户端,因此从服务器端,它完美地发送了所有字节,但是客户端端在读取了两个卡住的1000000字节后就发送了。有时工作正常,有时卡住。我正在使用300 MB pdf进行测试。任何帮助,代码和输出如下所示。

服务器

    package main

    import (
        "fmt"
        "io"
        "log"
        "net"
        "os"
        "strconv"
        "strings"
    )

    func main() {

        ls, err := net.Listen("tcp", ":1234")

        errFunc(err)

        defer ls.Close()

        conn, _ := ls.Accept()

        defer conn.Close()

        for {

            file, err := os.Open(strings.TrimSpace("./" + "Mag" + ".pdf"))

            errFunc(err)

            defer file.Close()

            fileInfo, err := file.Stat()

            errFunc(err)

            size := fileInfo.Size()

            numberOfTime := size / 1000000

            leftByte := size - numberOfTime*1000000

            numberOfTimeString := strconv.Itoa(int(numberOfTime))
            leftByteString := strconv.Itoa(int(leftByte))

            fmt.Println("1000000 times : ", numberOfTimeString)

            fmt.Println("Left Bytes : ", leftByteString)

            _, err = fmt.Fprintf(conn, numberOfTimeString+"\n")

            errFunc(err)

            _, err = fmt.Fprintf(conn, leftByteString+"\n")

            errFunc(err)

            fileWriter := io.Writer(conn)

            for i := 0; i < int(numberOfTime); i++ {

                n, err := io.CopyN(conn, file, 1000000)

                if i >= 30 {
                    fmt.Println(err, n)
                }
            }

            n, err := io.CopyN(fileWriter, file, leftByte+1)

            if err == io.EOF {
                fmt.Println(err, n)
            }

            fmt.Printf("Succefully bytes sent : %v \n\n\n\n\n", n)

            file.Close()

        }

    }

    func errFunc(err error) {

        if err != nil {
            log.Fatal(err)
        }

    }

客户端

    package main

    import (
        "bufio"
        "fmt"
        "io"
        "net"
        "os"
        "os/signal"
        "strconv"
        "strings"
        "syscall"
    )

    func main() {

        c := make(chan os.Signal, 15)
        signal.Notify(c, syscall.SIGINT)

        go func() {

            for {
                s := <-c

                switch s {

                case syscall.SIGINT:
                    os.Exit(1)
                }

            }

        }()

        conn, _ := net.Dial("tcp", ":1234")

        defer conn.Close()

        connReadWrite := bufio.NewReader(io.Reader(conn))

        var i int
        var filename string

        for {

            i++

            nu := strconv.Itoa(i)

            filename = "image" + nu + ".pdf"

            file, err := os.Create(filename)

            defer file.Close()

            numberOfTimeString, err := connReadWrite.ReadString('\n')

            if err != nil {

                fmt.Println(err)
            }

            println("1000000 times :", numberOfTimeString)

            numberOfTimeString = strings.TrimSuffix(numberOfTimeString, "\n")

            numberOfTime, err := strconv.Atoi(numberOfTimeString)

            if err != nil {

                fmt.Println(err)
            }

            leftByteString, err := connReadWrite.ReadString('\n')

            if err != nil {

                println(err)
            }

            println("Left Bytes :", leftByteString)

            leftByteString = strings.TrimSuffix(leftByteString, "\n")

            leftByte, err := strconv.Atoi(leftByteString)

            if err != nil {

                panic(err)
            }

            fmt.Println("After convert in Num :", numberOfTime, leftByte)

            newFileWriter := io.Writer(file)
            newFileReader := io.Reader(conn)

            for i := 0; i < numberOfTime; i++ {

                n, err := io.CopyN(newFileWriter, newFileReader, 1000000)

                if i >= 30 {
                    errFun(err, n)
                }
            }

            n, err := io.CopyN(newFileWriter, newFileReader, int64(leftByte))

            errFun(err, n)

            fmt.Printf("sucessfully Transfered ---> \n\n\n\n\n\n")

        }

    }

    func errFun(err error, n int64) {

        if err == io.EOF {

            fmt.Println("End of file : ", n)
            return

        } else if n == 0 {

            fmt.Println("n is : ", n)
            return

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

        }

        fmt.Println(err, " : ", n)
    }

输入/输出

首先从服务器端发送需要读取的字节数,然后从客户端获取需要读取的字节数,然后我发送文件然后读取。在图片中,我能够第二次发送一次卡住了的东西,有时也第一次卡住了,它会读取“%PDF ...”,甚至不打印“ 100000次:”,它正确地打印“%??? 00次:”,我只是不明白

enter image description here

1 个答案:

答案 0 :(得分:2)

我认为问题在于您在客户端中使用的是bytes.Buffer

connReadWrite := bufio.NewReader(io.Reader(conn))

但是您以后不会在CopyN中使用它:

newFileWriter := io.Writer(file)
newFileReader := io.Reader(conn)
for i := 0; i < numberOfTime; i++ {
    _, err := io.CopyN(newFileWriter, newFileReader, 1000000)
    if err != nil {
        log.Fatalln(err)
    }
}

使用:

newFileWriter := io.Writer(file)

for i := 0; i < numberOfTime; i++ {
    _, err := io.CopyN(file, connReadWrite, 1000000)
    if err != nil {
        log.Fatalln(err)
    }
}

可以修复它。

如果您可以控制用于发送文件的协议,建议您做一些简单的事情。例如,使用big-endian int64长度前缀。

发送:

func sendFile(name string, conn net.Conn) error {
    f, err := os.Open(name)
    if err != nil {
        return err
    }
    defer f.Close()

    fi, err := f.Stat()
    if err != nil {
        return err
    }
    sz := fi.Size()

    buf := bufio.NewWriter(conn)

    err = binary.Write(buf, binary.BigEndian, sz)
    if err != nil {
        return err
    }

    _, err = io.CopyN(buf, f, sz)
    if err != nil {
        return err
    }

    return buf.Flush()
}

接收:

func recvFile(name string, conn net.Conn) error {
    f, err := os.Create(name)
    if err != nil {
        return err
    }
    defer f.Close()

    buf := bufio.NewReader(conn)
    var sz int64
    err = binary.Read(buf, binary.BigEndian, &sz)
    if err != nil {
        return err
    }

    _, err = io.CopyN(f, buf, sz)
    if err != nil {
        return err
    }

    return nil
}