【问题标题】:How do I keep reading the remaining bytes after reading the first few bytes using conn.Read() in Go?在 Go 中使用 conn.Read() 读取前几个字节后,如何继续读取剩余字节?
【发布时间】:2019-03-02 17:51:54
【问题描述】:

我正在尝试从 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 循环,直到我读取了所需的字节数并在完成后中断,但我想知道是否有更好的方法来做到这一点?

【问题讨论】:

    标签: go


    【解决方案1】:

    Read 在数据可用时返回,而不是在填充缓冲区时返回。使用io.ReadFull() 读取,直到缓冲区已满或从连接读取错误。

    _, err := io.ReadFull(conn, headerBuf)
    
    ...
    
    _, err := io.ReadFull(conn, messageBuf)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-22
      • 1970-01-01
      • 2021-04-12
      • 1970-01-01
      相关资源
      最近更新 更多