Sending C array over gob ; decoding shows wrong data

53 views
Skip to first unread message

kanikak...@gmail.com

unread,
Dec 18, 2017, 7:51:26 AM12/18/17
to golang-nuts
I am sending a C structure which looks like this

typdef struct 
{
 unsigned char field1;
 unsigned char field2;
 unsigned char array[10];
}complete_data_t;

I am encoding this data using GOB and sending as UDP broadcast in a very small network. On the receiver side, all fields are correctly getting parsed but all array elements are coming as zero. I am populating these array elements with a non-zero value at the transmitter side.

var data C.complete_data_t

Transmitter side:
var buffer bytes.Buffer
err := gob.NewEncoder(&buffer).Encode(&data)
  _,err = conn.Write(buffer.Bytes())
buffer.Reset()

Receiver side:
rx_buf := make([]byte, 1024)
length,addr,err := conn.ReadFrom(rx_buf)
buffer := bytes.NewBuffer(rx_buf[:length])
err = gob.NewDecoder(buffer).Decode(&data)


  

Tamás Gulácsi

unread,
Dec 18, 2017, 8:04:32 AM12/18/17
to golang-nuts
Use a Go struct for GOB encoding/decoding.

Bryan Mills

unread,
Dec 19, 2017, 5:53:38 PM12/19/17
to golang-nuts
Per https://golang.org/pkg/encoding/gob/, “Structs encode and decode only exported fields.”
The fields of your C struct are unexported in Go, since they start with lower-case letters.

If the gob package is encoding or decoding the non-array fields of the struct, *that* is the bug.
Reply all
Reply to author
Forward
0 new messages