Home > Software design >  Compress and transfer file via TCP (Golang)
Compress and transfer file via TCP (Golang)

Time:04-06

I write simple example code, it worked, but size of file that recived is not compressed

My client (for connect to server and send file):

    // connect to server
    conn, err := net.Dial("tcp", serverAddr)
    CheckError(err)
    defer conn.Close()

    in, err := os.Open(srcFile)
    if err != nil {
        log.Fatal(err)
    }
    pr, pw := io.Pipe()
    gw, err := gzip.NewWriterLevel(pw, 7)
    CheckError(err)
    go func() {
        n, err := io.Copy(gw, in)
        gw.Close()
        pw.Close()
        log.Printf("copied    %v %v", n, err)
    }()
        //maybe error some next?
    _, err = io.Copy(conn, pr)

Please, help, how right to use pipe with copy

CodePudding user response:

As I already said in the comment, your code works. I created a little example to test or see if I can solve your problem. So I guess you can close this question.

package main

import (
    "compress/gzip"
    "io"
    "log"
    "net"
    "os"
)

func main() {
    // Create a listener on a random port.
    listener, err := net.Listen("tcp", "127.0.0.1:")
    if err != nil {
        log.Fatal(err)
    }
    log.Println("Server listening on: "   listener.Addr().String())
    done := make(chan struct{})
go func() {
    defer func() { done <- struct{}{} }()
    for {
        conn, err := listener.Accept()
        if err != nil {
            log.Println(err)
            return
        }
        go func(c net.Conn) {
            defer func() {
                c.Close()
                done <- struct{}{}
            }()
            buf := make([]byte, 1024)
            for {
                n, err := c.Read(buf)
                if err != nil {
                    if err != io.EOF {
                        log.Println(err)
                    }

                    return
                }
                log.Printf("received: %q", buf[:n])
                log.Printf("bytes: %d", n)

            }

        }(conn)
    }
}()

    conn, err := net.Dial("tcp", listener.Addr().String())
    if err != nil {
        log.Fatal(err)
    }
    log.Println("Connected to server.")

    file, err := os.Open("./file.txt")
    if err != nil {
        log.Fatal(err)
    }

    pr, pw := io.Pipe()
    w, err := gzip.NewWriterLevel(pw, 7)
    if err != nil {
        log.Fatal(err)
    }
    go func() {
        n, err := io.Copy(w, file)
        if err != nil {
            log.Fatal(err)
        }
        w.Close()
        pw.Close()
        log.Printf("copied to piped writer via the compressed writer: %d", n)
    }()

    n, err := io.Copy(conn, pr)
    if err != nil {
        log.Fatal(err)
    }
    log.Printf("copied to connection: %d", n)

    conn.Close()
    <-done
    listener.Close()
    <-done
}

The output of that program with a simple text file with many repeated characters in it, to have something to compress: The file is 153 bytes and I send/received 46 bytes

2022/04/04 11:23:58 Server listening on: 127.0.0.1:58250
2022/04/04 11:23:58 Connected to server.
2022/04/04 11:23:58 received: "\x1f\x8b\b\x00\x00\x00\x00\x00\x00\xff"
2022/04/04 11:23:58 bytes: 10
2022/04/04 11:23:58 copied to piped writer via the compressed writer: 153
2022/04/04 11:23:58 copied to connection: 46
2022/04/04 11:23:58 received: "*I-.I,NI,N\xc1\x01\x8aS\x8a\x13i\bx\xb9pX    \r\b\x00\x00\xff\xff\xc7\xfe\xa6c\x99\x00\x00\x00"
2022/04/04 11:23:58 bytes: 36
2022/04/04 11:23:58 accept tcp 127.0.0.1:58250: use of closed network connection
  • Related