你这里的 test.abc = 5 最好改成 test.abc = htons(5),
然后 Go 这边你用 encoding/binary 就可以读了,参见:
$ go doc encoding/binary.Read
package binary // import "encoding/binary"
func Read(r io.Reader, order ByteOrder, data interface{}) error
Read reads structured binary data from r into data. Data must be a pointer
to a fixed-size value or a slice of fixed-size values. Bytes read from r are
decoded using the specified byte order and written to successive fields of
the data. When decoding boolean values, a zero byte is decoded as false, and
any other non-zero byte is decoded as true. When reading into structs, the
field data for fields with blank (_) field names is skipped; i.e., blank
field names may be used for padding. When reading into a struct, all
non-blank fields must be exported or Read may panic.
The error is EOF only if no bytes were read. If an EOF happens after reading
some but not all the bytes, Read returns ErrUnexpectedEOF.
【 在 lioncat7 (lioncat) 的大作中提到: 】
: 收json,发送私有协议udp
: udp对应c代码大概是
: struct demo{
: int abc;
: char content[32];
: };
: struct demo test;
: memset(&test, 0, sizeof(test));
: test.abc =5;
: strcpy(test.content, "my test");
: send(fd, &test, sizeof(test));
: go里面好像不能指定string的固定长度,我解析json之后,难道只能一个字节一个字节赋值?
--
修改:flw FROM 27.38.197.*
FROM 27.38.197.*