doujia1679 2018-10-31 15:21
浏览 287

golang读取更多4096字节

I try read file from TLS connect, but I can read only 4096 bytes (n = 4096). How I can read full file?

reader := bufio.NewReader(pc.conn)
msg := make([]byte, 10*1024*1024)
n,err:=reader.Read(msg)
  • 写回答

1条回答 默认 最新

  • douwang6635 2018-10-31 16:14
    关注

    io.Reader.Read(p []byte) — if succeeds, — is free to return any number of bytes between 1 and len(p); this is by its contract:

    Read reads up to len(p) bytes into p. It returns the number of bytes read (0 <= n <= len(p)) and any error encountered. Even if Read returns n < len(p), it may use all of p as scratch space during the call. If some data is available but not len(p) bytes, Read conventionally returns what is available instead of waiting for more.

    (Emphasis mine.)

    The "magic number" 4096 you're observing is likely the size of the cache of some piece of software under your TLS connection.

    This actually matches the contract of the read(2) POSIX syscall (for sockets, it actually will be recv(2) — from "BSD sockets" which every platform supported by Go implements, including Windows; its counterpart from Winsock has the same semantics, FWIW).

    If you know exactly how many bytes you need to read from the source, use the io.ReadFull helper. This is usually the simplest approach to deal with data encoded in a TLV-like format (and encoding/binary helps as well).

    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题