[Solved] Read specific number of bytes which is in acceptable data


Try using this:

package main

import (
“encoding/binary”
“io”
)

func ReadPacket(r io.Reader) ([]byte, error) {
    lenB := make([]byte, 4)

    if _, err := r.Read(lenB); err != nil {
        return nil, err
    }

    //you can use BigEndian depending on the proto
    l := binary.LittleEndian.Uint32(lenB)

    packet := make([]byte, l)

    _, err := r.Read(packet)

    return packet, err
}

0

solved Read specific number of bytes which is in acceptable data