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

将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
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R