- 主题:求指路:udp发送固定长度sring
你这里的 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.*
嗯,你是对的,我没仔细看。
那就换成 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.*
27 bytes 那里可以简化一下,
直接用 bytes.Repeat 生成个切片发出去就好了,类似于 memset:
$ go doc bytes.Repeat
package bytes // import "bytes"
func Repeat(b []byte, count int) []byte
Repeat returns a new byte slice consisting of count copies of b.
It panics if count is negative or if the result of (len(b) * count)
overflows.
【 在 lioncat7 (lioncat) 的大作中提到: 】
: 写完了, 感谢指路
: 部分关键代码
: import (
: "encoding/binary"
: )
: buff := bytes.NewBuffer([]byte{})
: var abc int32
: abc = 7777
: err = binary.Write(buff, binary.BigEndian, &abc)
: if err != nil {
: log.Panic(err)
: }
: content := "abcdefg"
: err = binary.Write(buff, binary.BigEndian, []byte(content))
: if err != nil {
: log.Panic(err)
: }
: var zeroByte uint8
: /* fixed length 27 bytes */
: end := 27 - len(content)
: for i := 1; i <= end; i++ {
: err = binary.Write(buff, binary.BigEndian, &zeroByte)
: if err != nil {
: log.Panic(err)
: }
: }
: sendBuff := buff.Bytes()
: _, err = socket.Write(sendBuff) // 发送数据
: if err != nil {
: fmt.Println("发送数据失败,err: ", err)
: return
: }
--
FROM 27.38.197.*
直接看手册,甚至看源码
有几个实用的命令,给你介绍一下。假设你用的是 Go 的较新版本,那么:
$ GO111MODULE=off GOPATH=$(go env GOROOT) go list all
就可以看到所有的标准库,相应的源代码就在 cd $(go env GOROOT)/src 下面,
然后你再用:
$ GO111MODULE=off go doc encoding/binary
就可以看到每个模块的文档,进一步关于 API 的文档也是可以看到的,
你自己 go help doc 了解一下
我为了方便起见,把上面的命令封装了一个别名,特别好用:
$ alias golist='GO111MODULE=off GOPATH=$(go env GOROOT) cachecmd -async -ttl=100000m -key="golist" go list all 2>/dev/null | grep -v "vendor\|internal\|example" | fzf --preview "go doc {}" --bind "enter:execute:(go doc {}) | less" --bind "ctrl-f:page-down" --bind "ctrl-b:page-up"'
命令比较复杂,主要是:
* 使用 go list all 来获取所有的模块列表
* 使用 go doc 来查看模块文档
* 使用 cachecmd 来加速 go list all
* 使用 fzf 来预览列表、过滤列表和快速补全,以及预览文档内容
希望对版友有所帮助。
BTW:刚想起来其实还有个 web 版的工具,好像叫 godoc 什么的,但似乎新版本已经移除了,
官网的文档也是这个 godoc 生成的,但因为在墙外再加上我喜欢看 term 所以就没折腾本地的 godoc
印象中 godoc 里的内容还是很全的,自己安装的第三方包也都会生成文档。
【 在 lioncat7 (lioncat) 的大作中提到: 】
: 解决了
: repeateTime := 27 - len(incoming.SrcAddr)
: if repeateTime != 0 {
: binary.Write(buff, binary.BigEndian, bytes.Repeat([]byte{0}, repeateTime))
: }
: 另外请问下 我只过了一遍 the go programming language
: 如果想快速过一下go都提供了哪些基础库 有什么推荐么
--
修改:flw FROM 27.38.197.*
FROM 27.38.197.*