douju2014 2018-08-06 21:22
浏览 65
已采纳

将CSV文件服务器发送到客户端

I've just recently started trying to learn Go and I'm trying to write a small server/client application for sending a csv file, from a server to a client. I'm running into an invalid type error when trying to encode a struct into BigEndian binary. My struct seems to already be in a binary format, I'm unsure why I get the following error:

Error writing binary buffer to big endian binary.Write: invalid type *main.DataPack

Also, I'd like to keep the TCP connection open after sending the file, that's why I'm not using io.Copy.

Currently I'm triggering the handling of the file upload through by sending a '\x00' byte:

// Server
package main 

import (
    "path/filepath"
    "fmt"
    "io/ioutil"
    "net"
    "os"
    "encoding/binary"
    "bytes"

)


type DataPack struct {
    Length   int64
    Contents []byte
}

func main() {

    absPath, _ := filepath.Abs("./progs.csv")

    data, _ := ioutil.ReadFile(absPath)
    fmt.Println("%s",data)

    tel, err := net.Listen("tcp", "0.0.0.0:23")
    if err != nil {
        fmt.Println(err)
        return
    }

    for {
        conn, err := tel.Accept()
        if err != nil {
            break
        }
        fmt.Println("above filehandler")
        go fileHandler(conn)
    }
}

func fileHandler(conn net.Conn) {

    buf := make([]byte, 0)
    buf = append(buf, '\x00')
    conn.Write(buf)
    absPath, _ := filepath.Abs("./progs.csv")

    file, err := os.Open(absPath)
    defer file.Close()

    fi, err := file.Stat()
    if err != nil {
        fmt.Println("Fatal error reading file: ", err)
    }

    fmt.Println("This is the length of the file: ", fi.Size())

    data := &DataPack{Length: fi.Size()} // , Contents: }

    data.Contents = make([]byte, data.Length)
    n, err := file.Read(data.Contents)
    if err != nil {
        fmt.Println("error reading contents into struct: ", err)
    }
    fmt.Println("tried to read file contents: ", n)
    fmt.Println("DataPack: %+v", data) 

    buf1 := new(bytes.Buffer)
    err = binary.Write(buf1, binary.BigEndian, &data)

    if err != nil {
        fmt.Println("Error writing binary buffer to big endian ", err)
    }
    conn.Write(buf1.Bytes())  
}

Here is the client

package main

import (
    "log"
    "fmt"
    "net"
    "strings"
    "strconv"
    "bufio"
)

const (
    host = "127.0.0.1"
    port = 23
)

func main() {

addr := strings.Join([]string{host, strconv.Itoa(port)}, ":")
client := NewClient()
var err error

client.socket, err = net.Dial("tcp", addr)
if err != nil {
    log.Println("error setting up socket: %s", err)
}

for {

    m := bufio.NewReader(client.socket)
    b, err := m.ReadByte()
    if err != nil {

        fmt.Println("here is the error: ", err)
    }
    if b == '\x00'{
        fmt.Println("about to receive a file!!!")
        b, _ := m.ReadByte()
        fmt.Println("just got another byte ", b )
    }

} 

log.Printf("Over")

}

  • 写回答

1条回答 默认 最新

  • dougao2830 2018-08-07 08:48
    关注

    why you get the error

    Visit https://golang.org/pkg/encoding/binary/#Write

    Write writes the binary representation of data into w. Data must be a fixed-size value or a slice of fixed-size values, or a pointer to such data.

    type DataPack struct { Length int64 Contents []byte }

    Contents isn't a fixed-size value, so you got the invalid type error.

    how to solve it

    1. go binary
    2. json
    3. others
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?