go - 在 Go 中使用 conn.Read() 读取前几个字节后,如何继续读取剩余字节?
问题描述
我正在尝试从 Go 中的 tcp 连接中读取数据。我有一个特定的标题格式,我首先要解析。因此,请读取前 12 个字节,其中包含与标头相关的信息。这告诉我剩余的消息长度是一定数量的字节。在这种情况下 1607 字节。我尝试了以下代码来读取标头字节,然后尝试读取剩余字节。
import ("net"
log "github.com/sirupsen/logrus"
"bytes"
"encoding/binary"
)
func handleRequest(conn net.Conn) {
// Structs to make header parsing easiers
type encapSTHdrMsgType uint16
type encapSTHdrMsgEncap uint16
type encapSTHdr struct {
MsgType encapSTHdrMsgType
MsgEncap encapSTHdrMsgEncap
MsgHdrVersion uint16
Msgflag uint16
Msglen uint32
}
// Make a buffer to hold header data.
headerBuf := make([]byte, 12)
// Read the incoming header info into the buffer.
_, err := conn.Read(headerBuf)
if err != nil {
log.Debug("Error reading:", err.Error())
}
// Header is in big endian
var header encapSTHdr
headerReader := bytes.NewReader(headerBuf)
err = binary.Read(headerReader, binary.BigEndian, &header)
if err != nil {
log.Debugf("Could not read header bytes into the buffer: %v", err)
}
messageBuf := make([]byte, header.Msglen)
messageBufLen, err := conn.Read(messageBuf)
if err != nil {
log.Debugf("Error reading messages: %s", err.Error())
}
log.Debugf("The message buffer length is: %d", messageBufLen)
log.Debugf("The header message length is: %d", header.Msglen)
}
当我尝试通过创建所需长度为 1607 的新缓冲区来读取剩余字节时,我得到一个包含数据的新字节数组,但它的长度仅为 1228。因此,我有两个问题:
1) 为什么 conn.Read() 不读取剩余字节?
2)现在我知道我可以使用 for 循环,直到我读取了所需的字节数并在完成后中断,但我想知道是否有更好的方法来做到这一点?
解决方案
当数据可用时读取返回,而不是在填充缓冲区时返回。使用io.ReadFull()读取直到缓冲区已满或从连接读取错误。
_, err := io.ReadFull(conn, headerBuf)
...
_, err := io.ReadFull(conn, messageBuf)
推荐阅读
- java - IntelliJ IDEA 定义的 maven pom 中的错误
- node.js - 在节点 js 模块中保存变量
- javascript - date-fns:在任意时区添加天数
- python - 如何从 Python 中的字典列表中删除项目
- python - 在 Python 数据类中,为什么 InitVar 可以有默认值但没有 default_factory?
- python - 从 polybase 字符串中替换多个“”
- postgresql - PostgreSQL:创建一个生成的列
- powershell - 我在哪里可以找到与事件中心兼容的路径,以及 Azure 门户中 IoT 中心的服务主键
- python - i am sending commands through serial port in python but they are sent multiple times instead of one
- sql - Extract a particular substring from value