- 主题:求指路:udp发送固定长度sring
收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之后,难道只能一个字节一个字节赋值?
--
FROM 106.120.101.*
go 里面有byte
【 在 lioncat7 (lioncat) 的大作中提到: 】
: 收json,发送私有协议udp
:
: udp对应c代码大概是
: struct demo{
--
FROM 222.209.92.*
如果传内存原始buffer,跨机器跨平台的情况下,不是好主意,涉及字节序,大小端,数据宽度。
如果是传json,可以参考json包的Unmarshal 和Marshall吧
【 在 lioncat7 的大作中提到: 】
:
: 收json,发送私有协议udp
:
: udp对应c代码大概是
: struct demo{
--
FROM 117.176.243.*
无比正常的需求(特别是udp),在web时代居然成了不是好主意了…
网络字节序,定宽整数类型,填充对齐控制都是网络编程基本内容
【 在 gpmn 的大作中提到: 】
: 如果传内存原始buffer,跨机器跨平台的情况下,不是好主意,涉及字节序,大小端,数据宽度。
:
: 如果是传json,可以参考json包的Unmarshal 和Marshall吧
: ...................
--
FROM 114.249.199.*
go 的 string 是 utf-8 编码的变长容器,有长度,没有尾 \0。和 c 的字符串字面量不是一回事。
转 []byte 取切片复制到数组就行。
【 在 lioncat7 的大作中提到: 】
: 收json,发送私有协议udp
:
: udp对应c代码大概是
: ...................
--
FROM 114.249.199.*
你这里的 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.*
我感觉楼主是要用go,收json,然后转成私有udp发出去。
【 在 flw 的大作中提到: 】
: 你这里的 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.
: --
发自「今日水木 on 鸿蒙」
--
FROM 73.93.166.*
谢谢了
【 在 flw 的大作中提到: 】
: 你这里的 test.abc = 5 最好改成 test.abc = htons(5),
: 然后 Go 这边你用 encoding/binary 就可以读了,参见:
: $ go doc encoding/binary.Read
: ...................
--
FROM 106.120.101.*
嗯,你是对的,我没仔细看。
那就换成 write 吧!
$ go doc encoding/binary.Write
package binary // import "encoding/binary"
func Write(w io.Writer, order ByteOrder, data interface{}) error
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.
Boolean values encode as one byte: 1 for true, and 0 for false. Bytes
written to w are encoded using the specified byte order and read from
successive fields of the data. When writing structs, zero values are written
for fields with blank (_) field names.
别忘了明确字节序:
$ go doc encoding/binary.BigEndian
package binary // import "encoding/binary"
var BigEndian bigEndian
BigEndian is the big-endian implementation of ByteOrder.
$ go doc encoding/binary.LittleEndian
package binary // import "encoding/binary"
var LittleEndian littleEndian
LittleEndian is the little-endian implementation of ByteOrder.
网络字节序是 BigEndian,条件允许的话,一般建议用这个。
【 在 adu (阿杜) 的大作中提到: 】
: 我感觉楼主是要用go,收json,然后转成私有udp发出去。
: 发自「今日水木 on 鸿蒙」
--
FROM 27.38.197.*
是你没理解人家说啥。
低层代码考虑的东西多,web层考虑的就是应用了。
主要是字节序处理好
【 在 milksea 的大作中提到: 】
: 无比正常的需求(特别是udp),在web时代居然成了不是好主意了…
: 网络字节序,定宽整数类型,填充对齐控制都是网络编程基本内容
: --
发自「今日水木 on Mi 10 Pro」
--
FROM 111.198.53.*