我试图将编码的 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()
}
UYOU
相关分类