0

我正在尝试将编码的 blob 发送到本地对等点列表(在本地机器上的多个端口上侦听的客户端代码),当我为某些对等点解码 blob 时,在对等点上它可以工作,但对于某些它不并显示错误(gob:未知类型 id 或损坏的数据),我该如何解决?

//My blob struct
type packet struct {
    Nodes []a1.Node
    Ledger *a1.Block
    ActionType string
}


//Encoding Part
    for i:=1; i<len(ConnectedNodes);i++{
        var blob packet
        blob.Ledger = Ledger
        blob.ActionType = "AddBlock"
        blob.Nodes = []a1.Node{}
        conn, err := net.Dial("tcp", "localhost"+":"+ConnectedNodes[i].Port)
        if err != nil {
            // handle error
            fmt.Println("Error At Client In Making A Connection (sending New Block to client "+ ConnectedNodes[i].Name +")")
        }
        //sending request type to client
        _, _ = conn.Write([]byte("newblock add "))
        //sending data to the client node
        //gob.Register(blob)
        encoder := gob.NewEncoder(conn)
        error := encoder.Encode(blob)
        if error != nil {
            log.Fatal(error)
        }
    }

//Decoding Part running on another peer
//adds new block to the Ledger

//Recieving incoming data
    recvdSlice := make([]byte, 256)
    conn.Read(recvdSlice)
    RecievedData := string(recvdSlice)
    finalData := strings.Split(RecievedData, " ")

    if finalData[0] == "newblock"{
        var blob packet
        decoder := gob.NewDecoder(conn)
        err := decoder.Decode(&blob)
        if err != nil {
            fmt.Println("error at decoding New Block on client!")
            fmt.Println(err)
        }
        fmt.Println(Ledger.Hash)
        fmt.Println(blob.Ledger.Hash)
        if(bytes.Compare(Ledger.Hash, blob.Ledger.Hash)) == 0 {
            fmt.Println("Ledger is already updated !")
        }else{
            fmt.Println("New Block Added !")
            Ledger = blob.Ledger
        }
        a1.ListBlocks(Ledger)
        //SendingNewBlockToConnectedNodes()
    }
4

1 回答 1

1

发送者在编码 gob ( ) 之前写入 13 个字节"newblock add "

如果接收端在解码 gob 之前没有读取这 13 个字节,那么解码器将与数据流不同步并报告错误。

当数据可用、切片已填充或读取连接出错时,连接 Read 方法返回。忽略错误,对连接的 Read 调用将从连接中读取 1 到 len(recvdSlice) 字节。不能保证读取 13 个字节的数据,但由于时间原因,在实践中经常发生这种情况。

通过在解码 gob 之前仅读取前缀来修复。一种方法是用换行符分隔前缀。

将发件人代码更改为:

 _, _ = conn.Write([]byte("newblock add \n"))

将接收方代码更改为:

 br := bufio.NewReader(conn)
 receivedData, err := br.ReadString('\n')
 if err != nil {
     // handle error
 }
 finalData := strings.Split(receivedData, " ")

 if finalData[0] == "newblock"{
    var blob packet
    decoder := gob.NewDecoder(br) // <-- decode from the buffered reader
    err := decoder.Decode(&blob)

另一个解决方法是使用 gob 编解码器作为前缀。将发件人更改为:

    encoder := gob.NewEncoder(conn)
    if err := encoder.Encode("newblock add "); err != nil {
        // handle error
    }
    if err := encoder.Encode(blob); err != nil {
        // handle error
    }

将接收器更改为:

decoder := gob.NewDecoder(conn)
var receivedData string
if err := decoder.Decode(&receivedData); err != nil {
     // handle error
}
finalData := strings.Split(receivedData, " ")

if finalData[0] == "newblock"{
    var blob packet
    err := decoder.Decode(&blob)
于 2019-10-12T20:26:47.257 回答